Friday, December 31, 2021

[FIXED] Display a FrozenTime object in ISO_8601 while converted to a timezone

Issue

I have a Cake\I18n\FrozenTime object that I need displayed both as 2020-09-11T04:15:44+00:00 and converted to a specific timezone.

Normally, I'd call ->format('c'), but that uses the UTC according to my app configuration.

I know I can convert to a timezone using ->i18nFormat('yyyy-MM-dd HH:mm', 'Europe/Copenhagen'), but then I'll lose the convenience of the c date format shorthand. IntlDateFormatter predefined constants are lacking, and those from DateTimeInterface, (specifically, \DateTime::ATOM) don't work.

So before I go ahead and reinvent the wheel with ->i18nFormat("yyyy-MM-dd'T'HH:mm:ssxxx", 'Europe/Copenhagen'), is there a better way to display a Cake\I18n\FrozenTime in a specific date format and a specific timezone?


Solution

If you want to keep a date object, you can simply apply the timezone conversion on the object.

Frozen* objects are immutable, so you'll end up with a new object when applying the conversion:

echo $obj->setTimezone('Europe/Copenhagen')->format('c')

See also



Answered By - ndm

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.