0

I need to compare a past/future date with current date in PHP and present the difference in a "4 hours until", or "2 days 3 hours until" or "5 hours ago" format. Optionally in a "-4h" (which is bad) or "4h" (which is good) format.

So in a example:

x = $expiry_date - $todays_date

If the result is positive, eg. $expiry_date is 4 hours in the future, x = "4 hours to go", or "4 hrs". If the result was negative, for example "4 hours ago" or "-4hrs".

Any other, similarly sounding result formats are fine.

Any help please?

TomaszRykala
  • 927
  • 6
  • 16
  • 33
  • If you get difference of timestamps, then it's just simple division by number of seconds in an hour, day etc. – MightyPork Aug 07 '13 at 10:31
  • Could you give me an example please? – TomaszRykala Aug 07 '13 at 10:31
  • http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago – Mircea Soaica Aug 07 '13 at 10:33
  • Please see the PHP manual for ['DataTime::diff()`](http://php.net/manual/en/datetime.diff.php). That should make it easy to get the hours/minutes/whatever values that you need. As for formatting it with "X hours in the future", you'll need to format the string yourself. – Spudley Aug 07 '13 at 10:33
  • Yes: http://www.php.net/manual/en/datetime.diff.php | and http://bakery.cakephp.org/articles/g2010a/2008/04/12/datehelper-for-fuzzy-date-differences – MightyPork Aug 07 '13 at 10:34

3 Answers3

3

Refer Dates diff in PHP

$date1 = "2013-08-11";
$date2 = "2012-07-12";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

Or

$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days "; 

//or simply
//echo "difference " . $interval->days . " days ";
softvar
  • 17,917
  • 12
  • 55
  • 76
  • Beware of using calculations like `$time += 24*60*60` to manipulate timestamps. This creates problems with daylight saving time and other timezone anomalies. Use methods like `$time = strtotime('+1 day', $time)` instead. – ironcito Aug 07 '13 at 10:42
1

You can do that using the Carbon library. It would go something like this:

$now = Carbon::now();
$then = Carbon::create(2013, 8, 8, 23, 26, 11);
echo $now->diffForHumans($then);     

The output will be a human readable string just like what you're looking for.

ciruvan
  • 5,143
  • 1
  • 26
  • 32
0

See this question.

The variable will contain the difference in seconds, which you can then divide as required to get minutes/hours/days etc. E.g. divide by 60 to get minutes, 60 again to get hours etc. Then do some simple value checking to format as required.

Community
  • 1
  • 1
Chris Brown
  • 4,445
  • 3
  • 28
  • 36