sgb999
sgb999

Reputation: 79

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

Upvotes: 0

Views: 1403

Answers (2)

jspit
jspit

Reputation: 7703

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.

Upvotes: 0

mrhn
mrhn

Reputation: 18986

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.

Upvotes: 2

Related Questions