PHP: Calculate Real Differences Between Two Dates or Timestamps - Comment Page: 7
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...
Very nice function. This is useful when the PHP version < 5.3. Since version 5.3, PHP supports method Datetime::diff( $datetime1, $datetime2 ); that might be an easier solution.
Hi Rilwis nice to see you here,
Yes, this is old post and PHP’s own Datetime:diff is useful on PHP 5.3, but you still need to format output manually. And of course here is many custom modifications from original version… ;)
hi all,
I have a question to all,
How to calculate minutes “Operational Hours” within a period.
case example :
Date range : 2011-11-01 to 2011-11-30,
Non Operational Hours : between 22:00 to 08:00 week day (Mon to Frid)
Non Operational Days : Sat + Sun
Quest :
How to Calculate Total “Minutes” Operational Time ??
Please help,
thanks for your advice …
BP
Hi abang,
Is those Operational days always from Mon to Fri?
And is those hours fixed 08:00 – 22:00?
Heej JR, your script has helped me loads. Thanks thus far! I have a quick question, a problem I can’t simply solve by myself.
If I do:
echo dateDiff(1267074000, time(), 3);
it works fine.
But if I do:
echo dateDiff($setTime, $currentTime, 3);
it doesn’t work, even though holding the same results.
Is there anything I’m doing wrong?
Cheers and much thanks in advance.
Forgot to add: it gives me 42 years, xx days, etc. I can’t figure out why.
Hi Ben,
Could you echo your variables $setTime and $currentTime, just before deteDiff function and post output here, like:
Just pinched this for something too. Thanks a lot mate, works very well!
[…] recently came across a very simple solution detailed on the following site, PHP: Calculate Real Differences Between Two Dates or Timestamps. One of the best features about the code is that it does not rely on new PHP features such as the […]
Beginning of the function is hidden because of remarks in code :)
oops, now looks good, looks like something loading slowly
I hate to bring up a really old post but this has been extremely helpful with my current project. I am doing a project that calculates tenure. An employee can work full-time, or a percentage of time. For example if the employee worked between 01/01/01 and 01/01/02 at full-time I need it to have the result as 1 year but if the employee only worked, say 20 of 40 hours a week (50%), then I should get a result of 6 months. I feel that I should be able to modify the given code to include this but I am still learning PHP and I am having more trouble than I care to admit. Is there any way you or someone else could help me?
Thank you again.
Hi Jordan,
Do you want to get finally exact weeks, days, hours, minutes? And do you have always full months and years?
What I really need is just years months and days. between the two dates and then a total amount of days.
Do i always have full months and years? Yes the dates will always be with full months and years.
Hi Jordan,
It’s actually very easy to get just total amount of days between two timestamps with this function just change following line:
To:
And if you want years, months and days then change it to:
You can use this array as a parameter or use simple if…else structure to get it changed using parameter if you want use same function for both.
Hi JR..!
Thank’s for sharing this great script of yours, its very helpful!
You just saved me from being stuck for days..
Keep up the good work!
Thank you so much..!^__^
planetjane414
Very helpful, nice post Cheers :)
Hi JR, I able to set the code inside my system. The difference between 2 times display correctly in a table. But the total return the total hour only without ‘days or minute’.. Could you help me figure it out?
In a row, the time difference output is ok, but when I total it up outside the while loop it calculate the total difference but not presenting in a correct way, example.
row 1 = 46 hours, 32 minutes
row 2 = 32 hours, 28 minutes
Total = 78
TQ
Hi FM,
Could you post your code, how you calculate that total value?
Hi FM,
If you try to get sum of “46 hours, 32 minutes” + “32 hours, 28 minutes” then you get 78, but actually you should calculate total sum first without time formatting and then format time.
Hi JR. Thanks for the info. Yes I able to do that.
Hi FM,
You are welcome! Nice to hear that you got it working!
I was finding This type of Date Calculation function using PHP from a Long Time…Thanks A lot for this…
Using the first code the difference between 01/01/2013 and 31/12/2012 shows 43 years
Hi Rageesh.PK,
Date format 31/12/2012 is wrong. Following note from PHP strtotime manual:
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To avoid potential ambiguity, it’s best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
So you can use following:
So 31/12/2012 is not valid date here.
Hi, thanks for share, how can return total days number (2 months+3days = 63days)
thanks ;)
Hi paskuale,
You are welcome!
If you can use fixed values then just multiply months with 30 and years with 12 * 30 and so on. If you want exact values then you have to use timestamps / dates (from – to) and this function to get exact values.