Help.. Dates & Locales..

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
// $lt contains the date in the client format

$split = explode(",",$_SERVER[HTTP_ACCEPT_LANGUAGE]);

switch ($split[0]) {
  case "en-us":
        // already US format, so nothing to do
 case "en-gb":
      // dd/mm/yyyy format
      $lt = substr($lt,3,3) . substr($lt,0,3) . substr($lt,6);
 case "da":
 case "pt":
      // dd-mm-yyyy format
      $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 an easier way ?


Re: Help.. Dates & Locales..

Adrian Parker wrote:

Quoted text here. Click to load it

strtotime is quite flexible and will certainly accomodate US and ISO type
dates although (unless I'm doing something wrong) it seems to ignore
completely the LOCALE setting. This really only becomes a problem when its
parsing a date written in dd/mm/... format (as in UK). There may also be
problems with month *names* (jun, June etc) in non-english languages - I
haven't tested this yet.

All you need to do is account for the UK formatting:

$split = explode(",",$_SERVER[HTTP_ACCEPT_LANGUAGE]);

if ($split[0]=='en-gb') { // might need to add other locales here
        if (ereg("([0-9])/([0-9](1,2})/([0-9])", $lt, $parts)) {
                if ($regs[3]<100) {
                        if ($regs[3]>50) {
                                $regs[3]='19' . $regs[3];
                        } else {
                                $regs[3]='20' . $regs[3];
                $lt=implode('-', array($yr,$mo,$da));

But I'd be interested in hearing about other solutions / libraries.
Particularly with good international support.



Re: Help.. Dates & Locales..

Colin McKinnon wrote:

Quoted text here. Click to load it
whoops, that should be:
                $lt=implode('-', array($regs[3],$regs[2],$regs[1]));

....but you knew that already ;)


Site Timeline