473,587 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 3880
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 initFormatsAndS eparators () {
$this->dateformats["en-us"] = "mm/dd/yyyy";
$this->dateSeparato rs["en-us"] = '/'.
$this->dateformats["du-nl"] = "yyyy-mm-dd";
$this->dateSeparato rs["du-nl"] = '-'.
}
now you can split a date like this:

function splitDT($value, $locale)
{
$expr = '['.$this->dateSeparato rs[$locale].']';
$arr = split($expr, $value);
$formatArray = split($expr, $this->dateformats[$locale]);
for ($i=0; $i<count($forma tArray); $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($spl ittedDate, $locale)
{
$result = $this->dateFormats[$locale];
forEach($splitt edDate as $key => $value)
$result = str_replace($ke y, $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(",",$_S ERVER[HTTP_ACCEPT_LAN GUAGE]);

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***@phppeanu tsREMOVE-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 initFormatsAndS eparators () {
$this->dateformats["en-us"] = "mm/dd/yyyy";
$this->dateSeparato rs["en-us"] = '/'.
$this->dateformats["du-nl"] = "yyyy-mm-dd";
$this->dateSeparato rs["du-nl"] = '-'.
}
now you can split a date like this:

function splitDT($value, $locale)
{
$expr = '['.$this->dateSeparato rs[$locale].']';
$arr = split($expr, $value);
$formatArray = split($expr, $this->dateformats[$locale]);
for ($i=0; $i<count($forma tArray); $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($spl ittedDate, $locale)
{
$result = $this->dateFormats[$locale];
forEach($splitt edDate as $key => $value)
$result = str_replace($ke y, $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(",",$_S ERVER[HTTP_ACCEPT_LAN GUAGE]);

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_LANGI NFO): 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
3684
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 DD/MM/YYYY. However if the day is less than 12 the day then becomes the month e.g. 3 July 2003 is displayed in Visual Basic as 7 March 2003. So if...
2
4816
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 = day(rs1("Date")) & "/" & month(rs1("Date")) & "/" & year(rs1("Date"))%> Response.Write gigdateuk
6
2520
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 the time the page works ok but sometimes it seems to switch formats and this messes up the code on the page. I have spoke with the people who...
1
2359
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 date formats in the new DB to the old format? I´m searching for a database level solution, not server level.
4
5364
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 calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm...
1
1419
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 converts between these date formats and returns a varchar(20). This works fine however now I need to have the ability to sort on this date field in the...
2
1451
by: ABC | last post by:
Hi, All Which function return the desktop long and short date formats? Thanks
1
4685
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. <asp:RangeValidator id="varngRollCallDate" runat="server" ErrorMessage="Please enter a valid date in 'dd/mm/yyyy' format." ControlToValidate="txtDate"...
2
1236
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 browser displays the date as: 2/27/2007 When I goto the production server, the same browser displays the date as: 2/27/07. Where is this controled?...
0
7920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7973
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5718
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.