# PHP: Calculate Real Differences Between Two Dates or Timestamps - Comment Page: 1

I was using simple function to calculate difference between two dates and timestamps until I noticed, it's not working correctly in long intervals. It's very easy to calculate difference between two timestamps in seconds, but it's much more complicated print difference in human readable format. The Internet can be found in a wide range of ways to do this thing, but as a rule they use a fixed amount of seconds for the year and the month. So if we calculate year with using 365 or 365.25 days and month using 30 or 31 then the difference is not accurate, because of leap years, DST (Daylight Saving Time) and so on. Because of this problem, I decided to make a function (at least in the short...

## 226 comments on “PHP: Calculate Real Differences Between Two Dates or Timestamps - Comment Page: 1”

1 2 3 10
1. I really liked your blog! great

Reply
2. I was having the same accuracy issues caused by DST etc. This function has been a great help. Thanks!

Reply
3. Finally someone has done a “date diff” right!

Although I wish that under 24 hours it would have been displayed as “XX:YY:ZZ” instead.

Added from other comments:
BTW, IE6 users canâ€™t use this comment form.

BTW, this comment form requires cookies without saying so on errors.

Reply
• Hi LWC,

Nice to hear that you like my way to do real date diff. :)

Simple way to get “hours:minutes:seconds” format work for seconds if difference is less than 24 hours is following:

``````
// If time1 is bigger than time2
// Then swap time1 and time2
if (\$time1 > \$time2) {
\$ttime = \$time1;
\$time1 = \$time2;
\$time2 = \$ttime;
}

// Add the following here
\$seconds = \$time2-\$time1;
if (\$seconds < 86400) {
\$hours = floor(\$seconds / 3600);
\$seconds = \$seconds % 3600;
\$minutes = floor (\$seconds / 60);
\$seconds = \$seconds % 60;

return sprintf("%02d:%02d:%02d", \$hours, \$minutes, \$seconds);
}

// Set up intervals and diffs arrays
\$intervals = array('year','month','day','hour','minute','second');
\$diffs = array();
``````

I think that comment form is working, but because of spam comments are waiting for moderation, if the user has never before written anything on this blog.

Reply
4. Cheer’s thanks for this function. It really helped.

Just a quick question though. How would I edit it to remove the seconds? I’m doing something like Last Post was made 2 hours, 5 minutes ago and don’t really want the seconds showing.

It’s working fine and I’m using something like
``` echo dateDiff("now", \$posttime . "ago"; ```

Again cheers for this. Even if you don’t reply it’s really useful and exactly what I was looking for.

Reply
• Hi L3G3ND,

Nice to hear that this function helped you. :)

Complete removal of the seconds is easy, just remove second from the intervals array, like following:

``````
\$intervals = array('year','month','day','hour','minute');
``````

And then change default precisions from 6 to 5, like following:

``````
function dateDiff(\$time1, \$time2, \$precision = 5) {
``````

Finally take care about intervals which is less than minute, like following:

``````
// If time1 is bigger than time2
// Then swap time1 and time2
if (\$time1 > \$time2) {
\$ttime = \$time1;
\$time1 = \$time2;
\$time2 = \$ttime;
}

// Add the following here
\$seconds = \$time2-\$time1;
if (\$seconds < 60) {
return "Less than minute";
}
``````

Is this what you need? :)

Reply
5. Ya JR,

That method to remove the seconds works perfectly. Thanks again for this function. It’s really useful.

L3G3ND

Reply
6. http://www.myu2sig.com/siteage.php

echo dateDiff(1267125840, “now”, 4) . “\n”;

That’s how I’m using it… but it doesn’t appear to be quite right. As of this posing it should be displaying 93 Days, 12 Hours but it’s an hour off. It’s showing 93 Days 11 Hours

http://www.epochconverter.com/

This is what the converter says since I’m EST / EDT: Your timezone: Thursday, February 25, 2010 2:24:00 PM

Anyway, like I said… it’s an hour behind and I dont know what do do.

For simplicity sake, when it hits 2:24pm on the nose today it should show 94 Days but it’s going to show 93 Days, 23 Hours

Reply
7. Thought I’d add that if I’m right it’s not counting for the DST +1 we just had a while ago or something… I don’t know.

Reply
8. Hi Andy,

Did you set right timezone when you use this function?

I check out this case more specifically later.

Reply
9. // Set timezone
date_default_timezone_set(“GMT”);

I used the dsame time zone that the epox converter works with which displays the right time for me.

Reply
10. My concern is keeping it as accurate as possible. I mean sure I can change the TZ or agg +3600 to get it accurate but regardless of what I do wont I have to change something every year when time changes or whatever ?

Reply
11. No need to really toss this comment in there but I want it to be like that Ron Popeil (sp?) thing, just set it and forget it lol

Reply
12. @Andy

Could you post exact timestamps what you use with dateDiff function and Epoch Converter?

And what tool you use on Epoch Converter site?

Reply
13. Timestamp & dateDiff: echo dateDiff(1267125840, time(), 4) . “\n”;

I’m using what turns the timestamp in to human time.

So in my case which I showed before, 1267125840 translates into :

GMT: Thu, 25 Feb 2010 19:24:00 UTC
Your (Me) timezone: Thursday, February 25, 2010 2:24:00 PM

Reply
14. Problem solved by entirely removing the following… I think it was somehow conflicting with the server settings.

// Set timezone
date_default_timezone_set(“UTC”);

Reply
15. I wanted to add that if I’m right I would suggest to remove it from the above “demo” since most people have the right TZ set o ntheir server or atleast adding a note about possible conflictions with server TZ settings.

Reply
1 2 3 10

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close