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.
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) );