strftime()
Learn how PHP's strftime() formats a timestamp using locale-aware POSIX specifiers, why it was deprecated in PHP 8.1, and what to use instead.
Introduction
strftime() formats a Unix timestamp into a human-readable date/time string using POSIX-style specifiers (%Y, %B, %H, …) and — crucially — respects the active locale. That made it the go-to function whenever you needed month or weekday names in a language other than English.
This chapter shows how strftime() works, lists the most useful specifiers, and explains why you should reach for DateTime::format() or IntlDateFormatter in new code. strftime() was deprecated in PHP 8.1 and removed in PHP 9.0, so understanding both the old and the new approach matters when you maintain or migrate a codebase.
Syntax
strftime(string $format, ?int $timestamp = null): string|false$format— a string of literal text plus%specifiers (see the table below).$timestamp— a Unix timestamp. If omitted (ornull), the current time is used, so you rarely need to passtime()explicitly.
It returns the formatted string, or false on failure.
How strftime() differs from date()
Both functions turn a timestamp into a string, but they answer different needs:
date() | strftime() | |
|---|---|---|
| Specifier style | single letters (d, m, Y) | %-prefixed (%d, %m, %Y) |
| Locale-aware names | no (always English) | yes — follows setlocale(LC_TIME, …) |
| Status | available | deprecated in 8.1, gone in 9.0 |
So if you only need English output, date() is simpler. The historical reason to choose strftime() was localized month and weekday names.
A basic example
Here date() formats the current time as month/day/year, producing output like 03/03/2023. This is the English-only baseline that strftime() extends with localization.
Using strftime() for locale-aware output
%B is the full month name, %d the zero-padded day, and %Y the four-digit year, so the output reads like March 03, 2023. The power of strftime() shows up once you switch the locale.
Switching the locale
For specifiers like %B (month name) and %A (weekday name) to return localized text, set the LC_TIME locale before calling strftime() with setlocale():
<?php
setlocale(LC_TIME, 'fr_FR.UTF-8'); // French
echo strftime('%A %d %B %Y', mktime(0, 0, 0, 3, 3, 2023));
// → "vendredi 03 mars 2023"
?>The exact locale string (fr_FR.UTF-8, de_DE.UTF-8, …) must be installed on the server, otherwise setlocale() returns false and the output falls back to the C/English locale.
Common specifiers
| Specifier | Meaning | Example |
|---|---|---|
%Y | Year, 4 digits | 2023 |
%y | Year, 2 digits | 23 |
%m | Month, 2 digits | 03 |
%B | Full month name (localized) | March |
%b | Abbreviated month name | Mar |
%d | Day of month, zero-padded | 03 |
%e | Day of month, space-padded | 3 |
%A | Full weekday name (localized) | Friday |
%a | Abbreviated weekday name | Fri |
%H | Hour, 24-hour, zero-padded | 09 |
%I | Hour, 12-hour, zero-padded | 09 |
%M | Minutes | 05 |
%S | Seconds | 07 |
%p | AM / PM | AM |
%j | Day of the year | 062 |
%% | A literal percent sign | % |
The modern replacement
Because strftime() is gone in PHP 9.0, new code should format with DateTime or, for full localization, IntlDateFormatter from the intl extension:
<?php
// English-only output: DateTime::format() with date()-style letters
$dt = new DateTime('2023-03-03');
echo $dt->format('F d, Y'); // March 03, 2023
// Localized output without a server-wide locale: IntlDateFormatter
$fmt = new IntlDateFormatter(
'fr_FR',
IntlDateFormatter::FULL,
IntlDateFormatter::NONE
);
echo "\n", $fmt->format($dt); // vendredi 3 mars 2023
?>IntlDateFormatter is the recommended path: it is ICU-backed, does not depend on OS-installed locales, and is not affected by the deprecation. See DateTime::format() for the full specifier set.
Things to watch out for
- Pass a timestamp, not a
DateTime.strftime()expects an integer Unix timestamp; build one withmktime()orstrtotime(). - Locale must be installed. A missing locale silently degrades to English — check the return value of
setlocale(). - Use
gmstrftime()if you need UTC instead of the local timezone. %eis not available on Windows. Some specifiers are platform-dependent becausestrftime()delegates to the C library.
Conclusion
strftime() formatted a timestamp into a locale-aware string using POSIX % specifiers, which is why it was historically preferred over date() for non-English month and weekday names. Since it is deprecated in PHP 8.1 and removed in 9.0, treat it as legacy: read it confidently in existing code, but write new code with DateTime::format() or IntlDateFormatter.