PHP Function Calculate difference between two dates

in

This PHP function calculate difference between two dates. The result in array format contain day, hour, minute and second.

<?php
function dateDiff($date1, $date2) {
        $s = strtotime($date2) - strtotime($date1);
        $d = intval($s/86400);
        $s -= $d*86400;
        $h = intval($s/3600);
        $s -= $h*3600;
        $m = intval($s/60);
        $s -= $m*60;
        return array("d"=>$d, "h"=>$h, "m"=>$m, "s"=>$s);
}
?>

Usage :

<?php
print_r(dateDiff("2004-02-25 13:24:00", "2004-03-03 21:52:03"));
?>

Output :

Array ( [d] => 7 [h] => 8 [m] => 28 [s] => 3 )

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.