Get Date Format According To The Locale In Php

There is a very simple problem. I have a locale identifier, en, en_US, cs_CZ or whatever. I just need to get the date-time format for that locale. I know I can easily format any timestamp or date object according khổng lồ the locale. But I need just the string representation of the date format, let"s say a regular expression. Is there any function managing this functionality? I haven"t found any so far...

Bạn đang xem: Get date format according to the locale in php

Exapmle:

$locale = "en_US";$format = the_function_i_need($locale);echo $format; // prints something lượt thích "month/day/year, hour:minute"

*

function getDateFormat($locale) $formatter = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::NONE); if ($formatter === null) throw new InvalidConfigException(intl_get_error_message()); return $formatter->getPattern();Make shure you install intl.


*

converted from comment:

you will have to lớn build an array with a các mục of the possibilities.

http://en.wikipedia.org/wiki/Date_format_by_country should help.

Xem thêm: cấp ẩm cho da dầu

post the function somewhere when done, i"m sure it would come in handy for others


I"m trying to vì the same thing myself. Instead of building an array of all the countries, how about using regex to determine the format?

setlocale(LC_TIME, "us_US");// returns "mdy"$type1 = getDateFormat();setlocale(LC_TIME, "fi_FI");// returns "dmy"$type2 = getDateFormat();setlocale(LC_TIME, "hu_HU");// returns "ymd"$type3 = getDateFormat();/** *
return string */function getDateFormat()99)D11D21/", ); $replacements = array("mdy", "dmy", "ymd"); $date = new DateTime(); $date->setDate(1999, 11, 21); return preg_replace($patterns, $replacements, strftime("%x", $date->getTimestamp()));

*

*

Thanks for contributing an answer to lớn Stack Overflow!

Please be sure khổng lồ answer the question. Provide details và share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.


Post Your Answer Discard

By clicking “Post Your Answer”, you agree khổng lồ our terms of service, privacy policy và cookie policy


Not the answer you're looking for? Browse other questions tagged php datetime date format or ask your own question.


*

site thiết kế / logo sản phẩm © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. Rev2022.2.16.41461


Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device & disclose information in accordance with our Cookie Policy.