W3docs

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 (or null), the current time is used, so you rarely need to pass time() 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 stylesingle letters (d, m, Y)%-prefixed (%d, %m, %Y)
Locale-aware namesno (always English)yes — follows setlocale(LC_TIME, …)
Statusavailabledeprecated 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

php— editable, runs on the server

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

php— editable, runs on the server

%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

SpecifierMeaningExample
%YYear, 4 digits2023
%yYear, 2 digits23
%mMonth, 2 digits03
%BFull month name (localized)March
%bAbbreviated month nameMar
%dDay of month, zero-padded03
%eDay of month, space-padded 3
%AFull weekday name (localized)Friday
%aAbbreviated weekday nameFri
%HHour, 24-hour, zero-padded09
%IHour, 12-hour, zero-padded09
%MMinutes05
%SSeconds07
%pAM / PMAM
%jDay of the year062
%%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 with mktime() or strtotime().
  • 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.
  • %e is not available on Windows. Some specifiers are platform-dependent because strftime() 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.

Practice

Practice
What does the strftime() function in PHP do?
What does the strftime() function in PHP do?
Was this page helpful?