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 (which is in English):
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'){ /* If the page is in french */
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 still returns english
even after using strftime
.
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 (which is in English):
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'){ /* If the page is in french */
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 still returns english
even after using strftime
.
1 Answer
Reset to default 1there is a custom function in wordpress called date_i18n. so basically you do
echo date_i18n( 'H:i d-m-Y', $ts );
without the setLocale stuff..
find the function date_i18n and its parameters here.
you could even build in your translation all in there, without the language check before:
date_i18n( __( 'H:i d-m-Y', 'textdomain' ) );
(replace 'textdomain' with your theme textdomain..)
ICL_LANGUAGE_CODE
is the old method on getting the current language in WPML. use$current_language = apply_filters('wpml_current_language', null);
instead.. more here – honk31 Commented Jul 17, 2019 at 16:10