如何计算php中2个unix时间戳之间的间隔而不除以86400(60 * 60 * 24)

发布于 2024-09-24 07:28:19 字数 939 浏览 4 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜光 2024-10-01 07:28:19

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

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

您的结果始终是整数。

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

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

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.

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')
茶底世界 2024-10-01 07:28:19

正确的 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.

怎樣才叫好 2024-10-01 07:28:19

除以 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.

德意的啸 2024-10-01 07:28:19

人们应该使用 DateTime 库:

date_default_timezone_set('NZ');

$dateStart = DateTime::createFromFormat('U', '1233925200');
$dateEnd   = DateTime::createFromFormat('U', '1284985360');

$diff = $dateEnd->diff($dateStart);

记住:一天的长度并不(总是)86400 秒。

echo $diff->days;

One should use the DateTime library instead:

date_default_timezone_set('NZ');

$dateStart = DateTime::createFromFormat('U', '1233925200');
$dateEnd   = DateTime::createFromFormat('U', '1284985360');

$diff = $dateEnd->diff($dateStart);

Remember: the day is not (always) 86400 seconds long.

echo $diff->days;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文