![]() I had found my solution without having to do a huge infrastructure change. I found in ICU page a section about Timezones and the possibility to use the Z as suffix for expressing the timezone, when I tested the new pattern ("y-MM-dd’T’H:mm:ssZ") in both environments with both ICU versions it finally gave me the same result, the string was now: “2018–03–26T15:17:22+0000” which is totally acceptable with JS. This was the moment I had my second aha-moment. But, after seeing the amount of work it would take me and seeing that I would totally in charge of the security and configuration of the machine I preferred to take a second look at the code. Since I really like infrastructure I first thought of developing a custom environment for ElasticBeans Talk with ubuntu (since it has ICU lib in version 55.1). On the article I cited in the beginning of this article I discovered that different ICU library versions might give different outputs for formatting, that hit me like a ton of bricks (but also was my aha-moment).įrom there I finally knew my problem, I just had to solve it, and I really like to do this. When I decided to push the code the resulting date on the server was: “2018–03–26T15:17:22”, the difference is minimal, but we miss the Z there to say we are using UTC (or Zulu Timezone).Īfter more investigation and comparing phpinfo() outputs I found out that the only thing different between the server and my local machine was the ICU library versions, mine was 55.1 and the server was 51.2. I was happy, but software development is a box full of surprises. In my local machine it was giving me time strings like: "2018–03–26T15:17:22Z" which is totally acceptable by Date constructor on JavaScript. I resolved to isolate that behavior and tested a code that did not depend in CakePHP API whatsoever, I ended up with the following script: format($date->getTimestamp()) Here CakePHP is instantiating a new IntlDateFormatter, putting this instance in a in-memory cache and finally formatting the date object according with the configuration passed on the Time::setJsonEncodingFormat() method. So I started by investigating the code where the serialization was happening and found this on DateFormatTrait::_formatObject(): format($date->format( 'U')) What was the problem? Had to investigate. The pattern was: "y-MM-dd’T’H:mm:ssX", I made this pattern by looking on ICU documentation which is linked on the setJsonEncodeFormat() docs, but it was not giving the same results as my local. The final result of it was that my first pattern chosen for the dates were resulting in wrong formats, even though it was working on my local machine™️. For example, the ICU Data library, which is the backend used by Intl PHP extension, was in version 51 when the newest version is 57 (that is 6 major versions behind □). This being said, the packages on the machine were crazy behind in terms of version. The problem is that I chose to deploy our API using ElasticBeans Talk and the Linux running on the PHP platform is an Amazon Linux based of a RHEL distro. So there I was taking care of the API for an React app when my colleague called me so we could define an standard date format between ReactJS and CakePHP, then I thought, lucky me CakePHP offers me a great API of Time and Date so I can only call Time::setJsonEncodeFormat and I will be done. There are some nice articles that stress the complexity of handling Date serialization that is locale aware, here is one that was particularly helpful to me on this journey I am about to describe: A long journey to formatting a date internationally in PHP. CakePHP in its latest version is using Intl extension to handle Date and Time parsing and serialization among other things.īy using Intl they take away the need to have libraries built-in in the framework to do that kind of work. This week while building an ReactJS application I found an interesting behavior of Date and Time serialization in CakePHP. ![]() ![]() For a long version and description of great learning experience this was for me, read the full article □ Return Math.TLDR: When trying to create dates acceptable by Javascript in PHP, use the intl library and format dates with the following pattern: "y-MM-dd’T’HH:mm:ssZ"this will grant your application compatibility with older versions of ICU library while still being able to understand dates with timezone on JavaScript. ![]() 'July', 'August', 'September', 'October', 'November', 'December'], LongMonths: ['January', 'February', 'March', 'April', 'May', 'June', Array, This property allows you to configure the terms used for short/long days and months, the meridiem, and the ordinal suffix (for day of the month - works best with j).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |