{"id":2612,"date":"2016-06-15T12:17:06","date_gmt":"2016-06-15T10:17:06","guid":{"rendered":"http:\/\/nyamsprod.com\/blog\/?p=2612"},"modified":"2016-06-15T12:23:58","modified_gmt":"2016-06-15T10:23:58","slug":"formatting-a-date-in-php-the-right-way","status":"publish","type":"post","link":"https:\/\/nyamsprod.com\/blog\/formatting-a-date-in-php-the-right-way\/","title":{"rendered":"Formatting a Date in PHP the right way"},"content":{"rendered":"<div class=\"message warning\">\n<p><strong>Attention:<\/strong> Les informations de ce billet sont susceptibles d'&ecirc;tre obsol&egrave;tes car vieux de plus 2 ans.<\/p>\n<p><strong>Warning: <\/strong> The information you are reading may be obsolete, this post was published more than 2 years ago.<\/p>\n<\/div><p>Lately I&#8217;ve been doing some translation in PHP and one of the main issue was to translate a date into a human readable string according to the user settings.<\/p>\n<p>Historically, in PHP people have been using a conjunction of <code>strftime<\/code> and <code>setlocale<\/code> to achieve this goal. like in the following example:<\/p>\n<script src=\"https:\/\/gist.github.com\/cc5e9692284864a6a20345a47c4a536f.js?file=dateformatting-strftime.php\"><\/script>\n<p>This example has many problems:<\/p>\n<p>The string you have to give <code>setlocale<\/code> depends on the locale that exists on your platform. For instance on <em>Ubuntu<\/em> if I specify <code>fr_FR<\/code> instead of <code>fr_FR.UTF8<\/code> the result may differ depending on the installed locales. You could come up with a detection function prior to use <code>setlocale<\/code> but then you add even more burden in the locale detection.<\/p>\n<p>Another problem is that if you only want to convert one specific date and nothing else the code should then be:<\/p>\n<script src=\"https:\/\/gist.github.com\/cc5e9692284864a6a20345a47c4a536f.js?file=dateformatting-setlocale.php\"><\/script>\n<p>As you can see, <code>setlocale<\/code> affect your environment so using it should not be the recommend way. So what is the alternative ?<\/p>\n<p>Well since PHP5.5 the<a href=\"http:\/\/php.net\/manual\/en\/book.intl.php\" target=\"_blank\"> Intl extension<\/a> introduce the <a href=\"http:\/\/php.net\/intldateformatter.formatobject\" target=\"_blank\"><code>IntlDateFormatter::formatObject<\/code><\/a> static method. This method addresses all the previous usage limitations. here&#8217;s an example:<\/p>\n<script src=\"https:\/\/gist.github.com\/cc5e9692284864a6a20345a47c4a536f.js?file=dateformatting-intl.php\"><\/script>\n<ul>\n<li>The method expects a <code>DateTime<\/code> object which means that the object timezone is taken into account. This was not the case with <code>strftime<\/code>.<\/li>\n<li><a href=\"http:\/\/www.icu-project.org\/apiref\/icu4c\/classSimpleDateFormat.html#details\" target=\"_blank\">The formatting string follows the UCI standard<\/a> which make them understandable not only by PHP users.<\/li>\n<li>You can specify the locale per function call so no more changing your environment inside your code.<\/li>\n<li>The locale string is also platform independent. Which means that no extra prefix or requirement is needed to make this code works.<\/li>\n<\/ul>\n<p>The only drawback that I could spot was that <code>DateTimeImmutable<\/code> also introduced in PHP5.5 is not yet supported by the method despite <a href=\"https:\/\/bugs.php.net\/bug.php?id=65683\" target=\"_blank\">a bug for its support being filled since 2013<\/a>.<\/p>\n<p>To put it simple, if you need to format a Date in PHP please use the Intl extension whenever you can.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to format a Date  in PHP the right way using the Intl extension<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[770,771,802,803,806,412,804,805],"class_list":["post-2612","post","type-post","status-publish","format-standard","hentry","category-web","tag-date","tag-datetime","tag-formatting","tag-i18n","tag-intl-extension","tag-php","tag-setlocale","tag-strftime"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/comments?post=2612"}],"version-history":[{"count":5,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2612\/revisions"}],"predecessor-version":[{"id":2623,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2612\/revisions\/2623"}],"wp:attachment":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/media?parent=2612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/categories?post=2612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/tags?post=2612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}