Search code examples
phplaravelphp-carbon

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


Solution

  • 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.