Subtract Dates in PHP
Posted by Em on 23 November, 2007
23 Comments
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.
Popularity: 1,200 views
23 Comments on “Subtract Dates in PHP”
…. hmmmm…. what abt finding in days,years,decade,centuries… :p
>> 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! (=
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
hey this sounds cool, but mussu iss mein we have to define the dates pehlay se?? whats the dynamic part??
The result is dependent upon the input that can be dynamic/static
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!
Glad to hear that
meehn, thanks a lot. been lookin for something like this since.. pity ur link was on page 3 google for my keyword.
thanks
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.
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.
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.
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!
this script is nice one… really useful for me..
//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?
*sorry not using MY brains.. forgot the My.. hope you don;t take it wrong
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!
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));
}
Em.. can you please edit my comment website address… Its a typo.. should have been… http://bluecornea.com
Thank you once again.
Great! (All URLs updated)
thanks, this works fine with me
Thanks,
Doing some SugarCRM customization and it’s working for me just fine
This is a good solution to the problem of finding two dates difference. It works good.
Thanks…………
A very good solution for me , of all the codes i use this works well. thanks for posting