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...
I really liked your blog! great
I was having the same accuracy issues caused by DST etc. This function has been a great help. Thanks!
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.
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:
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.
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.
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:
And then change default precisions from 6 to 5, like following:
Finally take care about intervals which is less than minute, like following:
Is this what you need? :)
Ya JR,
That method to remove the seconds works perfectly. Thanks again for this function. It’s really useful.
L3G3ND
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
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.
Hi Andy,
Did you set right timezone when you use this function?
I check out this case more specifically later.
// 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.
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 ?
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
@Andy
Could you post exact timestamps what you use with dateDiff function and Epoch Converter?
And what tool you use on Epoch Converter site?
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
Problem solved by entirely removing the following… I think it was somehow conflicting with the server settings.
// Set timezone
date_default_timezone_set(“UTC”);
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.