What is an alternative to strftime?

Solved
emrh Posted messages 439 Status Member -  
emrh Posted messages 439 Status Member -

Hello,

I need to display the date with the day's name in French... I found this that works very well:

setlocale(LC_TIME, 'fr_FR.utf8','fra'); $aujourdhui = ucwords(strftime('%A %d %B %Y')); Aujourd'hui : <?php echo $aujourdhui;?> 

Result: Today: Sunday, July 17, 2022

However, I just read on PHP.net regarding strftime: "This function is OBSOLETE as of PHP 8.1.0. It is strongly recommended to avoid them."

What would be the alternative to this instruction to get exactly the same result?
I've tried a lot of things using date() but I can't manage to get something that matches the desired syntax...

Thanks in advance for your insights!


3 answers

Pitet Posted messages 2845 Status Member 530
 

Hello,

Prefer using the DateTime and DateInterval classes to manipulate dates in PHP.
As indicated in the documentation, an alternative is to use the IntlDateFormatter class.
You can use the predefined constants of this class (LONG, SHORT, RELATIVE_FULL, ...) for the date and time format upon instantiation (2nd and 3rd parameters).
https://www.php.net/manual/fr/class.intldateformatter.php#intl.intldateformatter-constants

You can also define a custom format with the setPattern method.
https://www.php.net/manual/fr/intldateformatter.setpattern.php

<?php $today = new DateTime(); $formatter = new IntlDateFormatter('en_US', IntlDateFormatter::FULL, IntlDateFormatter::NONE); echo $formatter->format($today); // Sunday, July 17, 2022 $formatter->setPattern('EEEE d MMMM y'); echo ucwords($formatter->format($today)); // Sunday, July 17, 2022 
2
Anonymous user
 

Hello, thanks for the example. Like the author, I planned to look into this change; I'll keep your snippet in mind.

Have a nice day :).

0
emrh Posted messages 439 Status Member 20
 

Hello Pitet,
Thank you for your help...

But I have this error message:

Fatal error: Uncaught Error: Class 'IntlDateFormatter' not found in /var/www/html/gite/gestion/accueil.php:78 Stack trace: #0 {main} thrown in /var/www/html/gite/gestion/accueil.php on line 78

On the other hand, if I copy/paste into my code
class IntlDateFormatter {
..../....
public setTimeZone(IntlTimeZone|DateTimeZone|string|null $timezone): ?bool
}

I get this:
Parse error: syntax error, unexpected '(', expecting variable (T_VARIABLE) in /var/www/html/gite/gestion/accueil.php on line 78

My lines 76 to 85

class IntlDateFormatter { /* Methods */ public __construct( ?string $locale, int $dateType = IntlDateFormatter::FULL, int $timeType = IntlDateFormatter::FULL, IntlTimeZone|DateTimeZone|string|null $timezone = null, IntlCalendar|int|null $calendar = null, ?string $pattern = null )

But one thing really worries me: Will I have to copy all this gibberish to replace my original 2 lines?

0
Pitet Posted messages 2845 Status Member 530
 

No, you shouldn't redefine the class; you need to enable the PHP Intl extension.

What environment are you developing in? If you're on Windows with software like WampServer or Laragon, the Intl extension should already be present but not enabled. Generally, you just need to uncomment the line extension=intl in the php.ini file and restart the server (you should also be able to enable the extension via the WampServer/Laragon/etc. tool interface). If you're on Linux, you should easily find information on how to enable this extension based on your distribution.

0
emrh Posted messages 439 Status Member 20
 

Indeed, I just transferred the relevant page.php to Ionos and your code is working perfectly!
(I will check my php.ini file, I'm on Linux with a local server).

Thank you very much, I’m adopting your code to avoid the obsolescence of mine!

:-)

0
emrh Posted messages 439 Status Member 20
 

On Linux:

sudo apt-get install php7.4-intl               (7.4 for me)
sudo service apache2 restart

1