I'm trying to create a date in this exact format:
2022-05-19T02:00:00.000 02:00
And I'm doing this:
$todayDate = new DateTime('19-05-2022', new DateTimeZone('Europe/Madrid'));
$todayDate->format(DateTime::ATOM),
But the output looks like this:
2022-05-19T00:00:00 02:00
There's a subtle difference:
2022-05-19T02:00:00.000 02:00 <-- wanted
2022-05-19T00:00:00 02:00 <-- the one I got
Any way I can get the one I need? What format would that be?
CodePudding user response:
As indicated by the documentation on DateTime
constants, you're looking for DateTimeInterface::RFC3339_EXTENDED
(example: 2005-08-15T15:52:01.000 00:00) rather than DateTimeInterface::ATOM
, also known as DateTimeInterface::RFC3339
(exemple: 2005-08-15T15:52:01 00:00).
CodePudding user response:
I assume the questioner wants to transfer a pure date ( with the time 00:00 ) to another time zone. He wants to get hours '02'.
If a very specific fixed format is needed, I don't recommend using predefined constants that may no longer exist or may be changed in a future PHP version.
$strDate = date_create('19-05-2022', new DateTimeZone('UTC'))
->setTimeZone(new DateTimeZone('Europe/Madrid'))
->format("Y-m-d\TH:i:s.vP")
;
echo $strDate;
//2022-05-19T02:00:00.000 02:00
Demo: https://3v4l.org/SBtd4