470,833 Members | 1,447 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

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(",",$_SERVER[HTTP_ACCEPT_LANGUAGE]);

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
Jul 17 '05 #1
2 1612
Adrian Parker wrote:
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()


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]{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=strtotime($lt);

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

HTH

C.
Jul 17 '05 #2
Colin McKinnon wrote:

}
$lt=implode('-', array($yr,$mo,$da));
}

whoops, that should be:
$lt=implode('-', array($regs[3],$regs[2],$regs[1]));

....but you knew that already ;)

C.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by garykpdx | last post: by
7 posts views Thread by mike | last post: by
2 posts views Thread by Kenneth | last post: by
3 posts views Thread by Phoe6 | last post: by
30 posts views Thread by Alf P. Steinbach | last post: by
11 posts views Thread by James Kanze | last post: by
13 posts views Thread by Soumen | last post: by
2 posts views Thread by mathieu | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.