行为的

时间:2020-12-08 01:25:19

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.

在两个日期之间构建一个时间。在我的头,是$var_time_diff = 36000 =显示10小时10小时。

But in fact

但事实上

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

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

显示01: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:

如果是5小时,一切都没问题,但如果我们像这样显示日期:

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

it will be "06:00:00".

这将是“06:00:00”。

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

我的php配置有问题吗?还是php函数的正常行为?

4 个解决方案

#1


5  

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().

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

#2


2  

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

你在UTC以外的某个时区。试一试:

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

#3


0  

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.

编辑:啊,所以它调整到你现在的时区,mktime,所以效果是否定的。

#4


-1  

use mktime, and min 1 for hour @ $diff

使用mktime, min 1小时@ $diff。

#1


5  

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().

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

#2


2  

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

你在UTC以外的某个时区。试一试:

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

#3


0  

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.

编辑:啊,所以它调整到你现在的时区,mktime,所以效果是否定的。

#4


-1  

use mktime, and min 1 for hour @ $diff

使用mktime, min 1小时@ $diff。