PHP has been evolving rapidly, and with the release of PHP 8.1, several long-standing functions were deprecated. One such function is strftime(), which has been used for decades to format date and time values. In modern PHP development, it’s important to transition to the newer, more robust alternatives such as IntlDateFormatter. This tutorial will guide you through understanding why strftime() was deprecated, how to switch to IntlDateFormatter, and how to optimize your date and time handling in PHP moving forward.
This tutorial is designed to cover a wide range of topics, from beginner to advanced use cases, so whether you’re just starting with PHP or you’re an experienced developer, you’ll find actionable insights and code samples at various levels.
Why Was strftime() Deprecated in PHP 8.1?
The strftime() function has been around for a long time, but over the years, it has shown limitations. Primarily, it relies on the system’s locale settings, which can be inconsistent across different environments. Moreover, strftime() is not UTF-8 safe, which makes it problematic when dealing with internationalization. These limitations prompted PHP’s core developers to deprecate it in favor of modern alternatives that handle localization and formatting more reliably, such as IntlDateFormatter from the intl extension.
Key Benefits of IntlDateFormatter
- Locale Aware: Handles different locale settings more effectively.
- Unicode Support: Provides proper UTF-8 handling, ensuring consistent formatting across languages and character sets.
- Flexibility:
IntlDateFormatteroffers a more versatile approach to date and time formatting compared tostrftime(). - Consistency: The
IntlDateFormatteris part of the Internationalization Functions (intl) extension, which is designed to handle globalization, making it a more future-proof choice.
Transitioning from strftime() to IntlDateFormatter
Basic Example with strftime()
Let’s start by reviewing a basic example of how you might have used strftime() in earlier versions of PHP:
$date = strtotime('2023-09-01');
echo strftime("%B %d, %Y", $date); // Output: September 01, 2023
In this case, the strftime() function formats the date to display the full month name, day, and year. However, after PHP 8.1, using this function will generate a deprecation warning, and eventually, it will be removed entirely from future PHP versions.
Converting to IntlDateFormatter
To modernize this code and avoid deprecation issues, you can replace strftime() with IntlDateFormatter. The IntlDateFormatter provides similar functionality, but with better locale and UTF-8 handling.
Here’s how you can refactor the previous example using IntlDateFormatter:
$date = strtotime('2023-09-01');
$formatter = new IntlDateFormatter('en_US', IntlDateFormatter::LONG, IntlDateFormatter::NONE);
echo $formatter->format($date); // Output: September 1, 2023
In this code, we use IntlDateFormatter to format the date according to the US English locale. The constants IntlDateFormatter::LONG and IntlDateFormatter::NONE specify that we want a long date format (e.g., “September 1, 2023”) without any time component.
Formatting with Different Locales
One of the advantages of IntlDateFormatter is its ability to handle various locales easily. For example, if you need to display the date in French:
$date = strtotime('2023-09-01');
$formatter = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::NONE);
echo $formatter->format($date); // Output: 1 septembre 2023
In this case, the date is formatted according to French conventions. This flexibility makes IntlDateFormatter a great choice for applications that need to handle multiple languages.
Advanced Date and Time Formatting with IntlDateFormatter
For more advanced use cases, such as formatting both date and time, you can modify the constants passed into IntlDateFormatter. For example, to include the time:
$date = strtotime('2023-09-01 14:30:00');
$formatter = new IntlDateFormatter('en_US', IntlDateFormatter::LONG, IntlDateFormatter::SHORT);
echo $formatter->format($date); // Output: September 1, 2023, 2:30 PM
In this case, we use IntlDateFormatter::SHORT to format the time as “2:30 PM” and IntlDateFormatter::LONG for the date format.
Handling Custom Date Formats
Sometimes, you may need more control over the date format, beyond the predefined styles. IntlDateFormatter allows you to specify custom date and time patterns using ICU (International Components for Unicode) patterns.
Here’s an example of using a custom pattern:
$date = strtotime('2023-09-01 14:30:00');
$formatter = new IntlDateFormatter('en_US', IntlDateFormatter::NONE, IntlDateFormatter::NONE);
$formatter->setPattern('MMMM d, yyyy, h:mm a');
echo $formatter->format($date); // Output: September 1, 2023, 2:30 PM
In this example, we create a custom pattern that formats the date as “September 1, 2023, 2:30 PM”. You can adjust the pattern according to your needs, making IntlDateFormatter a versatile tool for date and time formatting.
Localization and Internationalization
One of the key benefits of transitioning to IntlDateFormatter is its support for localization and internationalization. When working on applications that need to support multiple languages and regions, using the intl extension ensures that date and time values are formatted correctly for each locale.
Working with Timezones
IntlDateFormatter also allows you to handle different time zones, which is crucial for applications dealing with global users. To set a specific time zone, use the setTimeZone() method:
$date = strtotime('2023-09-01 14:30:00');
$formatter = new IntlDateFormatter('en_US', IntlDateFormatter::LONG, IntlDateFormatter::SHORT, 'America/New_York');
echo $formatter->format($date); // Output: September 1, 2023, 2:30 PM EDT
By specifying the time zone, you can ensure that dates and times are presented accurately according to the user’s or application’s settings.
Common Pitfalls When Migrating from strftime() to IntlDateFormatter
- Time Zone Mismatches: When working with dates, ensure you are setting the correct time zone for the application.
- Locale Settings: Always verify that the locale is set appropriately to avoid incorrect formatting, especially for non-English languages.
- Pattern Formatting: Be cautious when working with custom date patterns; a slight mistake in the pattern can lead to incorrect formatting.
Conclusion
As PHP continues to evolve, it’s important to keep your codebase up to date with modern standards. The deprecation of strftime() in PHP 8.1 marks a significant shift in how date and time formatting should be handled. By transitioning to IntlDateFormatter, you gain access to a powerful and flexible tool that supports internationalization, Unicode, and consistent date formatting across environments.
Whether you’re building simple applications or complex, global systems, IntlDateFormatter provides the necessary capabilities to handle date and time formatting reliably. By following this tutorial, you now have the knowledge to migrate from strftime() to IntlDateFormatter and modernize your date and time handling in PHP.

