Behavior of <?php echo date("H:i:s", 0);?>
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?
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()
.
You're in some timezone other than UTC. Try:
<?php
date_default_timezone_set('UTC');
echo date("H:i:s",0) . "\n";
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.
use mktime, and min 1 for hour @ $diff
精彩评论