Skip Navigation

[Résolu] Force date format in English

This support ticket is created Il y a 2 années et 5 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 3 réponses, has 2 voix.

Last updated by Jamal Il y a 2 années et 5 mois.

Assisted by: Jamal.

Auteur
Publications
#2213373
Screenshot 2021-11-05 at 11.05.49 AM.png

Dear Sir/Madam,

My site default language is Traditional Chinese, I want to change the date format in English. How can I do that?

Here is my shortcdoe

時間:{!{types field='class-start-time' style='text' format='d-M-Y h:ia'}!}{!{/types}!} - {!{types field='class-end-time' style='text' format='d-M-Y h:ia'}!}{!{/types}!}
#2216025

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting the Toolset support.

Toolset uses WordPress functions to render date fields. WordPress on the other hand uses PHP functions to render a date. The used functions are forced to use the server locale. However, WordPress does offer some other functions that support localization.
Check this article https://wordpress.org/support/article/formatting-date-and-time/#localization

This being said, you will need to build your own shortcode that will render the date in the desired language. And you will be able to do it with two functions (setlocale to set the language), then strftime to format the time value. This is a simple PHP example:

<?php
setlocale(LC_ALL, 'fr_FR');

echo strftime("%A %e %B %Y");
?>

Read more about these functions here:
- hidden link
- hidden link
- hidden link

Keep in mind that Toolset Date fields are stored as timestamps. You can get the value by prefixing the field's slug with "wpcf-":

global $post;
$start_time = get_post_meta($post->ID, 'wpcf-class-start-time', true)
setlocale(LC_ALL, 'fr_FR');
return strftime("%A %e %B %Y", $start_time);

I hope this helps. Let me know if you have any questions.

#2219153

Dear Jamal,

Should I need to resume the locale after I return the date?

Best regards,

Kelvin

#2220461

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+01:00)

I would say yes, this way, you will let other parts of the website use the default locale. You can get the current local by assigning the locale 0:

$current_locale = setlocale( LC_ALL, 0 );

Another option, is to set the locale only for dates and time by using LC_TIME instead of LC_ALL.

Please note that the target local needs to be installed on the system. Check these online threads:
- https://stackoverflow.com/questions/12565981/setlocale-and-strftime-not-translating-month
- hidden link

This being said, please note that this is a general PHP question, rather than a Toolset specific question. So, it is considered custom code and it is out of the scope of the support. If you are not comfortable with PHP programming consider hiring a developer https://toolset.com/contractors/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.