PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, December 31, 2021

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

 December 31, 2021     cakephp, cakephp-3.0, datetime     No comments   

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

  • Chronos API > \Cake\Chronos\ChronosInterface::setTimezone()


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing