Date Difference In PHP – Find Date Difference

by Hiroshi on August 7, 2008

in Checks, Date & Time, Useful Scripts

We can find out the difference between two given dates in PHP very easily. Consider the following PHP script:

<?php
function dateDiff($dformat, $endDate, $beginDate)
{
           $date_parts1=explode($dformat, $beginDate);
           $date_parts2=explode($dformat, $endDate);
           $start_date=gregoriantojd($date_parts1[1], $date_parts1[0], $date_parts1[2]);
           $end_date=gregoriantojd($date_parts2[1], $date_parts2[0], $date_parts2[2]);
           return $end_date - $start_date;
}
// adjust date parts according to date format 102 for d-m-y and 012 for m-d-y
$date1="30-06-2008";
$date2="06-08-2008";
print "If we minus " . $date1 . " from " . $date2 . " we get " . dateDiff("-", $date2, $date1) . ".";
?>

Result will be
If we minus 30-06-2008 from 06-08-2008 we get 37

Download Script

Related Posts

Previous post:

Next post: