Re: [PHP] Using DateTimeZone
- From: jochem@xxxxxxxxxxxxx (Jochem Maas)
- Date: Thu, 04 Dec 2008 21:28:29 +0100
Lester Caine schreef:
Lester Caine wrote:
Lester Caine wrote:Completing the circle slowly ....
I'm looking to tidy up things a bit by clearing out a lot of old code
and switching to using the internal DateTime functions.
Information is stored in the databases UTC normalized, and we get
around the problem of getting a real tz offset by getting the users
to register it rather than simply relying on the browser. So dates
and times are displayed either UTC or user local time. The server
defaults are not relevant, as only the user details should be used.
OK cracked the first bit - need an '@' in place of the "U"
The thing that I am having a little trouble establishing is thenew DateTime( '@'.$datetime_to_display, $dateTimeZoneUser );
correct way to take a UTC unix epoch timestamp from the data and
display it with the users timezone offset.
$dateTimeZoneUser = new DateTimeZone("user's setting");
$dateTimeUser =
new DateTime( date( "U", $datetime_to_display ), $dateTimeZoneUser );
$dateTimeUser = new DateTime( '@'.$datetime_to_display);
$dateTimeUser->setTimeZone( $dateTimeZoneUser );
$date = $dateTimeUser->format( DATE_ATOM );
But of cause the bit of the jigsaw I forgot is that this needs
translating to the correct language. format does not respect 'setlocale'
setting? How do I get this translated to the users language as well as
their timezone?
DATE_ATOM is a locale independent format AFAICT.
secondly, and I think this is rather odd, DateTime uses the same
formatting code as date() ... which only does english, to quote:
<quote from=http://php.net/date>
To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().
</quote>
and seeing as you don't seem to be able to retrieve the timestamp
that the DateTime object represents in order to feed it to strftime()
you *** out of luck. ... well you could do something like the following
but it makes me feel like maybe one should avoid DateTime until
it's somewhat more flexible/complete:
<?php
setlocale(LC_TIME, "nl_NL.ISO8859-1");
$d = new DateTime();
echo strftime("%A, %d %B %Y", strtotime($d->format(DATE_ATOM))), "\n";
?>
.
- Follow-Ups:
- Re: [PHP] Using DateTimeZone
- From: Lester Caine
- Re: [PHP] Using DateTimeZone
- References:
- Using DateTimeZone
- From: Lester Caine
- Re: [PHP] Using DateTimeZone
- From: Lester Caine
- Re: [PHP] Using DateTimeZone
- From: Lester Caine
- Using DateTimeZone
- Prev by Date: Re: PHP Basics
- Next by Date: Re: [PHP] COM and the PHP equivalent of ASP Currency data type
- Previous by thread: Re: [PHP] Using DateTimeZone
- Next by thread: Re: [PHP] Using DateTimeZone
- Index(es):