View Full Version : calculating the difference between two date&time

06-04-2007, 12:51 PM
I am facing a problem while getting the difference between two dates in PHP. i used the following code
$diff=date("Y-m-d",strtotime('01-01-2007') - strtotime('01-01-2008')) but it is returning wrong date. i tried with most of the functions provided with php but still i can't. also How it will possible if the date is having time also. please send us the code if anybody already knows.

06-04-2007, 05:02 PM
What you're doing isn't going to return a date, its going to return an amount of time.


06-05-2007, 06:47 AM
so what is the solution for that. i want the differnce between two date with time. pl help me to sort out this

06-05-2007, 08:47 AM
First, the format you are using is not valid for strtodate. You cannot use the hyphenated method directly with strtotime so instead use a slashed method: 01/01/2007.
Second, using the date() function on the return value will result in 1 year after unix epoch (January 1, 1970 00:00 OR December 31, 1969 11:59), which is December 31, 1970 for this example.
So, unless this is the result you want to use, your going to need to do some calculations:
60 seconds per minute
60 minutes per hour
24 hours per day
365.25 days per year
Total real days per year is 31 557 600 seconds.

Hope that helps you out!

Thought I would throw this in too:
32-bit systems don't linke negative times too much (especially windows) so you may want to consider using an absolute value for your difference calculations.