Travelling through Date and Time, part IV

Welcome to the fourth installment of my series about date and time in PHP. Let’s review what we’ve done so far: In the first part, we looked at the basics (DateTime object and friends). Then, we applied the theory and discussed the basic outline for an application in FLOW3. Finally we dealt with the until-then omitted timezones. Still, there is one important thing that is missing. It could be summarized by the following question: What does 07/09/08 mean? Well, yes, it’s a date, but it could be a very different date depending how you interpret it. Personally, I’m always confused when I come across a date formatted differently to what I’m used to … Which leads us to today’s topic: We need localization in order to make it easy for everyone to understand the dates in our application.

How do we do this? Unfortunately, this time it’s not that clear. Basically, you have two options to consider: strftime and IntlDateFormatter. We’ll have a look at both and will see advantages and disadvantages of both approaches. But, first and foremost, they have one thing in common: They don’t work with DateTime. What the … ? Yes. But at least they both work with timestamps ;)

Let’s start with IntlDateFormatter. This class was introduced in PHP 5.3 and is only available if the PECL extension intl is installed. To use it, you need to define a format object first, like this:
$enUSFormat = new IntlDateFormatter('en_US', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'America/New_York', IntlDateFormatter::GREGORIAN);
To find out more about the options, please refer to the docs.
Finally, you can use this format object to get a localized output of a timestamp like so:
If you want to have more/full control about the format used, you can also specify a pattern at the end:
$enUSFormat = new IntlDateFormatter('en_US', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'America/New_York', IntlDateFormatter::GREGORIAN, 'MM/dd/yyyy');
Possible patterns can be found on the ICU website.

The other option is to go with strftime. It’s the traditional way of doing things, and, in contrast to IntlDateFormatter, it is locale aware. That means that the output depends on the locale setting, which can be changed like this:
setlocale(LC_TIME, 'en_US');
Note that either LC_ALL (locale setting for all localization areas) or LC_TIME (only for date/time formatting) works fine here.
Then, you can use strftime like this:
strftime('%c', $myDateTime->getTimestamp());
%c is just one of the formatting options you have. There are a lot more available which offer possibilities similar to IntlDateFormatter’s pattern string. Again, for more information, please refer to the docs.

To conclude, which option should be used? I’d opt for strftime. Mainly because it is available on all PHP setups (a big point in my opinion), and also because it is easier to use in conjunction with setlocale. The disadvantage of strftime is that it uses a different format string than date() or DateTime::format() in contrast to IntlDateFormatter.

With this, I’m going to end my journey through date and time in PHP. I hope you enjoyed it!

filed under , posted on April 24, 2010

Comments are closed.