简体   繁体   中英

Convert datetime to include utc timezone php

I am trying to convert datetime to a UTC timecode using PHP, how do I convert 2022-05-14 13:30:30 so it will come out as 2022-05-14T13:30:30+01:00

Thanks

The format can be obtained with the method toAtomString() . With my timezone settings, i got the timezone 02:00 , so i had to use setTimezone() and specifying the timezone on the parsing. Can be left out, if yours is correct.

Carbon::parse('2022-05-14 13:30:30', 'Europe/london')
    ->setTimezone('Europe/london')
    ->toAtomString();

This will format the date as follows 2022-05-14T13:30:30+01:00 .

Simple solution with DateTime:

$date = date_create('2022-05-14 13:30:30', new DateTimeZone('Europe/London'))
  ->format(DateTimeInterface::ATOM);

echo $date;  //2022-05-14T13:30:30+01:00

Try 3v4l.org .

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