When working with dates in a PHP/MySQL environment, they typically come in two formats:
- Y-m-d H:i:s in the database (the DATETIME format)
- d/m/Y in HTML, for display purposes
To change the format of a date, you generally go through an intermediate step: the timestamp (Unix time, or Posix time). The timestamp represents the number of seconds elapsed since January 1, 1970. You use the strtotime function to convert a date to a timestamp, then the date function to convert the timestamp to the desired format.
<?php
$date = "2020-07-09 13:30:15";
echo date("d/m/Y", strtotime($date));
// Result: 09/07/2020
?>The problem with this method is that you are limited in your input format. In Europe, we use dates in the d/m/Y format (day/month/year), while the strtotime function expects dates in the American m/d/Y format (month/day/year).
- If the separator is a hyphen (-), the date is considered European
- If the separator is a slash (/), the date is considered American
Taking our example again, but simply changing the separator, nothing works anymore:
<?php
$date = "09/07/2020 13:30:15";
echo date("d/m/Y", strtotime($date));
// Result: 07/09/2020 !!!
?>[Warning] Since PHP 8.1,
strftimeis deprecated.DateTime::format()provides a locale-independent replacement.IntlDateFormatter::format()provides a locale-aware replacement.
Example:
<?php
$date = "09/07/2020 13:30:15";
$dateTime = DateTime::createFromFormat("d/m/Y H:i:s", $date);
echo $dateTime->format("d/m/Y");
// Result: 09/07/2020
?>The solution to this problem lies in the DateTime class. This class also allows you to work with dates, with the difference that you can specify the format of the input date. Dates will no longer be limited to the American format.
<?php
class DateUtils {
public static function formatDate($date, $inputFormat, $outputFormat) {
$dateTime = DateTime::createFromFormat($inputFormat, $date);
return $dateTime->format($outputFormat);
}
}
echo DateUtils::formatDate("09/07/2020 13:30:15", "d/m/Y H:i:s", "d/m/Y");
?>Display today’s date in a localized format with PHP
To display today’s date in a localized format (e.g., French, Italian, English) with PHP, you need to:
- Set the locale information with the
setlocalefunction. - Set the default timezone with the
date_default_timezone_setfunction. - Use the
strftimefunction to format the date with the locale configuration. - Optionally use
utf8_encodeto handle encoding issues.
Note: utf8_encode is only necessary if you haven’t declared the encoding in your HTML code:
<meta charset="utf-8">Here is the PHP code to display a date in a localized format:
<?php
setlocale(LC_TIME, "en_US.utf8", "en_US", "English_United States");
date_default_timezone_set("Europe/London");
echo utf8_encode(strftime("%A %d %B %Y, %H:%M"));
// Result: Thursday 09 July 2020, 13:31
?>Note: replace the last line with echo strftime('%A %d %B %Y, %H:%M'); if you have declared the encoding in your HTML code.
You can find more information about strftime in the official PHP documentation.
PHP 8.1: strftime and gmstrftime functions are deprecated
The strftime and gmstrftime functions format a Unix timestamp based on the current locale settings defined with the setlocale() function.
The setlocale function sets the locale on the PHP process rather than the current worker thread. This is problematic when there are multiple worker threads.
PHP offers independent, locale-aware time formatting APIs that are better, richer, and do not suffer from this limitation. In particular, the intl extension and the IntlDateFormatter class.
<?php
$formatter = new IntlDateFormatter("en_US", IntlDateFormatter::FULL, IntlDateFormatter::SHORT);
echo $formatter->format(new DateTime());
// Result: Thursday, July 9, 2020 at 1:31 PM
?><?php
$formatter = new IntlDateFormatter(
"en_US",
IntlDateFormatter::FULL,
IntlDateFormatter::NONE,
"America/New_York",
IntlDateFormatter::GREGORIAN,
"EEEE d MMMM yyyy"
);
echo $formatter->format(new DateTime());
?>Solution
For locale-aware date/time formatting in PHP, use IntlDateFormatter::format (requires the intl extension):
In a real-world, ideal use case, the IntlDateFormatter object is instantiated once based on the user’s locale information (from a session, a user database field, etc.).
<?php
$formatter = new IntlDateFormatter(
"en_US",
IntlDateFormatter::LONG,
IntlDateFormatter::SHORT,
"America/New_York"
);
echo $formatter->format(new DateTime());
// Result: July 9, 2020, 1:31 PM
?>For locale-independent date/time formatting, use the date() function or DateTime::format / DateTimeImmutable::format:
<?php
echo date("d/m/Y H:i:s");
// or
echo (new DateTime())->format("d/m/Y H:i:s");
?>DateTimeImmutable::format can also format a DateTimeImmutable object:
<?php
$date = new DateTimeImmutable("2020-07-09 13:30:15", new DateTimeZone("Europe/Paris"));
echo $date->format("d/m/Y H:i:s");
// Result: 09/07/2020 13:30:15
?>






0 Comments