Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Replacing strftime() and gmstrftime() in PHP 8.1 and later

ErrorDeprecated: Function strftime() is deprecated

Short answer

strftime() and gmstrftime() are deprecated since PHP 8.1. For output that does not depend on language, use date() or DateTimeInterface::format(). For translated month and day names, use IntlDateFormatter, which needs the intl extension. Rewrite the format string, since the codes are different.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every call, so you can sort them into language-independent and language-dependent ones.

    grep -rnE "g?m?strftime\(" --include=*.php src app public
  2. 2Match what you see, then jump to its fix

    1. 1Formats like %Y-%m-%d %H:%M:%S, with no month or weekday names, or names you only need in English.The format only has numbers or English namesFix
    2. 2The code calls setlocale(LC_TIME, ...) first, or prints month and weekday names in French, German or another language.The output must be in the visitor's languageFix
    3. 3The intl check in the previous cause prints bool(false), or code using IntlDateFormatter stops because the class does not exist.IntlDateFormatter is not available on the serverFix

What does the error look like?

  • Deprecated: Function strftime() is deprecated
  • Deprecated: Function gmstrftime() is deprecated

Cause 1 of 3

The format only has numbers or English names

1Check

The manual says to use date() for locale-independent formatting. DateTime format() does not use locales, so names come out in English.

<?php
echo date('Y-m-d H:i:s', 0), PHP_EOL;
echo (new DateTimeImmutable('2000-01-01 08:05:09'))->format('D, j F Y');

2Fix

Swap the function and rewrite the format string with date() format characters, since strftime codes are not accepted. For example %Y-%m-%d becomes Y-m-d. Look up every other code in the date format table (Y year, m month, d day, H hour, i minutes, s seconds, j day without zero, l weekday name, F month name).

// before
echo strftime('%Y-%m-%d %H:%M', $time);
// after
echo date('Y-m-d H:i', $time);

3Verify

Compare old and new output for the same timestamp before removing the old call.

php -d error_reporting=E_ALL -r "echo date('Y-m-d H:i', 86400), PHP_EOL;"

Confirmed by php.net

Did this fix it?

Cause 2 of 3

The output must be in the visitor's language

1Check

strftime() followed the system locale, which had to be installed on the server. The manual points to IntlDateFormatter::format() for this case. The class comes from the intl extension.

php -r "var_dump(extension_loaded('intl'));"

2Fix

Create a formatter for the locale with an ICU pattern, then format the timestamp. With a pattern, the pattern decides the layout and the locale decides the language.

<?php
$fmt = new IntlDateFormatter(
    'fr_FR',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    'Europe/Paris',
    IntlDateFormatter::GREGORIAN,
    'EEEE d MMMM y'
);
echo $fmt->format(time());

3Verify

Run it and check the day and month names appear in the chosen language.

php demo.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

IntlDateFormatter is not available on the server

1Check

The intl extension is built on the ICU library and must be installed and enabled for the PHP version that runs your site.

php -m | grep -i intl
php --ini

2Fix

Install or enable the intl extension for that exact PHP build (package manager, hosting panel, or extension=intl in php.ini on Windows), then restart PHP-FPM or the web server. Do not fall back to the deprecated function.

; php.ini
extension=intl

3Verify

Confirm the extension is loaded in the same runtime that serves the site.

php -r "var_dump(extension_loaded('intl'));"

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Keep date formatting in one helper function, so a future change touches one place.
  • Store timestamps in UTC and format only at the edge.
  • Add intl to your container image or provisioning script, so every server has it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

PHP warns that null is passed to a built-in function

Deprecated: Passing null to parameter #2 ($needle) of type string is deprecated

Since PHP 8.1, built-in functions no longer accept null for a non-nullable scalar parameter silently.

3 causes, each with a fix

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.