473,396 Members | 2,020 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,396 software developers and data experts.

Date formats for locale

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":
break;
case "en-gb":
$lt = substr($lt,3,3) . substr($lt,0,3) . substr($lt,6);
break;
case "da":
case "pt":
$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 any easier way ?

Thanks
Adrian


Jul 17 '05 #1
3 3867
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 http://pear.php.net/package/Date ?
(please let me know if you find a workaround for it's dependency on
setLocale based functions ;-) )

Greetings,

Henk Verhoeven,
www.pphPeanuts.org.

// 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:
http://www.phppeanuts.org/site/index...Converter.html
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()

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":
break;
case "en-gb":
$lt = substr($lt,3,3) . substr($lt,0,3) . substr($lt,6);
break;
case "da":
case "pt":
$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 any easier way ?

Thanks
Adrian



Jul 17 '05 #2
Henk,

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
php.

-Adrian
"Henk Verhoeven" <ne***@phppeanutsREMOVE-THIS.org> wrote in message
news:cl**********@news3.zwoll1.ov.home.nl...
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 http://pear.php.net/package/Date ?
(please let me know if you find a workaround for it's dependency on
setLocale based functions ;-) )

Greetings,

Henk Verhoeven,
www.pphPeanuts.org.

// 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:
http://www.phppeanuts.org/site/index...Converter.html
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()

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":
break;
case "en-gb":
$lt = substr($lt,3,3) . substr($lt,0,3) . substr($lt,6);
break;
case "da":
case "pt":
$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 any easier way ?

Thanks
Adrian


Jul 17 '05 #3
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).

Greetings,

Henk Verhoeven.

http://lists.gnu.org/archive/html/bu.../msg00006.html

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.

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ray | last post by:
Date problem. Using non American date formats with Microsoft Access and Visual Basic. I set my regional settings to English(Ireland) or English(United Kingdom) and my date appears in Access as...
2
by: monki | last post by:
hi i am working with uk date formats (dd/mm/yy) however the server the site is hosted on is in the us(mm/dd/yy) format i am using the following to convert the ddates gigdateuk =...
6
by: Suzanne | last post by:
Hi, I am having problems with my website which uses ASP. It is switching between American and European date formats - this is causing problems on the page which use the Date function. Most of...
1
by: OKI | last post by:
Hi. I had a VB program that was using an SQL Server 6.5 DB. Date format was dd/mm/yyyy. Now the DB was changed to an SQL Server 7 and the date format is yyyy/mm/dd. How can I do for changing...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
1
by: Vilen | last post by:
Hi. I have a DB in which we store dates in yyyy/mm/dd. However when we want to display this date via a web frontend, it needs to be in dd/mm/yyyy. I've declared a function (shown below) which...
2
by: ABC | last post by:
Hi, All Which function return the desktop long and short date formats? Thanks
1
by: Brendan Reynolds | last post by:
In an ASP.NET 1.1 app I have the following range validation control. This is an intranet app that will be used only within Ireland, so all date input is expected to be in dd/mm/yyyy format. ...
2
by: gstark | last post by:
Hi, I have a difference between machines in the date formats they produce. This one web application has this code. Me.txtRxDate.Text = Now().ToShortDateString. On the development server, the...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.