14

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 to 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...

Exapmle:

$locale = "en_US";
$format = the_function_i_need($locale);
echo $format; // prints something like "month/day/year, hour:minute"
2
  • I agree with @dagon. and setting PHP to follow UTC will make it much easier. Jan 11, 2012 at 22:27
  • 1
    @Dagon I believe your comment is more accurate, you should put it as an answer.
    – Matt K
    Jan 11, 2012 at 22:40

4 Answers 4

11
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.

3
  • To me this is the best answer - universal, short and working. Though those ICU formats seem to be pretty short on possibilities. I'd need something like IntlDateFormatter::SHORT with 4 digit years and leading 0s.
    – userfuser
    May 20, 2017 at 23:24
  • I didn't pick that up from the manual right away (it says "Medium style (Jan 12, 1952)"), but apparently the IntlDateFormatter::MEDIUM is just what I needed (leading zeros, 5-digit year)
    – userfuser
    May 21, 2017 at 6:19
  • this was the correct answer for me in this day and age. modern api, no screwing around with setlocale().
    – glasz
    Feb 19, 2022 at 0:17
3

converted from comment:

you will have to build an array with a list of the possibilities.

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

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

2
  • this is advice, but not answer
    – verybadbug
    Jan 21, 2017 at 13:18
  • i was asked to turn comment in to answer, and so i did.
    – user557846
    Feb 5, 2017 at 6:24
2

I'm trying to do 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()
{
    $patterns = array(
        '/11\D21\D(1999|99)/',
        '/21\D11\D(1999|99)/',
        '/(1999|99)\D11\D21/',
    );
    $replacements = array('mdy', 'dmy', 'ymd');

    $date = new \DateTime();
    $date->setDate(1999, 11, 21);

    return preg_replace($patterns, $replacements, strftime('%x', $date->getTimestamp()));
}
2
  • 2
    Some years later: set locale like above, just skip the getDateFormat function and use strftime("%x", $timestamp)
    – Kjell
    Dec 7, 2018 at 11:05
  • $likes += 1; //Just in time. ;) May 9, 2019 at 22:27
0

Use strftime() in conjunction with setlocale(). Seems like it's what you're looking for.

2
  • Thanks for the answer, but that's the opposite way. If not, I don't follow your idea. Could you write a short example, please?
    – Pavel S.
    Jan 11, 2012 at 22:30
  • 2
    ah, I think I understand now. strftime() will convert the time and language of the output, but for the actual format like "yyyy-mm-dd" in Canada as opposed to "dd/mm/yyyy" in Albania I believe you will have to use Dagon's approach. seems like there should be a function that does this...
    – Matt K
    Jan 11, 2012 at 22:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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