gpt4 book ai didi

php - 日期在不同版本的 php 中返回不同的结果

转载 作者:可可西里 更新时间:2023-10-31 23:31:22 25 4
gpt4 key购买 nike

我有一个约会AB

我想得到它们之间的小时/分钟。喜欢:

date('h:i', strtotime(B) - strtotime(A));

但我得到了奇怪的结果:

echo date('h:i', strtotime('2014-01-01') - strtotime('2014-01-01'));
// echoes: 01:00 (!)


date_default_timezone_set('Europe/London');
$date = new DateTime();
$A = $date->format('Y-m-d H:i:s'); echo '<br />';
$date->modify("+64 minutes");
$B = $date->format('Y-m-d H:i:s'); echo '<br />';
echo date('h:i', strtotime($B) - strtotime($A));
// echoes 02:04 (!)

Live example对于之前的代码:

这是为什么,如何得到预期的结果?

最佳答案

这是正确的行为

为什么?想一想:strtotime('2014-01-01') - strtotime('2014-01-01') - 但date() 需要 timestamp 作为第二个参数。这意味着,您正在尝试从零点时间戳获取日期。而这一点在不同时区不同。您的 London TZ 有 +01 偏移量,这就是为什么 0 点时间戳是 1970 年 1 月 1 日 01:00:00 - 这就是为什么 date('h:i', 0)01:00

尝试设置,例如,Moscow 区域:

date_default_timezone_set('Europe/Moscow');
$date = new DateTime();
$A = $date->format('Y-m-d H:i:s');
$date->modify("+64 minutes");
$B = $date->format('Y-m-d H:i:s');

echo date('h:i', strtotime($B) - strtotime($A));//04:04

-您会准确看到 04:04 - 因为莫斯科的当前偏移量是 +03(所以 03 小时 + 64 分钟修改)

关于php - 日期在不同版本的 php 中返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21074050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com