PHP Magic Book – Free PHP Scripts, Tutorials and Downloads, PHP AtoZ Reloaded, free php tutorials, free php downloads, php scripts, PHP tips

Date Difference In PHP – Find Date Difference

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
Tags: , ,
Posted in: Checks, Date & Time, Useful Scripts
Post's RSS » RSS 2.0
Post's Comments RSS » RSS 2.0

Related Posts




4 Responses to “Date Difference In PHP – Find Date Difference”

  1. Apr 18, 2009
    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. Apr 29, 2009
    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. May 2, 2009
    Lyyza Says:

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

  4. Dec 13, 2009
    Jake Says:

    @Lyyza

    LOL

Post a Comment

  Subscribe Via RSS
  Subscribe Via Email