Subtract Dates in PHP

This item was filled under [ PHP ]

PHP does not provide functions to calculate difference between two dates. Here is a simple function that does the basic job.

<?php

//date format: yyyy-mm-dd
$date_diff = subtract_dates("2007-11-5", "2007-11-20");
echo $date_diff . " days";

<em>//function to find difference between two dates</em>
function subtract_dates($begin_date, $end_date)
{
return round(((strtotime($end_date) - strtotime($begin_date)) / 86400));
}

?>

It can be further modified to provide difference in minutes or hours. For that purpose, simply change the value 86,400 to 60 or 3,600 respectively.

Rate this topic:
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4 out of 5)
Loading ... Loading ...
Popularity: 1,200 views
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

23 Comments on “Subtract Dates in PHP”

  • Abdul Basit
    23 November, 2007, 21:10

    …. hmmmm…. what abt finding in days,years,decade,centuries… :p

  • 23 November, 2007, 21:21

    >> echo $date_diff . ” days”;
    ..shows the data in days ;)
    And yea, divide by n*365*86400 (where n = # of years) and it will answer your “years,decade,centuries” :p

    Enjoy! (=

  • Abdul Basit
    23 November, 2007, 21:35

    thanks for the prompt reply….. it “helped” me so much…. i was trying for it like days.. wasnt able to figure it out…. thanks to you…:p:p:p:p

  • 24 November, 2007, 21:27

    hey this sounds cool, but mussu iss mein we have to define the dates pehlay se?? whats the dynamic part??

  • 24 November, 2007, 21:36

    The result is dependent upon the input that can be dynamic/static ;)

  • alex
    2 January, 2008, 8:36

    YOU ARE A GODSEND, been trying to write my own lengthy function to do this and it was off by about a week on large counts. this is precise. THANKS!

  • 2 January, 2008, 9:34

    Glad to hear that :D

  • 5 March, 2008, 19:06

    meehn, thanks a lot. been lookin for something like this since.. pity ur link was on page 3 google for my keyword.

    thanks

  • Shane
    14 March, 2008, 6:43

    There are 366 days in a leap year. Your function will be off a day if you span one leap year in your date range; even more days the more leap years you span. So your function is only good for time frames within 365 days of each other.

  • 14 March, 2008, 11:41

    Try..
    $date_diff = subtract_dates(”2007-11-5″, “2008-11-5″);
    //output: 366 days

    Where as,
    $date_diff = subtract_dates(”2006-11-5″, “2007-11-5″);
    //returns: 365 days

    The leap year case seems to be working fine.

  • Shane
    27 March, 2008, 8:11

    Em said this on November 23, 2007 at 9:21 pm
    “And yea, divide by n*365*86400 (where n = # of years) and it will answer your “years,decade,centuries” :p”

    Sorry, I was referring to the above; when you want to output into years. You can’t count on 365 days as a standard year or you’ll be off when you break it down into # years, # months, # days. After searching around, I found a great function that accounts for leap years on php.net/datetime by a user nate at example dot com. But for only breaking down into days, your function works just fine.

  • 27 March, 2008, 14:35

    I Agree. The above function would work only if one desire to find number of days. To calculate in years etc., we’ll have to look up for something else. :)
    Though I was only messing with my friend over the above statements! ;)

  • gobi
    9 September, 2008, 13:03

    this script is nice one… really useful for me..

  • 11 September, 2008, 10:42

    //date format: yyyy-mm-dd
    $date_diff = subtract_dates(”2008-08-07 19:40:30″, “2008-08-07 19:41:33″);
    if($date_diff > 3600){
    echo $date_diff/3600 . ” Hours”;
    }elseif($date_diff > 60){
    echo $date_diff/60 . ” Mins”;
    }else{
    echo $date_diff . ” Secs”;
    }

    //function to find difference between two dates
    function subtract_dates($begin_date, $end_date)
    {
    return round(((strtotime($end_date) - strtotime($begin_date)) / 1));
    }

    In the above..

    When less than 60 it works fine.. but when about 60..

    say the difference is 1Min 2Secs.. it should give me 1.02 Mins.. instead gives me.. 1.0333333333333 Mins.

    Sorry not using brains anything wrong here?

  • 11 September, 2008, 10:43

    *sorry not using MY brains.. forgot the My.. hope you don;t take it wrong :)

  • Em
    11 September, 2008, 11:03

    1.02 minute is actually 62 seconds. If you do, 62 DIV 60 - you will always get 1.0333 min.

    One way of doing this would be, 62 DIV 60, pick the whole number - this will be your minute part. Now 62 MOD 60, and you will get the actual seconds :)
    //echo $DIV-result . $MOD-result;

    Check it for > 3600 as well!

  • 11 September, 2008, 12:06

    Thanks EM, this does it…

    Here is the code… The below displays… the difference in

    Hours, Mins and Secs.

    //date format: yyyy-mm-dd
    $date_diff = subtract_dates("2008-08-07 19:40:00", "2008-08-07 23:49:59");
    if($date_diff > 3600){
    echo $hour = (int)($date_diff/3600) . " Hour/s ";
    echo (int)(($date_diff/60) - ($hour * 60)) . " Min/s ";
    echo $date_diff % 60 . " Sec/s";
    }elseif($date_diff > 60){
    echo (int)($date_diff/60) . " Min/s ";
    echo $date_diff % 60 . " Sec/s";
    }else{
    echo $date_diff . " Sec/s";
    }

    //function to find difference between two dates
    function subtract_dates($begin_date, $end_date)
    {
    return round(((strtotime($end_date) - strtotime($begin_date)) / 1));
    }

  • 11 September, 2008, 12:07

    Em.. can you please edit my comment website address… Its a typo.. should have been… http://bluecornea.com

    Thank you once again.

  • Em
    11 September, 2008, 12:29

    Great! (All URLs updated) ;)

  • joseph
    12 September, 2008, 12:40

    thanks, this works fine with me

  • 12 September, 2008, 23:27

    Thanks,

    Doing some SugarCRM customization and it’s working for me just fine :)

  • monica
    15 September, 2008, 13:42

    This is a good solution to the problem of finding two dates difference. It works good.

    Thanks…………

  • serene
    30 October, 2008, 15:20

    A very good solution for me , of all the codes i use this works well. thanks for posting

Leave a Comment