PHP strftime() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

In PHP, strftime() formats a timestamp in local time using C-style % format codes. It is deprecated and no longer available in modern PHP.

This function is deprecated as of PHP 8.1 (removal planned for a future PHP major). It is documented here so you can understand older code. For new code, use date() or IntlDateFormatter instead.

Syntax

Call strftime() with the values you need. Start with this line.

PHP
$text = strftime('%A, %d %B %Y', 1700000000);
CallWhat it does
strftime('%A, %d %B %Y', 1700000000)Returns "Tuesday, 14 November 2023" in the default C locale and UTC.
strftime('%H:%M', 1700000000)Returns "22:13" in UTC.
strftime('%Y-%m-%d', 1700000000)Returns "2023-11-14".

Do not use this function in new code. Use function_exists('strftime') while migrating old code. For English output, date() is enough. For translated month and day names, use IntlDateFormatter.

Library declarations
PHP
strftime(string $format, ?int $timestamp = null): string|false

Parameters

ParameterDescription
formatRequired. A format string with % codes, such as %Y, %m, %d, %e, %H, %M, %S, %A and %B.
timestampOptional. A Unix timestamp. If it is null or left out, the current time is used.

Return Value

Return ValueDescription
stringThe formatted date and time in the default time zone.
falseReturned on failure.

PHP Examples

Example 1 — Legacy Call with a Safe Fallback

Check that strftime() exists, and fall back to date() when it does not. Both paths print the same text.

PHP
<?php
error_reporting(E_ALL & ~E_DEPRECATED);
date_default_timezone_set('UTC');

$ts = 1700000000;

if (function_exists('strftime')) {
    echo strftime('%A, %d %B %Y', $ts), PHP_EOL;
    echo strftime('%H:%M', $ts), PHP_EOL;
} else {
    echo date('l, d F Y', $ts), PHP_EOL;
    echo date('H:i', $ts), PHP_EOL;
}

How It Works

  1. error_reporting() hides the deprecation notice on PHP 8. 1 to 8.3.
  2. function_exists() avoids a fatal error on versions where strftime() is gone.
  3. %A, %d, %B and %Y match l, d, F and Y in date().
  4. %H:%M matches H:i. In strftime() %M is minutes, but in date() the minutes letter is i.

Example 2 — %e vs %d for Single-Digit Days

%d pads a day with a zero, while %e pads it with a space.

PHP
<?php
error_reporting(E_ALL & ~E_DEPRECATED);
date_default_timezone_set('UTC');

$ts = 1699185600; // 5 November 2023, 12:00 UTC

if (function_exists('strftime')) {
    echo strftime('[%e] [%d]', $ts), PHP_EOL;
} else {
    echo '[', sprintf('%2d', (int) date('j', $ts)), '] [', date('d', $ts), ']', PHP_EOL;
}

How It Works

  1. The timestamp falls on 5 November 2023, a single-digit day.
  2. %e gives ' 5' with a leading space and %d gives '05' with a leading zero.
  3. The fallback uses sprintf('%2d') to build the same space-padded value.
  4. date('d') already gives the zero-padded form.

Example 3 — Modern Replacement with IntlDateFormatter

IntlDateFormatter formats dates for any locale. Use it when you need translated names. The fallback keeps the example working without the intl extension.

PHP
<?php
date_default_timezone_set('UTC');

$ts = 1700000000;

if (class_exists('IntlDateFormatter')) {
    $formatter = new IntlDateFormatter(
        'en_US',
        IntlDateFormatter::NONE,
        IntlDateFormatter::NONE,
        'UTC',
        IntlDateFormatter::GREGORIAN,
        'EEEE, MMMM d, y'
    );
    echo $formatter->format($ts), PHP_EOL;
} else {
    echo date('l, F j, Y', $ts), PHP_EOL;
}

How It Works

  1. class_exists() checks whether the intl extension is installed.
  2. The pattern 'EEEE, MMMM d, y' means full weekday, full month, day and year.
  3. Change 'en_US' to another locale, such as 'fr_FR', to get translated names.
  4. Without intl, date('l, F j, Y') prints the same English text.

Frequently Asked Questions

No. It was Deprecated in PHP 8.1 (removal planned for a future PHP major). Use date() or IntlDateFormatter in new code.
Use date() for English output, or IntlDateFormatter when you need localized names such as French or Spanish months.
Its output depended on the operating system and setlocale(), so results differed between servers and were hard to test.
Map each code: %Y to Y, %m to m, %d to d, %H to H, %M to i, %S to s, %A to l and %B to F. Some codes have no direct match, so check each one.
It did, after calling setlocale(), but only if that locale was installed on the server. IntlDateFormatter is the reliable way to do this today.

Next: strptime()

strptime() parses a date and time string using C-style % format codes and returns the part.

strptime() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful