Handle date and time beautifully with PHP and display it correctly in your Web application.
Date and time is usually composed of year, month, day, hour, minute, second, and of course time zone. We will see in what follows how to create, modify, and display date and time correctly in the context of proper Internationalization (i18n) and Localization (l10n).
Notes:
- The techniques used by the developer to determine and apply the appropriate locale are outside the scope of this article.
- The exercise is left to the reader to try the code written on this page in order to witness the results obtained with the techniques shown.
Contents
Manipulating Date & Time
DateTime objects can be created without parameters to capture the current clock settings for your computer/server including the local timezone setting as follows:
$now = new DateTime();
The constructor may also be used to parse standard-formatted date-time strings as follows:
$now = new DateTime('2022-11-08 15:52 GMT-5');
We can convert the time from one timezone to another by setting a new timezone on the same object as follows:
$now->setTimeZone(new DateTimeZone('America/Edmonton'));
Refer to the Wikipedia List of tz database time zones page for a full listing of the timezone strings and their meanings. You may find a complete listing of PHP-supported timezone strings by running the following PHP command:
DateTimeZone::listIdentifiers();
Time can be added or subtracted from the DateTime object using the add and sub methods and new DateInterval objects as follows:
$now->add(new DateInterval('P1Y2M3D'));
$now->sub(new DateInterval('P1YT3H2M1S'));
In the example above, 1 year, 2 months, and 3 days are added to $now and then 1 year, 3 hours, 2 minutes, and 1 second are removed. Alternately, the DateTime::modify method may be used as follows to add 1 day and 3 seconds as well as to remove 1 hour:
$now->modify('+1 day -1 hour +3 seconds');
The date and time in this object can also be returned as a string to then be echoed to output as follows:
echo $now->format('l, F j, Y, H:i:s T');
This is great, however, the format method does not use locales. All output is in English. Refer to the PHP.net reference on DateTime, DateInterval, and DateTimeZone for any details that you may be wondering about.
In the following section we will see how we can simultaneously output the Date and Time information in a different locale (language) and a different timezone.
Using IntlDateFormatter to Output Localized Dates and Times
IntlDateFormatter is part of the Intl extension of PHP and must be loaded directly in the php.ini file. This is done by ensuring the “extension=intl” line is not commented (not prepended by “;”) as follows:
extension=intl
Once this type of change in the configuration is done, Apache must be restarted so that php runs with the fresh configuration.
Output of Formatted Date & Time
The IntlDateFormatter class, as the name implies, is used to correctly format dates, internalionalized. To be used, first build a new formatter object as follows:
$fmt = new IntlDateFormatter(
'en_US',//locale string
IntlDateFormatter::FULL,//date format
IntlDateFormatter::FULL,//time format
'America/Montreal',//timezone string
IntlDateFormatter::GREGORIAN//calendar type
);
Where
- the locale string may come from a $_SESSION[‘locale’] variable (set for the user)
- the date format can be set to any constant IntlDateFormatter::SHORT, MEDIUM, LONG, FULL, and other values giving results that are increasingly long and that you should test. See the documentation for full details.
- the time format can be set to any of the same constants as previously listed.
- the timezone string as supported by the DateTimeZone class and explained above. This string may come from a $_SESSION[‘timezone’] variable (set for the user).
- the calendar type will be either IntlDateFormatter::GREGORIAN or IntlDateFormatter::TRADITIONAL. Traditional, or non-Gregorian calendars need to be specified in the locale string. For example, “cn@calendar=CHINESE”.
An example using a chinese calendar is as follows:
$fmt = new IntlDateFormatter(
'cn@calendar=CHINESE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Asia/Hong_Kong',
IntlDateFormatter::TRADITIONAL
);
One may also specify a custom format string as an additional parameter, following the constants for ISO 8601 Date Output. For example,
$fmt = new IntlDateFormatter(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Montreal',
IntlDateFormatter::GREGORIAN,
'MMMM d, Y');
and
$fmt = new IntlDateFormatter(
'fr@calendar=CHINESE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Asia/Hong_Kong',
IntlDateFormatter::TRADITIONAL,
'MMMM d, Y');
The new, last, parameter is a string that should be subject to internationalization (i18n) and localization (l10n) procedures.
With our IntlDateFormatter object, we can format DateTime objects for output as defined in the formatter parameters. One command is required, as follows:
$now = new DateTime();//our current date and time
echo $fmt->format($now->getTimestamp());//output in format
Conclusion
To handle date and time beautifully with PHP is simple with the help of a few classes:
The techniques shown in this article are correct to complete the internationalization (i18n) of dates and times on a Web application. The formats used for output remain a development and possibly localisation (l10n) decision. The techniques used by the developer to determine and apply the appropriate locale were outside the scope of this article.

One reply on “Handle date and time beautifully with PHP”
Getting the timezone automatically from the Web browser with JavaScript can be implemented. See https://stackoverflow.com/questions/1905397/how-to-get-clients-timezone