You might need to calculate the date difference in days using PHP. You can easily calculate the days difference by using something like the code below.
The code shown above takes the UNIX timestamp of two dates and subtracts the older date from the newer date to get the number of seconds. Then we simply divide the seconds by 60 (since there are 60 seconds in a minute) to get the minutes, again by 60 (since there are 60 minutes in an hour) to get the hours and lastly by 24 (since there are 24 hours in a day) to get the number of days.
You don’t have to subtract the newer date from the older date though since we’re making use of the php abs() function which will automatically return an absolute value and eliminate a minus sign to make a negative value positive.
I hope that helps!







August 11th, 2008 at 5:03 am
Really helpful!!!!!!!!!
September 9th, 2008 at 5:46 am
Indeed.
I was working on an old site that only had version 4.0 of MySQL installed so no DATEDIFF command.
Your code above allowed me to do the calculation almost as easily in PHP.
November 22nd, 2008 at 10:31 am
ohh dear..itz so easy…just tell me how to get end date from starting date and number of days + time , and also eliminate the weekends..
January 16th, 2009 at 5:37 am
This technique is daylight saving proof, because the one hour is everytime rounded correctly.
March 2nd, 2009 at 5:06 am
how can i create a form that store data that is fed on it to the database and can calculate the date from which a customer logs in for booking in a hotel and on the day when he/she checks out it automatically calculates the days spent and amount to be paid and declares the room vacant
March 13th, 2009 at 12:56 pm
Can you show the code for the calculation of the date difference? Thank you.
May 26th, 2009 at 9:06 am
where is the code?
May 29th, 2009 at 3:04 am
where the hell is the coooode ?
July 8th, 2009 at 1:17 am
where is the code plssssssssssss
July 25th, 2009 at 5:53 pm
uhm, I don’t see any code…?!
August 2nd, 2009 at 12:22 am
Good thinking, this is way easier then the calculation in PHP itself…
August 19th, 2009 at 4:59 am
reeeeeeeeeee there no code
August 19th, 2009 at 5:00 am
conde is not there
December 7th, 2009 at 11:21 pm
This plays is a joke. No friggin code
January 6th, 2010 at 9:21 pm
I found your article on the following page, which contains a number of references for Php Date Time calculations.
Calculate the difference between two Dates (and time) using Php. The following page provides a range of different methods (7 in total) for performing date / time calculations using Php, to determine the difference in time (hours, munites), days, months or years between two dates.
See Php Date Time – 7 Methods to Calculate the Difference between 2 dates.
January 27th, 2010 at 3:51 am
are you jocking with others……………..where is the code……………………..
February 11th, 2010 at 1:07 pm
That is the lightest script I have ever seen.. or not seen…
February 17th, 2010 at 12:49 pm
Are you people that lazy?! Look up mktime() and use simple arithmetic. Stop copy/pasting code and learn the shit yourself. There’s a reason why APIs and documentation exist.
February 22nd, 2010 at 5:20 am
I think he forgot to insert the code. Truly he must be “lost in code”
March 13th, 2010 at 12:31 am
conde is not there
March 13th, 2010 at 12:32 am
That is the lightest script I have ever seen.. or not seen
March 13th, 2010 at 12:33 am
That is the lightest script I have ever seen.. or not seenThat is the lightest script I have ever seen.. or not seenThat is the lightest script I have ever seen.. or not seen
March 18th, 2010 at 1:02 am
Hey,
Where is ur code?
March 18th, 2010 at 5:29 am
Here the simple PHP code to get the date difference in days.It subtract the given date from current date and returns the different in days.
function nicetime($date)
{
if(empty($date)) {
return “No date provided”;
}
$periods = array(“second”, “minute”, “hour”, “day”, “week”, “month”, “year”, “decade”);
$lengths = array(“60″,”60″,”24″,”7″,”4.35″,”12″,”10″);
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return “Bad date”;
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now – $unix_date;
} else {
$difference = $unix_date – $now;
}
return floor($difference/(60*60*24));
}
$date = “2010-03-09″;
$result = nicetime($date); // 2 days ago
echo $result;
April 28th, 2010 at 7:34 am
$dateDiff = strtotime($totdate) – strtotime($frmtdate);
$fullDays = floor($dateDiff/(60*60*24));
echo ‘The difference between these dates is’.$fullDays;
July 8th, 2010 at 10:18 am
can’t find the code…
anyways, to get the difference in days excluding time, do this
$days = abs(strtotime(date(‘Y-m-d’,$timeTo)) – strtotime(date(‘Y-m-d’, $timeFrom)));