Date formats for locale

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a server app which is sent a date in the local format of the client
machine.  I've no control over the client app at all, so have to code at the
server end to cope with any problems.   The server is in the US, so any
dates need to be in US format before they will convert using strtotime()

Here is what i'm doing at the moment..

// need to convert date to use USA mm/dd/yyyy format on server
$split = explode(",",$_SERVER[HTTP_ACCEPT_LANGUAGE]);

switch ($split[0]) {
 case "en-us":
 case "en-gb":
  $lt = substr($lt,3,3) . substr($lt,0,3) . substr($lt,6);
 case "da":
 case "pt":
  $lt = substr($lt,3,2) . "/" . substr($lt,0,2) . "/" . substr($lt,6);
$ustime = strtotime($lt);

As you can see, the switch could get huge..  Is there any easier way ?


Re: Date formats for locale

Hi Adrian,

Suppose you have a DateFormatter class, then you could define an array
with a date format for each locale, and one with separators, like:

function initFormatsAndSeparators () {
     $this->dateformats["en-us"] = "mm/dd/yyyy";
     $this->dateSeparators["en-us"] = '/'.
     $this->dateformats["du-nl"] = "yyyy-mm-dd";
     $this->dateSeparators["du-nl"] = '-'.
now you can split a date like this:

function splitDT($value, $locale)
     $expr = '['.$this->dateSeparators[$locale].']';
     $arr = split($expr, $value);
     $formatArray = split($expr, $this->dateformats[$locale]);
     for ($i=0; $i<count($formatArray); $i++)
         $result[$formatArray[$i]] = $arr[$i];
     return $result;

This gives you an array like:
array('yyyy' => '2004', 'mm' => '7', 'dd' => '18')

formatting suchg a splitted date into another format can then be done like:

function formatDate($splittedDate, $locale)
     $result = $this->dateFormats[$locale];
     forEach($splittedDate as $key => $value)
    $result = str_replace($key, $value, $result);
     return $result;

Of course there will be more to it then this, for example you may want
to convert 'mm' => '7' into 'mm' => '07' and check that the value for
'mm' is no larger then 12. Or you might want to process month names.
This will probably lead to a case switch with a case for 'mm' and one
for for 'yyyy' etc. that you run through while looping through the
$splittedDate array. But at least that case switch will be a lot smaller
then a case for each and every locale.

BTW, did you check out ?
(please let me know if you find a workaround for it's dependency on
setLocale based functions ;-) )


Henk Verhoeven,

// Copyright (c) the partners of MetaClass, 2003, 2004
// Licensed under the Academic Free License version 2.0

For working code (but no direct support of locale's) see:

Adrian Parker wrote:

Quoted text here. Click to load it

Re: Date formats for locale


Yeah, I know I can do what you suggest, it's more the fact of having to
specify the format for all possible locales which seems to be a huge hole in


Quoted text here. Click to load it
Quoted text here. Click to load it

Re: Date formats for locale

Hi Adrian,

Sorry for the late reaction (working in phpPeanuts AND meeting a project
deadline...). I did some googling, maybe the following leads to some
parsable open source locale definitions (see below).


Henk Verhoeven.

I'm writing to suggest that the default output format of 'date' (when
| neither '-R' nor '-I' option is given) has to be made locale-dependent
| using gettext().
| date.c (in sh-utils-2.0) has the following:

Thanks for the suggestion.
That problem was addressed by this change:

2000-03-29  Paul Eggert

         * src/date.c: Include <langinfo.h> if it exists.
         (DATE_FMT_LANGINFO): New macro.
         (show_date): Use it to get the locale-specific default format for
         "date" if it exists.

Site Timeline