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

  • Share/Bookmark

, , ,

4 Reviews

  1. Lyyza says:

    where can i put this coding at the html form..
    i understand this coding but how can can implement this at html form

  2. King says:

    @Lyyza: Actually, not for html form. You need to put it in some PHP file to work. Just put the code and modify date1 and date2 variables.

  3. Lyyza says:

    can u give example where should i put that code in the php file.?

Leave a Review