nyamsprod.com

Pas un seul de vos ancêtres n'est mort jeune. Ils ont tous copulé au moins une fois.

Billets mentionnant : date

Formatting a Date in PHP the right way

Attention: Les informations de ce billet sont susceptibles d'être obsolètes car vieux de plus 2 ans.

Warning: The information you are reading may be obsolete, this post was published more than 2 years ago.

Lately I’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.

Historically, in PHP people have been using a conjunction of strftime and setlocale to achieve this goal. like in the following example:

This example has many problems:

The string you have to give setlocale depends on the locale that exists on your platform. For instance on Ubuntu if I specify fr_FR instead of fr_FR.UTF8 the result may differ depending on the installed locales. You could come up with a detection function prior to use setlocale but then you add even more burden in the locale detection.

Another problem is that if you only want to convert one specific date and nothing else the code should then be:

As you can see, setlocale affect your environment so using it should not be the recommend way. So what is the alternative ?

Well since PHP5.5 the Intl extension introduce the IntlDateFormatter::formatObject static method. This method addresses all the previous usage limitations. here’s an example:

  • The method expects a DateTime object which means that the object timezone is taken into account. This was not the case with strftime.
  • The formatting string follows the UCI standard which make them understandable not only by PHP users.
  • You can specify the locale per function call so no more changing your environment inside your code.
  • The locale string is also platform independent. Which means that no extra prefix or requirement is needed to make this code works.

The only drawback that I could spot was that DateTimeImmutable also introduced in PHP5.5 is not yet supported by the method despite a bug for its support being filled since 2013.

To put it simple, if you need to format a Date in PHP please use the Intl extension whenever you can.

Dealing with date and time in modern PHP

Attention: Les informations de ce billet sont susceptibles d'être obsolètes car vieux de plus 2 ans.

Warning: The information you are reading may be obsolete, this post was published more than 2 years ago.

Any PHP application has to deal at some point with date and time and there are so many ways to get it wrong in PHP that I thought I would give some tips on how to properly handle them in modern PHP. My post will assume that you are using the currently stable release of PHP, which is at the time of writing PHP 5.6. That being said I’ll indicate for each advice the minimum PHP version required to start using it. (suite…)

Getting the first day of the previous month in PHP

Attention: Les informations de ce billet sont susceptibles d'être obsolètes car vieux de plus 2 ans.

Warning: The information you are reading may be obsolete, this post was published more than 2 years ago.

Today a colleague of mine came to me with a simple question.

How to get the first day of the previous month in PHP ?

My colleague was using the following code:

<?php
echo date('Y-m-01', strtotime('-1 MONTH')), PHP_EOL;

But the code would not work and would instead return the first day of the current month. This behavior is documented in the PHP documentation so after some quick thinking here’s the code we ended up using:

<?php
echo  date('Y-m-d', strtotime(date('Y-m-01').' -1 MONTH')), PHP_EOL;

We first detect the first day of the current month and then we substract 1 month.

Now to be completely honest, this code sucks but we are still going to use it because it will run on any PHP version. But If you are using a version of PHP>=5.3 you should know that the parsing algorithm has been improved. So next time you really need this information use the following code:

<?php
date_default_timezone_set('UTC'); //should be set in your script or in your php.ini
$date = new DateTime('FIRST DAY OF PREVIOUS MONTH');
echo $date->format('Y-m-d'), PHP_EOL;