如何计算php中2个unix时间戳之间的间隔而不除以86400(60 * 60 * 24)
我有 2 个 unix 时间戳,我位于亚太/奥克兰时区(GMT+12,夏令时 = GMT+13)
我想计算 2 个时间戳之间的天数间隔,其中一个在夏令时内,另一个不在夏令时内。
我的示例日期是:
2009 年 2 月 7 日 (1233925200)
到 2010 年 9 月 21 日 (1284985360)
(不包括 21 日) 看到这里它说 591 天: http://www.timeanddate.com/date/durationresult.html?d1=7&m1=2&y1=2009&d2=21&m2=9&y2=2010
让我们计算一下,这是我的时间戳(均基于奥克兰 00:00 时间)
1284985360-1233925200 = 51060160
51060160 / 86400 = 590.974
所以是的,我需要 591。我不想使用“向上舍入”解决方案
有没有像 strtotime 这样可靠的方法,但用于计算日期间隔,最好不需要 php 5.3+ 最低
编辑:需要澄清,我使用 STRTOTIME 来获取这些时间戳,我认为那是 UTC
编辑2:我相信我已经找到了问题。虽然我的结束日期是 9 月 21 日,但我实际上使用 time() 来获取结束日期,而 time() 返回了错误的时间戳,也许它没有考虑 GMT+12,无论我将 time() 切换为strtotime(date('d M Y')) 它返回了正确的时间戳!尤里卡 591 天
I have 2 unix timestamps, I'm in AsiaPacific/Auckland timezone (GMT+12, DaylightSavings = GMT+13)
I want to calculate the number of days interval between 2 timestamps, where one is inside daylight savings time and one is not.
My example dates are:
7 Feb 2009 (1233925200)
to 21 September 2010 (1284985360)
(not including 21st)
see here it says 591 days: http://www.timeanddate.com/date/durationresult.html?d1=7&m1=2&y1=2009&d2=21&m2=9&y2=2010
Let's calculate, here are my timestamps (both are based on Auckland 00:00 time)
1284985360-1233925200 = 51060160
51060160 / 86400 = 590.974
So yea I need 591. I don't want to use the "round up" solution
Is there any reliable method like strtotime, but for calculating date intervals, preferably that don't need php 5.3+ minimum
EDIT: need to clarify, I'm using STRTOTIME to get these timestamps, I thought that was UTC
EDIT2: I believe I have found the issue. While my end date was 21 September, I was actually using time() to get my end date, and time() was returning the wrong timestamp, perhaps it doesn't account for the GMT+12, regardless I switched time() to strtotime(date('d M Y')) and it returned the correct timestamp! eureka 591 days
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在计算差异之前计算两个时间戳的完整天数:
您的结果始终是整数。
由于您使用
strtotime
来获取这些时间戳,因此请指定时间00:00:00+0000
以始终获取 86400 的倍数:Calculate the number of full days for both timestamps before calculating the difference:
The your result is always an integer.
And since you’re using
strtotime
to get these timestamps, specify the time00:00:00+0000
to always get a multiple of 86400:正确的 Unix (POSIX) 时间戳采用 UTC,因此您从已经错误的值开始。您面临着一场艰苦的战斗,因为 API 通常会假设时间戳采用 UTC。最好解决这个问题,之后简单的除法实际上会给出正确的结果。
A correct Unix (POSIX) timestamp is in UTC, so you're starting out with values that are already wrong. You're facing an uphill battle, since APIs will generally assume that timestamps are in UTC. It would be best to fix this, after which the simple division would actually give the correct result.
除以 86400 既可靠又简单。我不知道任何特殊功能,也看不出它们存在的理由。
Dividing by 86400 is reliable and easy. I don't know any special functions and i don't see a reason for them to exist.
人们应该使用 DateTime 库:
记住:一天的长度并不(总是)86400 秒。
One should use the DateTime library instead:
Remember: the day is not (always) 86400 seconds long.