简体   繁体   中英

Timezone php and angular format datetime

from php I write like this:

$date = new \DateTime('2019-11-15 23:00:00', new \DateTimeZone('UTC'));
$result = [
   'id'                    => $auction->getId(),
   'endDate'               => $date,
];

Now in the api I saw:

endDate : 2019-11-15T23:00:00+00:00

In my linux vps when I wrote date I get:

Fri Nov 15 20:27:50 UTC 2019

The problem is that on front I have a countdown, and on every endDate is adding 2 hours and I don't understand why.

Your front will diplay the date in YOUR time zone.

In you database or backend, the date is stored with the default time zone.

Here is an example:

 const date = '2019-11-15T23:00:00+00:00' diffWithYourTimeZone = new Date(date).getTimezoneOffset(); console.log(diffWithYourTimeZone) // You will probably see 120(min) or -120(min)

If you do console.log(new Date()) in your navigator, you will have something like this xxx xxx xx 2019 HH:MM:MiMi GMT+0200 (...) it means that you are in the zone +2H from GMT( Greenwich Mean Time ). When you store data in your backend, it's always as GMT + 0. But for your user in the front, you want to dsplay their date and time. This is why your navigator convert your date in GMT + 0 in the time in GMT + 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM