的行为">

的行为

发布于 2024-10-06 04:27:44 字数 630 浏览 1 评论 0原文

我用 :

<?php 
echo date("H:i:s", $var_time_diff);
?>

来构建两个日期之间的时间..在我的脑海中它是
$var_time_diff = 36000 = 显示 10:00:00 10 小时

但实际上

<?php echo date("H:i:s", 0);?> 

显示 01:00:00 而不是 00:00:00

因此

$date_a = "18:15:04";
$date_b = "23:15:04";
$diff = strtotime($date_b) - strtotime($date_a);

,目前 $diff 是 5 小时,但如果我们像这样显示日期:

echo date("H:i:s", $diff);

它将是 "06:00:00"

那么我的 php 配置有问题还是 php 函数日期的正常行为?

I used :

<?php 
echo date("H:i:s", $var_time_diff);
?>

to construct a time between two dates.. and in my head it was
$var_time_diff = 36000 = display 10:00:00 for 10 hours.

But in fact

<?php echo date("H:i:s", 0);?> 

display 01:00:00 and not 00:00:00.

So we have

$date_a = "18:15:04";
$date_b = "23:15:04";
$diff = strtotime($date_b) - strtotime($date_a);

All is ok for the moment $diff is 5 hours but if we display date like this:

echo date("H:i:s", $diff);

it will be "06:00:00".

So something wrong with my php config or it's a normal behavior for php function date?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

︶葆Ⅱㄣ 2024-10-13 04:27:44

date() 函数使用您当前的时区。如果您想忽略配置的时区,请使用 date_default_timezone_set() 或使用 gmdate()

The date() function uses your current time zone. If you want to ignore your configured time zone, use date_default_timezone_set() or use gmdate().

勿忘初心 2024-10-13 04:27:44

您所在的时区不是 UTC。尝试:

<?php
date_default_timezone_set('UTC');
echo date("H:i:s",0) . "\n";

You're in some timezone other than UTC. Try:

<?php
date_default_timezone_set('UTC');
echo date("H:i:s",0) . "\n";
陌若浮生 2024-10-13 04:27:44

我不知道为什么,但是 date 在您的示例中输出当前小时,首先从秒中创建时间戳并且它可以工作。不过,我将继续关注这个问题以获得更深入的解释。

$date_a = "18:15:04";
$date_b = "23:15:04";
$diff = strtotime($date_b) - strtotime($date_a);
echo date("H:i:s", mktime(0,0,$diff));

编辑:啊,所以它会调整到你当前的时区,mktime也是如此,所以效果被否定了。

I'm not sure why, but date outputs the current hour in your example, make a timestamp from your seconds first and it works. I'll be following this question for a deeper explanation though.

$date_a = "18:15:04";
$date_b = "23:15:04";
$diff = strtotime($date_b) - strtotime($date_a);
echo date("H:i:s", mktime(0,0,$diff));

edit: ah, so it adjusts to your current timezone, so does mktime, so the effect is negated.

↙厌世 2024-10-13 04:27:44

使用 mktime,最小 1 表示小时 @ $diff

use mktime, and min 1 for hour @ $diff

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