如何计算php中2个unix时间戳之间的间隔而不除以86400(60*60*24) [英] How to calculate the interval between 2 unix timestamps in php WITHOUT dividing by 86400 (60*60*24)

查看:55
本文介绍了如何计算php中2个unix时间戳之间的间隔而不除以86400(60*60*24)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个 unix 时间戳,我在亚太/奥克兰时区(GMT+12,DaylightSavings = GMT+13)

I have 2 unix timestamps, I'm in AsiaPacific/Auckland timezone (GMT+12, DaylightSavings = GMT+13)

我想计算 2 个时间戳之间的天数间隔,其中一个在夏令时内,一个不在.

I want to calculate the number of days interval between 2 timestamps, where one is inside daylight savings time and one is not.

我的示例日期是:

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

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

让我们计算一下,这是我的时间戳(均基于奥克兰 00:00 时间)

Let's calculate, here are my timestamps (both are based on Auckland 00:00 time)

1284985360-1233925200 = 51060160
51060160 / 86400 = 590.974

所以是的,我需要 591.我不想使用四舍五入"解决方案

So yea I need 591. I don't want to use the "round up" solution

有没有像strtotime这样可靠的方法,但是为了计算日期间隔,最好不需要php 5.3+最低

Is there any reliable method like strtotime, but for calculating date intervals, preferably that don't need php 5.3+ minimum

需要澄清一下,我正在使用 STRTOTIME 来获取这些时间戳,我认为那是 UTC

need to clarify, I'm using STRTOTIME to get these timestamps, I thought that was UTC

我相信我已经发现了问题.虽然我的结束日期是 9 月 21 日,但我实际上是使用 time() 来获取我的结束日期,而 time() 返回了错误的时间戳,也许它没有考虑 GMT+12,无论我将 time() 切换到strtotime(date('d M Y')) 并返回正确的时间戳!尤里卡591天

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

推荐答案

在计算差异之前先计算两个时间戳的全天数:

Calculate the number of full days for both timestamps before calculating the difference:

floor(1284985360 / 86400) - floor(1233925200 / 86400)

你的结果总是一个整数.

The your result is always an integer.

并且由于您使用 strtotime 来获取这些时间戳,请指定时间 00:00:00+0000 以始终获得 86400 的倍数:

And since you’re using strtotime to get these timestamps, specify the time 00:00:00+0000 to always get a multiple of 86400:

strtotime($str.' 00:00:00+0000')

这篇关于如何计算php中2个unix时间戳之间的间隔而不除以86400(60*60*24)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆