Replacing strftime() and gmstrftime() in PHP 8.1 and later
Deprecated: Function strftime() is deprecatedShort 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.
Fix it fast
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 public2Match what you see, then jump to its fix
- 1
Formats 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
The 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
The 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
- 1
What does the error look like?
Deprecated: Function strftime() is deprecatedDeprecated: 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
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.phpConfirmed by php.net
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 --ini2Fix
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=intl3Verify
Confirm the extension is loaded in the same runtime that serves the site.
php -r "var_dump(extension_loaded('intl'));"Confirmed by php.net
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 fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix