PHP Function to return the number of days between two dates


function dateDiff($start, $end) {

$start_ts = strtotime($start);

$end_ts = strtotime($end);

$diff = $end_ts - $start_ts;

return round($diff / 86400);


echo dateDiff("2006-04-05", "2006-04-01");


[Edit] – Thanks Joel for pointing out the bug mentioned in the comments bellow 🙂


Software developer by day, scale model builder and wargamer by night.

Posted in Uncategorized
58 comments on “PHP Function to return the number of days between two dates
  1. David says:

    Awesome! I read so many sites that did this in 100+ lines of code… I expected it to be built in to PHP. Thanks Richard!

  2. Przemek says:

    Simple and work! Thanks!

  3. Claudiu says:

    Super function, thanks dude:)

  4. Joel says:

    Nice, clean code. Thanks!

  5. Joel says:

    Hi again. At first your script seemed perfect, but after some testing I found a bug. Your script doesn’t seem to know about March 31th.. Try to run this:

    echo dateDiff(“2008-03-29”, “2008-03-30”) . “”;
    echo dateDiff(“2008-03-29”, “2008-03-31”) . “”;
    echo dateDiff(“2008-03-29”, “2008-04-01”) . “”;
    echo dateDiff(“2008-03-29”, “2008-04-02”) . “”;

    I can’t figure out what’s wrong.

  6. Joel says:

    Sorry about spaming, but I think I found the problem and a solution =)

    For some reason the script doesn’t give an integer for some dates. Instead you get a number like this: 2.95833333333. When you use floor() you get 2 days instead of 3. When I tried round() instead everything seems to work.

    Thanks again for this script!

  7. Richard@Home says:

    Thanks for the update Joel. I’ll update the code to reflect your suggestion and give you a credit 🙂

  8. Jay says:

    Was looking for a quick snippet to do this… good job and thanks.

  9. Khangai says:

    hi. 1 question from me. i need to print everyday between 2 date. then what should i do?

  10. Lfcfan says:

    Is it possible to add some functionality to this to exclude weekend days? I’m developing a holiday request app so don’t want to include Saturday and Sunday in the calculation of the days requested. Thanks!

  11. Chris says:

    This is great, but I think there is a rounding problem. You need to do:

    return round(($diff / 86400) + 0.5);

    Otherwise things seem to be off by a day when it’s after noon.

  12. Sheeja says:

    Thanks . This is very useful

  13. George says:

    $dateDiff = round((strtotime(”2008-03-29″) – strtotime(“2008-03-30″))/ 86400);

  14. Rajan Pratap Singh says:

    Great function. It solve my one issue

  15. seo free says:

    Nice post…thanks lot

  16. Ben Crowther says:

    Any chance for help on leap years???

  17. Scott says:

    What do you need help with on leap years? Since you’re converting to Unix timestamps with strtotime(), it handles it internally

  18. hey man, thanx for the function, really usefull.

  19. SONJE says:

    Thanks for the function().. it really helps me on my project. But I need not to include the weekends or our dayoff(sunday and monday) on the calculation. I need your help guys.

  20. subikar says:

    Fantastic function thank you for sharing with us.

  21. Aziz says:

    nice effort.. keep it up.

  22. Daniel says:

    Nice clean code. Thank u.

  23. MeloWise says:

    Thanks for help. I have modified your code to give me the years weeks days hours and seconds between two dates and return the array. Now it will fit my project nicely.

    Thanks again,

  24. Novation says:

    Simple, clean and usefull 🙂


  25. septia says:

    awesome, thanks dude…

  26. ricky says:

    thanks!! this is so useful.

  27. Patrick says:

    The rounding problem occurs when your period includes a day on which daylight saving time starts or ends, in which case there’s either 23 or 25 hours in a day. To do this correctly you would have to convert the dates to a Julian calendar like this:

    function getDays($date1, $date2){
    $date1 = explode(‘-‘, $date1);
    $date2 = explode(‘-‘, $date2);
    $date1 = gregoriantojd($date1[1], $date1[2], $date1[0]);
    $date2 = gregoriantojd($date2[1], $date2[2], $date2[0]);
    return abs($date1 – $date2);

  28. munguis says:

    Thanks man! You’ve saved my day.

  29. shankar says:

    i used this function in my site

  30. wfjanjua says:

    Thanks. It’s easy

  31. Manny says:

    Finally, thank you!

  32. JamieLastStand says:

    Re: Lfcfan – I have added some functionality to exclude weekends:

    $startDate = '2011-01-14 07:00:00';
    $endDate = '2011-05-10 07:00:00';
    $diff = dateDiffExcludeWeekends($startDate, $endDate);
    echo "This is the difference excluding weekends: ".$diff;

    function dateDiffExcludeWeekends($startDate, $endDate) {

    // Sets the Count
    $count = 0;
    $startStt = strtotime($startDate);
    $endStt = strtotime($endDate);

    // iterates through each day till the end day is back at the start date
    while (date("Y-m-d", $startStt) <= date("Y-m-d",$endStt)){
    $count = (date("w", $endStt) != 0 && date("w", $endStt) != 6) ? $count +1 : $count;
    $endStt = $endStt - 86400;
    return $count;

  33. drmarx says:

    thanks millions to JamieLastStand.

  34. Developer says:


  35. you can also use this.

    function DateDifference($dateOfJoing)
    $currDate = time();
    $dateDiff = floor(( $currDate – strtotime( $dateOfJoing))/(60*60*24));
    return $dateDiff;

  36. r_y says:

    An avarage day does NOT have 60 * 60 * 24 seconds.

  37. safatullah says:

    Thanks…it works.

  38. Trav says:

    Thank you was trying to do the same thing but with about 10 lines of code. Cool

  39. Pseudo says:

    Thank you! Helped me a lot 🙂

  40. Hi good post

    I have used this inside a loop but only the first result in the loop is correct.

  41. says:

    * incorrect sorry

  42. Malix says:

    Thanks a lot!

  43. Jonathon morris says:

    This does not return the number of days between to dates. This returns number number of 24 hours between the two dates.

  44. Basavaraj says:

    thanx a lot,, i used this my project.
    good job

  45. your routine is wrong. it doesn’t account for the fact that every 4 years between the given dates one has to add an additional day.


    1967-12-31 to 2011-12-14 returns 16044 and it should be 16054.

    The Correct routine would be:


    function dateDiff($start, $end) {

    $start_ts = strtotime($start);

    $end_ts = strtotime($end);

    $diff = $end_ts – $start_ts;

    $start_array = split("-", $start);
    $start_year = $start_array[0];
    echo "start_year = $start_year “;

    $end_array = split(“-“, $end);
    $end_year = $end_array[0];
    echo “end_year = $end_year “;

    $years = $end_year-$start_year;

    if (($years%4) == 0) {
    $extra_days = ((($end_year-$start_year)/4)-1);
    } else {
    $extra_days = ((($end_year-$start_year)/4));

    $extra_days = round($extra_days);

    echo “extra_days = $extra_days “;

    return round($diff / 86400)+$extra_days;


    echo dateDiff(“1967-12-31”, “2011-12-04”);


    Would return 16054 which is the correct number, while your routine returns 16044 which is 10 days short.



  46. Sorry, let me get out the echos (I always like to see what’s happening :D)

    This gives back the correct number of days which is 16054. I have a customer that uses the PICK ERP which works internally with this format.

    Kudos for your routine however!



  47. Snow says:

    really Big thank to this ^^

  48. prakash says:

    why 86400 to round value

  49. Brian says:

    86,400 is the number of seconds in a day (60 * 60 * 24).

  50. Brian says:

    The original routine seems to work OK if one enters only dates (without any times) into the routine. However, if times are present in the strings with more than 12 hours difference between them then the difference is increased by 1 because of the round. Try:

    echo dateDiff(’24-Feb-2012 01:00′, ’24-Feb-2012 23:50′) . PHP_EOL;

    It returns 1. Because the interval between the two _timestamps_ is greater than 0.5 days the round() rounds it up to 1. If you are looking for the number of days between two _dates_ then any time component to the two input parameters needs to be stripped off.

    Here’s one solution to do that:

    function dateDiff($date1, $date2=”) {
    $d1 = strtotime($date1);
    $d1 = mktime(0, 0, 0, date(‘m’, $d1), date(‘d’, $d1), date(‘Y’, $d1));
    $d2 = strtotime($date2);
    $d2 = mktime(0, 0, 0, date(‘m’, $d2), date(‘d’, $d2), date(‘Y’, $d2));
    return round(($d2 – $d1) / 86400);

  51. Rumesh says:

    Thank you so much….
    helped to me lot…

  52. Steven says:

    function($startDate, $endDate){
    $datetime1 = new DateTime($startDate);
    $datetime2 = new DateTime($endDate);
    $interval = $datetime1->diff($datetime2);
    return $daysDiff = $interval->d;

  53. ton says:

    thx helped to me

  54. Mike says:

    Bravo! Well done sir, concise and very helpful.

  55. derp says:

    dude, the font on your blog is TOO BIG! it should be smaller!

  56. Iulian says:

    Clear, light, useful. Thanks

  57. sabarish says:

    i want know how to store the date between two dates in database.. in between days will be count and store to the database? how?

  58. Dennis says:

    should be floor, not round

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: