0

I'm having an odd problem getting timezones to show in the JSON results of Time/FrozenTime objects in CakePHP 3.5 after migrating our application to PHP 7.2 and to a new server environment.

A simplified example occurs when formatting a new Cake\I18n\Time like so:

(new Time())->i18nFormat("yyyy-MM-dd'T'HH:mm:ssxxx")

Previously this would return a string like so:

2020-01-31T10:57:43-07:00

However in our new environment, the same code fails to return the timezone portion, instead returning results like:

2020-01-31T10:57:43

Internally, tracing i18nFormat through to Cake\I18n\DateFormatTrait::_formatObject, they both seem to be calling the exact same parameters to datefmt_create:

static::$_formatters[$key] = datefmt_create(
    'America/Denver',
    0,
    0,
    'America/Denver',
    1,
    'yyyy-MM-dd\'T\'HH:mm:ssxxx');

But this seems to return different results, the "xxx" portion has no effect.

Is there some environment setting that would effect this? Or is it caused by something else?

How can I get the timezone portion back?

Andy Hoffner
  • 3,267
  • 2
  • 21
  • 22

1 Answers1

0

It looks like the default version for PHP servers in AWS Beanstalk, Amazon Linux/2.9.2, only natively supports ICU version 50.1.2-11.12 - which doesn't appear to support any of the "x" or "X" strings in Timezone formats.

I found a decent workaround for now - just use the older "Z" string instead, setting the default JSON format for Cake's Time & Date objects:

In config/bootstrap.php:

$customFormat = "yyyy-MM-dd'T'HH:mm:ssZ";
\Cake\I18n\Time::setJsonEncodeFormat($customFormat); // For any mutable DateTime
\Cake\I18n\FrozenTime::setJsonEncodeFormat($customFormat);  // For any immutable DateTime

This won't give identical results but it's still an accepted ISO8601 format that contains the timezone (ex. 2020-01-31T10:57:43-0700) and one supported as-is by javascript's Date class.

Andy Hoffner
  • 3,267
  • 2
  • 21
  • 22