John Davidson

Get Locale Date in PHP - 2022

0 comments
Message:


How should I get the Day and Month (i.e. Friday, August) with the corresponding locale (i.e. de_DE) in PHP?


Most examples use setlocale() and strftime(), the last one being deprecated as of PHP 8.1.0.



My Answer:
To get the Day and Month with the corresponding locale in PHP without using the deprecated strftime() function, you can use the IntlDateFormatter class. Here's an example of how you can achieve this:

php
$locale = 'de_DE';
$date = new DateTime();
$dateFormatter = new IntlDateFormatter($locale, IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'UTC');
$dateFormatter->setPattern('EEEE, MMMM');
echo $dateFormatter->format($date);


In this example, we first define the desired locale ('de_DE') and create a new DateTime object. We then create a new IntlDateFormatter object with the specified locale, date and time formats, and timezone. We set the pattern to 'EEEE, MMMM' to format the output as the full day of the week followed by the full month name. Finally, we use the format() method of the IntlDateFormatter object to format the date and output the result.

This way, you can get the Day and Month with the corresponding locale in PHP without using the deprecated strftime() function.

Rate this post

3 of 5 based on 2407 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us