admin管理员组

文章数量:1336602

I have a requirement to change the order of DD/MM/YYYY tags according to a users country .

The way that I was thinking to do this is to create a country / Dateformat table and according to the country selection to move the fields around using jquery .

Is there an existing way for this to be done in php or even in js or a better approach ? I was also looking for a table of country/ dateformat rather than inserting all the values manually but I couldn't find anything ...

I have a requirement to change the order of DD/MM/YYYY tags according to a users country .

http://en.wikipedia/wiki/Date_format_by_country

The way that I was thinking to do this is to create a country / Dateformat table and according to the country selection to move the fields around using jquery .

Is there an existing way for this to be done in php or even in js or a better approach ? I was also looking for a table of country/ dateformat rather than inserting all the values manually but I couldn't find anything ...

Share Improve this question edited Mar 10, 2013 at 21:20 scott.korin 2,5972 gold badges25 silver badges37 bronze badges asked Mar 10, 2013 at 21:16 AthanatosAthanatos 1,0897 gold badges15 silver badges33 bronze badges 4
  • Check out the term "locale". – Bart Friederichs Commented Mar 10, 2013 at 21:18
  • Take a look at jqueryui./datepicker/#localization and docs.jquery./UI/Datepicker/Localization – MatRt Commented Mar 10, 2013 at 21:21
  • Thanks for the answers everyone but I need a small example of code if possible to move the fields around using e.g the JS toLocaleString that was suggested , I have three select elements DD MM YYYY . The jquery example is good but it looks that you need to include a datepicker for each country. – Athanatos Commented Mar 10, 2013 at 21:36
  • By far the best solution is to use a single, unambiguous format (e.g. 10 March, 2013). If you attempt to "localise" the string, you will still get it wrong a good percentage of the time. Also, the implementation of Date.prototype.toLocaleString is implementation dependent, so you will still need to manually parse the date if you want to present it in a consistent format across all browsers (Some may do Tuesday, 11 March 2013 and some may do 3/11/2013 and others 2013-03-11). – RobG Commented Mar 10, 2013 at 23:19
Add a ment  | 

3 Answers 3

Reset to default 2

For PHP, this should be a good start: http://php/manual/en/function.setlocale.php

For JavaScript: Display date/time in user's locale format and time offset

All in all, most modern languages have locale support built-in very well. You should not have to implement this yourself. It will be tiresome and buggy (localization is hard).

if you use PHP then the IntlDateFormatter() helps you out:

$d = new DateTime();

$fmt = new IntlDateFormatter('en-US', IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
echo "US: ".$fmt->format($d)."<br/>";

$fmt = new IntlDateFormatter('en-GB', IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
echo "GB: ".$fmt->format($d)."<br/>";

$fmt = new IntlDateFormatter('en-AU', IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
echo "AU: ".$fmt->format($d)."<br/>";

$fmt = new IntlDateFormatter('de-DE', IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
echo "DE: ".$fmt->format($d)."<br/>";

Output:

US: 2/1/18
GB: 01/02/2018
AU: 1/2/18
DE: 01.02.18

If you want to change the format of a date on the client side, you can try the toLocaleString function on the Date object in JavaScript. The toLocaleString will change the format based on the client OS's settings for their location. You also would not need to have a table with the country and date format.

This can be done without the need for jQuery or any additional plugin.

本文标签: phpHow to change Date format according to the countryStack Overflow