I used: build time between two dates .. and in my...">

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

I used:

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

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

But actually

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

display 01:00:00, not 00:00:00.

So we have

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

Everything is in order at the moment $diffis 5 hours, but if we indicate the date as follows:

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

will be "06:00:00".

So is something wrong with my php config or is this normal behavior for php function date?

+3
source share
4 answers

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

+5

, UTC. :

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

I'm not sure why, but it datedisplays the current hour in your example, first create a timestamp from your seconds, and it works. I will follow 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 the current time zone, therefore mktime, therefore, the effect is denied.

0
source

use mktime and min 1 for the hour @ $ diff

-1
source

Source: https://habr.com/ru/post/1779016/


All Articles