Reputation: 1519
I am working on a php code as shown below:
<?php <time datetime="<?php echo esc_attr(date_format($ts, 'H:i d-m-Y')) ?>"
data-timezone="<?php echo esc_attr($tz_param) ?>"><?php echo esc_html(date_format($ts, 'F j H:i')) ?></time> ?> // Line A
Line A returns the following date on the webpage:
July 10 21:30
print_r($ts)
prints:
DateTime Object
(
[date] => 2019-07-10 21:30:00.000000
[timezone_type] => 3
[timezone] => America/New_York
)
July 10 21:30
Problem Statement:
I am wondering what changes I should make in the php code above at Line A above so that when the page is in french, it should return the date in french.
This is what I have tried but it is still returning the date in Engllish.
<?php if(ICL_LANGUAGE_CODE=='fr'){
setlocale(LC_TIME, 'fr_FR');
?>
<time datetime="<?php echo esc_attr(date_format($ts, 'H:i d-m-Y')) ?>"
data-timezone="<?php echo esc_attr($tz_param) ?>"><?php echo strftime(esc_html(date_format($ts, 'F j H:i'))) ?></time> // Line B
<?php } ?>
Line B above still returns english.
Upvotes: 3
Views: 2862
Reputation: 36
From http://php.net/manual/en/function.date.php:
To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().
Upvotes: 0
Reputation: 791
goes around the issue:
<?php
if (date("m", time() ) === "01" ) { print "Janvier"; }
if (date("m", time() ) === "02" ) { print "Février"; }
if (date("m", time() ) === "03" ) { print "Mars"; }
if (date("m", time() ) === "04" ) { print "Avril"; }
if (date("m", time() ) === "05" ) { print "Mai"; }
if (date("m", time() ) === "06" ) { print "Juin"; }
if (date("m", time() ) === "07" ) { print "Juillet"; }
if (date("m", time() ) === "08" ) { print "Août"; }
if (date("m", time() ) === "09" ) { print "Septembre"; }
if (date("m", time() ) === "10" ) { print "Octobre"; }
if (date("m", time() ) === "11" ) { print "Novembre"; }
if (date("m", time() ) === "12" ) { print "Décembre"; }
?>
I tried to use the setlocale() process and had the same problem you did. The printout was still in English. This might be an 'old school' solution, but it's simple, and it works.
Upvotes: 0
Reputation: 10709
You should use an intl extension:
$ts = new DateTime();
$formatter = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::LONG);
$formatter->setPattern('MMMM d HH:mm');
echo $formatter->format($ts);
Disponible formats for setPattern are listed here
Upvotes: 5
Reputation: 7614
Assuming your website's base language is French, you can make use of the built-in WordPress function date_i18n()
like this:
echo date_i18n( 'F j H:i', $ts ); //Assuming $ts is your time stamp
If $ts
is actually a date object, you will have to grab the timestamp first like this:
echo date_i18n( 'F j H:i', $ts->getTimeStamp() ); //Assuming $ts is a dateTime object
If you are struggling with a timezone difference (i.e. the time returned is a few hours ahead/behind) then you will need to combine the function with get_date_from_gmt
like this:
$correct_date = get_date_from_gmt(date_format($ts, 'Y-m-d H:i:s')); //gets the date in your current timezone
echo date_i18n( 'F j H:i', strtotime($correct_date) );
Upvotes: 2
Reputation: 222
Reference from php doc
Note: The return value of setlocale() depends on the system that PHP is running. It returns exactly what the system setlocale function returns.
setlocale(LC_ALL, 'fr_FR');
echo strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978)); //vendredi 22 d�cembre 1978
Upvotes: 0
Reputation: 520
Use strftime : http://php.net/manual/en/function.strftime.php
<?php
setlocale(LC_TIME, "fr_FR");
echo strftime(" in French %d.%M.%Y and");
(not sure about that %d.%M.%Y but you can read documentation)
OR use Carbon package
Localization http://carbon.nesbot.com/docs/#api-localization
Carbon::setLocale('fr');
Carbon::yesterday()-> diffForHumans();
Upvotes: 2