-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
  • 32,953
  • 42
  • 145
  • 236
1
$diff = (int) ( abs(strtotime($date1) - strtotime($date2)) / (60 * 60 * 24) );
echo 'diff:' . $diff
hsz
  • 148,279
  • 62
  • 259
  • 315
  • I would recommend against using `strtotime` in favour of [`DateTime`](http://php.net/DateTime). –  Jan 24 '12 at 14:31
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,616
  • 2
  • 17
  • 37