473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 1753
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: garykpdx | last post by:
Is there some sort of tutorial on locales or the locale module? I can't seem to find a list showing all possible locales. I made 'en' work alright, but when I tried 'de' or 'de_DE' or 'es_ES',...
7
by: mike | last post by:
Hello, I am kind of new to this javascript stuff and I am constantly having problems trying to get my webpage validated. I have the following <script>printdate();</script> and when I validate it...
2
by: Kenneth | last post by:
Hi, I have installed VS.NET 2000 on an english version of windows 2000 professional and in control panel I've chosen Regional Options and on the general Tab I have Swedish in Your...
0
by: news | last post by:
This had me stumped for ages yesterday, and since I solved it and can't find much reference to the problem, I thought I'd post here. I have various vbscript ASP pages that take an ADO recordset...
3
by: Phoe6 | last post by:
I would like to parse RFC 1123 date format and compare two dates. I find that datetime module does not specifically confirms to any RFC. Any suggestions as how I can handle the RFC 1123 date...
30
by: Alf P. Steinbach | last post by:
I once suggested in that SomeOne Else(TM) should propose a string value class that accepted literals and char pointers and so on, with possible custom deleter, and in case of literal strings just...
11
by: James Kanze | last post by:
On Apr 11, 6:48 am, Jerry Coffin <jerry.cof...@gmail.comwrote: The combination of the two. *IF* it is appropriate to use a container here, that container
13
by: Soumen | last post by:
I wanted convert a mixed case string to a lower case one. And I tried following code: std::transform(mixedCaseString.begin(), mixedCaseString::end(), mixedCaseString.begin(),...
2
by: mathieu | last post by:
Hi, I am playing with the following C++ piece of code (*). At least on my system debian/gcc 4.3 it looks like I am not writing out a floating point separator as a comma. what are the operation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.