Difference between 2 Dates (yyyy-mm-dd)


How do I calculate the difference (in days) between 2 dates with PHP?

I’m storing dates in my db like so: 2010-07-27 and I’d like to compare this with todays date.

Many thanks for any pointers.

With a bit of a jiggery and a whole lotta pokery…

function dateDiff($dformat, $endDate, $beginDate)
    $date_parts1=explode($dformat, $beginDate);
    $date_parts2=explode($dformat, $endDate);
    $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
    $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
    return $end_date - $start_date;

// mm-dd-yyyy
$date2 = date("m-d-Y");
$date3 = dateDiff("-", $date2, $date1);

echo $date3;


If you are still compelled to do with PHP:

$diff = time() - strtotime("2010-07-01");
if($diff < 86400)
    echo 'Less than a day';
    echo (int)($diff / 86400) . ' days';

Check out the manual young man. (5.3 Only)


However, given that these dates are in your database, why not ask it for the number of days?

Fatal error: Call to undefined function date_diff() in.....


Maybe this will help. :wink:

SELECT DATEDIFF(NOW(), created_on) AS age FROM quote;

Failing that, there are non-5.3 solutions in the comments fella.

Oh wow, first example and there it is.
Slightly worried that 5.3 can’t compute, but here goes…