0

Possible Duplicate:
How to calculate the difference between two dates using PHP?

Q : How to get the day(s) of difference between 2 dates?

e.g

<?php 
echo $date1 = strtotime(date('2012-10-11'));
echo "<br />";
echo $date2 = strtotime(date('2012-11-15'));
echo "<br />";
echo $result = $date2 - $date1;                     
?>

The $result should be 36 days. How to get the days?

Community
  • 1
  • 1
Thu Ra
  • 2,013
  • 12
  • 48
  • 76
  • http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php . you need to have a look on this – rOcKiNg RhO Nov 16 '12 at 05:17

2 Answers2

11

From PHP NET - http://www.php.net/manual/en/datetime.diff.php

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

OR procedural

<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>

Or the above as one-liner:

print date_create('2009-10-11')->diff(date_create('2009-10-13'))->d;
mario
  • 144,265
  • 20
  • 237
  • 291
Dinesh
  • 3,065
  • 1
  • 18
  • 19
0
echo $result= (strtotime("2012-11-15") - strtotime("2012-10-11")) / (60 * 60 * 24);
vivek salve
  • 991
  • 1
  • 9
  • 20