0

I need something like this

$today = date("m-d-Y");
$otherdate = "05-03-2013";

//Some math here

//echo the difference between the two dates in days

I've tried using strtotime but it doesn't work correctly and usually says something like the difference was somewhere around 15k days (which obviously isn't right)

Any help?

Kara
  • 6,115
  • 16
  • 50
  • 57

3 Answers3

1

Try diff() method of DateTime:

$now  = new DateTime();
$then = DateTime::createFromFormat('m-d-Y', '05-03-2013');

$interval = $now->diff($then);

$days = $interval->format('%a days');
BlitZ
  • 12,038
  • 3
  • 49
  • 68
  • I was just wondering, that works when i use a direct string like $then = DateTime::createFromFormat('m-d-Y', '05-03-2013'); but when i use $anotherdate = $row['laston']; $then = DateTime::createFromFormat('m-d-Y', $anotherdate); it does not work... – Douglas Mckee May 08 '13 at 02:17
  • @Douglas Mckee , sholud be working. Please, check string for consistency. – BlitZ May 08 '13 at 02:20
0

Try this for Y-m-d format date

$now = strtotime(date("Y-m-d")); // or your date as well
$otherdate = strtotime("2013-05-03");
$datediff = $now - $otherdate;
echo floor($datediff/(60*60*24)). "Days";
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • please reconsider your answer because if You use it when you change your clock (winter to summer time) there will be wrong result. Try `$now = strtotime("2013-04-01");` and `$otherdate = strtotime("2013-03-30");` – Soyale May 07 '13 at 10:14
0

PHP 5.3+

$b_date1 = new DateTime($date);
$today1 = new DateTime($today);
$interval = date_diff($today1,$b_date1);

PHP version less than 5.2

Or

function date_diff1($date1, $date2) { 
    $current = $date1; 
    $datetime2 = date_create($date2); 
    $count = 0; 
    while(date_create($current) < $datetime2){ 
        $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current))); 
        $count++; 
    } 
    return $count; 
}
Community
  • 1
  • 1
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107