PHP Function to return the number of days between two dates
<?php
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

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!
David said this on May 25, 2007 at 6:34 am
Simple and work! Thanks!
Przemek said this on March 27, 2008 at 10:14 am
Super function, thanks dude:)
Claudiu said this on April 10, 2008 at 5:36 am
Nice, clean code. Thanks!
Joel said this on April 11, 2008 at 7:50 am
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.
Joel said this on April 11, 2008 at 10:46 am
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!
Joel said this on April 11, 2008 at 10:59 am
Thanks for the update Joel. I’ll update the code to reflect your suggestion and give you a credit
Richard@Home said this on April 11, 2008 at 1:58 pm
Was looking for a quick snippet to do this… good job and thanks.
Jay said this on September 13, 2008 at 7:51 pm
hi. 1 question from me. i need to print everyday between 2 date. then what should i do?
Khangai said this on November 7, 2008 at 6:25 am
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!
Lfcfan said this on November 12, 2008 at 11:28 am
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.
Chris said this on February 11, 2009 at 6:51 pm
Thanks . This is very useful
Sheeja said this on March 19, 2009 at 6:55 am
$dateDiff = round((strtotime(”2008-03-29″) – strtotime(“2008-03-30″))/ 86400);
George said this on March 23, 2009 at 3:52 pm
Great function. It solve my one issue
Rajan Pratap Singh said this on April 8, 2009 at 4:45 am
Nice post…thanks lot
seo free said this on May 13, 2009 at 2:42 pm
Any chance for help on leap years???
Ben Crowther said this on July 14, 2009 at 3:22 pm
What do you need help with on leap years? Since you’re converting to Unix timestamps with strtotime(), it handles it internally
Scott said this on August 2, 2009 at 7:24 am
hey man, thanx for the function, really usefull.
Desarrollo Web said this on September 8, 2009 at 8:12 pm