Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Date format according to the country

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

http://en.wikipedia.org/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 ...

like image 897
Athanatos Avatar asked Jan 23 '26 16:01

Athanatos


2 Answers

For PHP, this should be a good start: http://php.net/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).

like image 149
Bart Friederichs Avatar answered Jan 25 '26 04:01

Bart Friederichs


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
like image 40
Zoltán Süle Avatar answered Jan 25 '26 05:01

Zoltán Süle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!