se...">

PHP: Are timezone data embedded in Timestamps in some way?

date_default_timezone_set("Asia/Singapore"); // UTC +8
$dt = new DateTime();
$dt->setTimestamp(DateTime::createFromFormat('u', gmdate('u'))->getTimestamp());
echo $dt->getTimezone()->getName(); // Asia/Singapore
echo $dt->format('d M Y H:i:s'); // Correct local time

$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('d M Y H:i:s'); // Correct UTC Time
die;

I am wondering if the timestamps contain temporary data. On line 3, you see what I used gmdate(), which should give me UTC / GMT. But when I get the time zone and datetime format, they fall into local time. I have not set timezones in my object yet DateTime, I gave it a UTC timestamp. It somehow knew that it was necessary to switch to local time. This makes me wonder if Timezone data is included in timestamps.

+3
source share
2 answers

setTimestamptakes a timestamp in GMT 0, and you passed it in GMTbecause of gmdate('u'). The DateTimedefault object occupies your current time zone.

- , DateTime .

, Timezone

. unix-epoch.

+3

.

, .

, , .

, .

+1

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


All Articles