Help.. Dates & Locales..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adrian Parker

    #1

    Help.. Dates & Locales..

    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(",",$_S ERVER[HTTP_ACCEPT_LAN GUAGE]);

    switch ($split[0]) {
    case "en-us":
    // already US format, so nothing to do
    break;
    case "en-gb":
    // dd/mm/yyyy format
    $lt = substr($lt,3,3) . substr($lt,0,3) . substr($lt,6);
    break;
    case "da":
    case "pt":
    // dd-mm-yyyy format
    $lt = substr($lt,3,2) . "/" . substr($lt,0,2) . "/" . substr($lt,6);
    break;
    }
    $ustime = strtotime($lt);

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

    Thanks
    Adrian


  • Colin McKinnon

    #2
    Re: Help.. Dates & Locales..

    Adrian Parker wrote:
    [color=blue]
    > 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()
    >[/color]

    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(",",$_S ERVER[HTTP_ACCEPT_LAN GUAGE]);

    if ($split[0]=='en-gb') { // might need to add other locales here
    if (ereg("([0-9]{1,2})/([0-9](1,2})/([0-9]{2,4})", $lt, $parts)) {
    unset($regs[0]);
    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));
    }
    }
    $ustime=strtoti me($lt);

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

    HTH

    C.

    Comment

    • Colin McKinnon

      #3
      Re: Help.. Dates &amp; Locales..

      Colin McKinnon wrote:

      [color=blue]
      > }
      > $lt=implode('-', array($yr,$mo,$ da));
      > }[/color]
      whoops, that should be:
      $lt=implode('-', array($regs[3],$regs[2],$regs[1]));

      ....but you knew that already ;)

      C.

      Comment

      Working...