Home > Back-end >  Why is strftime being removed from php?
Why is strftime being removed from php?

Time:11-15

I searched the whole web, but was not able to find the reason why strftime is being removed from php. From my point of view it was the perfect function for easy access to custom date formats. The "alternative" IntlDateFormatter::format() feels so cumbersome.

Can anyone explain why strftime is no longer part of php?

CodePudding user response:

It's because it has "better" methods, like IntlDateFormatter::format

As stated in here

PHP offers locale-independent and locale-aware time formatting APIs that are better, feature-rich, and more intuitive. In PHP 8.1 and later, strftime and gmstrftime are deprecated, and using them emits a deprecation notice. These functions will be removed in PHP 9.0.

The problem is also stated there:

strftime and gmstrftime functions format a Unix timestamp based on the current locale set using the setlocale function. gmstrftime function is similar to strftime, except that it returns the time in Greenwich Mean Time (GMT).

setlocale function sets the locale setting to the PHP process, rather than the current working thread. This can result in sudden change in the locale if another thread calls setlocale function. This can be specially troublesome if PHP is used in a multi-threaded Server API (such as mod_php for PHP).

It also quotes replacements:

For locale-aware date/time formatting, use IntlDateFormatter::format (requires Intl extension).

In a real-life and ideal use case, the IntlDateFormatter object is instantiated once based on the user's locale information, and the object instance can is used whenever necessary.

 setlocale(LC_TIME, "en_US");
 echo strftime('%x', time());
 $formatter = new IntlDateFormatter('en_US', IntlDateFormatter::LONG, IntlDateFormatter::NONE);
 echo $formatter->format(time());

CodePudding user response:

From here:

strftime and gmstrftime functions format a Unix timestamp based on the current locale set using the setlocale function. gmstrftime function is similar to strftime, except that it returns the time in Greenwich Mean Time (GMT).

setlocale function sets the locale setting to the PHP process, rather than the current working thread. This can result in sudden change in the locale if another thread calls setlocale function. This can be specially troublesome if PHP is used in a multi-threaded Server API (such as mod_php for PHP).

PHP offers locale-independent and locale-aware time formatting APIs that are better, feature-rich, and more intuitive. In PHP 8.1 and later, strftime and gmstrftime are deprecated, and using them emits a deprecation notice. These functions will be removed in PHP 9.0.

  •  Tags:  
  • php
  • Related