-1

In php, if you have a string in the date/time format example "2013-09-08 00:25:31", how can you compare that with the current time, and get the difference in number of days?

Thanks.

omega
  • 40,311
  • 81
  • 251
  • 474

2 Answers2

1

you should check DateTime::diff

you may do it like

$now = new DateTime();
$prev = new DateTime('2013-09-08');
$interval = $now->diff($prev);
echo $interval->format('%R%a days');
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
1

You can use strtotime() to convert two dates to unix time and then calculate the number of seconds between them. From this it's rather easy to calculate different time periods.

$date1 = "2007-03-24";
$date2 = "2009-06-26";

$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);

You check this link for more answers.

Community
  • 1
  • 1
Dash
  • 314
  • 2
  • 14