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}!}
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.
Dear Jamal,
Should I need to resume the locale after I return the date?
Best regards,
Kelvin
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/