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
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.
SONJE said this on January 13, 2010 at 6:31 am
Fantastic function thank you for sharing with us.
subikar said this on February 5, 2010 at 11:10 am
nice effort.. keep it up.
Aziz said this on March 7, 2010 at 9:19 am
Nice clean code. Thank u.
Daniel said this on March 23, 2010 at 7:26 pm
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,
MeloWise said this on April 11, 2010 at 6:45 pm
Simple, clean and usefull
Thanks!
Novation said this on April 19, 2010 at 9:24 am
awesome, thanks dude…
septia said this on May 3, 2010 at 7:29 am
thanks!! this is so useful.
ricky said this on May 17, 2010 at 6:16 am
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);
}
Patrick said this on May 21, 2010 at 3:53 pm
Thanks man! You’ve saved my day.
munguis said this on November 19, 2010 at 3:35 am
i used this function in my mybabynames.in site
shankar said this on January 4, 2011 at 3:01 am
Thanks. It’s easy
wfjanjua said this on February 6, 2011 at 8:56 am
Finally, thank you!
Manny said this on May 1, 2011 at 7:19 pm
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;
}
JamieLastStand said this on May 24, 2011 at 11:36 am
thanks millions to JamieLastStand.
drmarx said this on June 6, 2011 at 9:57 am
Thanks!
Developer said this on June 8, 2011 at 1:03 pm
you can also use this.
function DateDifference($dateOfJoing)
{
$currDate = time();
$dateDiff = floor(( $currDate – strtotime( $dateOfJoing))/(60*60*24));
return $dateDiff;
}
Satya Prakash Gupta said this on June 26, 2011 at 2:40 pm
An avarage day does NOT have 60 * 60 * 24 seconds.
r_y said this on July 13, 2011 at 10:29 am
Thanks…it works.
safatullah said this on July 28, 2011 at 5:42 am
Thank you was trying to do the same thing but with about 10 lines of code. Cool
Trav said this on August 9, 2011 at 9:04 pm
Thank you! Helped me a lot
Pseudo said this on August 14, 2011 at 3:52 pm
Hi good post
I have used this inside a loop but only the first result in the loop is correct.
jason.bruce88@googlemail.com said this on September 6, 2011 at 4:07 pm
* incorrect sorry
jason.bruce88@googlemail.com said this on September 6, 2011 at 6:25 pm
Thanks a lot!
Malix said this on November 22, 2011 at 12:44 pm
This does not return the number of days between to dates. This returns number number of 24 hours between the two dates.
Jonathon morris said this on December 13, 2011 at 2:34 pm
thanx a lot,, i used this my project.
good job
Basavaraj said this on December 14, 2011 at 5:19 am
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.
Example:
1967-12-31 to 2011-12-14 returns 16044 and it should be 16054.
The Correct routine would be:
<?php
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.
Regards,
JP
Jean Paul Lopez said this on December 14, 2011 at 10:40 am
Sorry, let me get out the echos (I always like to see what’s happening
)
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!
Regards,
JP
Jean Paul Lopez said this on December 14, 2011 at 10:48 am
really Big thank to this ^^
Snow said this on December 20, 2011 at 3:53 pm
why 86400 to round value
prakash said this on December 26, 2011 at 7:14 am
86,400 is the number of seconds in a day (60 * 60 * 24).
Brian said this on January 28, 2012 at 6:12 pm
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);
}
Brian said this on January 28, 2012 at 6:49 pm
Thank you so much….
helped to me lot…
Rumesh said this on February 17, 2012 at 12:00 pm
function($startDate, $endDate){
$datetime1 = new DateTime($startDate);
$datetime2 = new DateTime($endDate);
$interval = $datetime1->diff($datetime2);
return $daysDiff = $interval->d;
}
Steven said this on October 8, 2012 at 8:19 am
thx helped to me
ton said this on October 30, 2012 at 10:37 am
Bravo! Well done sir, concise and very helpful.
Mike said this on November 19, 2012 at 2:32 am
dude, the font on your blog is TOO BIG! it should be smaller!
derp said this on January 15, 2013 at 1:04 pm
Clear, light, useful. Thanks
Iulian said this on March 5, 2013 at 10:07 am