-1

Possible Duplicate:
Date Difference in php?
Compare 2 timestamps in PHP and get the days between?

I've 2 dates like 15/02/2012 and 18/03/2012, I need to know the number of days difference between them. Does such a utility exist in php?

Community
  • 1
  • 1
Stefano
  • 25
  • 2
  • 4

4 Answers4

2

date_diff is what you want.

fredley
  • 31,101
  • 42
  • 135
  • 231
1
$diff = (int) ( abs(strtotime($date1) - strtotime($date2)) / (60 * 60 * 24) );
echo 'diff:' . $diff
hsz
  • 143,040
  • 58
  • 252
  • 308
1

PHP has a function for this:

http://www.php.net/manual/en/datetime.diff.php

Example

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

Output

+2 days
0

You could turn those dates into timestamps:

$ts1 = strtotime("15-02-2012");
$ts2 = strtotime("18-03-2012");
$diff = ($ts2 - $ts1)/86400;

echo $diff . " days";
Rick Kuipers
  • 6,498
  • 2
  • 16
  • 36