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 :-)

About these ads

~ by Richard@Home on March 28, 2006.

57 Responses to “PHP Function to return the number of days between two dates”

  1. 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. Simple and work! Thanks!

  3. Super function, thanks dude:)

  4. Nice, clean code. Thanks!

  5. 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. 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. Thanks for the update Joel. I’ll update the code to reflect your suggestion and give you a credit :)

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

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

  10. 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. 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. Thanks . This is very useful

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

  14. Great function. It solve my one issue

  15. Nice post…thanks lot

  16. Any chance for help on leap years???

  17. 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. 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. Fantastic function thank you for sharing with us.

  21. nice effort.. keep it up.

  22. Nice clean code. Thank u.

  23. 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. Simple, clean and usefull :-)


  25. awesome, thanks dude…

  26. thanks!! this is so useful.

  27. 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. Thanks man! You’ve saved my day.

  29. i used this function in my site

  30. Thanks. It’s easy

  31. Finally, thank you!

  32. 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. thanks millions to JamieLastStand.

  34. Thanks!

  35. you can also use this.

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

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

  37. Thanks…it works.

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

  39. 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. * incorrect sorry

  42. Thanks a lot!

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

  44. 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. really Big thank to this ^^

  48. why 86400 to round value

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

  50. 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. Thank you so much….
    helped to me lot…

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

  53. thx helped to me

  54. Bravo! Well done sir, concise and very helpful.

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

  56. Clear, light, useful. Thanks

  57. 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?

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: