This is a discussion on Using DateTimeZone within the PHP General forums, part of the PHP Programming Forums category; I'm looking to tidy up things a bit by clearing out a lot of old code and switching to ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. The thing that I am having a little trouble establishing is the 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 ); $date = $dateTimeUser->format( DATE_ATOM ); Is giving results wildly different from 2008 So how is one supposed to handle epoch values? I little aside while sorting the lookup table for timezones, DateTimeZone::listIdentifiers() apparently should accept a country code, but again I can't seem to find out how to make it work. Would I expect it to return just values for the country requested? Lester Caine - G8HFL ----------------------------- Contact - http://lsces.co.uk/lsces/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/ Model Engineers Digital Workshop - http://medw.co.uk// Firebird - http://www.firebirdsql.org/index.php |
|
|||
|
Lester Caine wrote:
> 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 the 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 ); new DateTime( '@'.$datetime_to_display, $dateTimeZoneUser ); > $date = $dateTimeUser->format( DATE_ATOM ); > > Is giving results wildly different from 2008 > So how is one supposed to handle epoch values? That gives me the correct date - but changing the timezone makes no difference to the output - seems to be displaying local timezone only. > I little aside while sorting the lookup table for timezones, > DateTimeZone::listIdentifiers() apparently should accept a country code, > but again I can't seem to find out how to make it work. Would I expect > it to return just values for the country requested? -- Lester Caine - G8HFL ----------------------------- Contact - http://lsces.co.uk/lsces/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/ Model Engineers Digital Workshop - http://medw.co.uk// Firebird - http://www.firebirdsql.org/index.php |
|
|||
|
Lester Caine wrote:
> Lester Caine wrote: >> 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 the >> 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 ); > new DateTime( '@'.$datetime_to_display, $dateTimeZoneUser ); Completing the circle slowly .... $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? >> Is giving results wildly different from 2008 >> So how is one supposed to handle epoch values? > That gives me the correct date - but changing the timezone makes no > difference to the output - seems to be displaying local timezone only. > >> I little aside while sorting the lookup table for timezones, >> DateTimeZone::listIdentifiers() apparently should accept a country >> code, but again I can't seem to find out how to make it work. Would I >> expect it to return just values for the country requested? -- Lester Caine - G8HFL ----------------------------- Contact - http://lsces.co.uk/lsces/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/ Model Engineers Digital Workshop - http://medw.co.uk// Firebird - http://www.firebirdsql.org/index.php |
|
|||
|
Lester Caine schreef:
> Lester Caine wrote: >> Lester Caine wrote: >>> 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 the >>> 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 ); >> new DateTime( '@'.$datetime_to_display, $dateTimeZoneUser ); > Completing the circle slowly .... > $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 shit 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"; ?> |
|
|||
|
Jochem Maas wrote:
> Lester Caine schreef: >> Lester Caine wrote: >>> Lester Caine wrote: >>>> 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 the >>>> 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 ); >>> new DateTime( '@'.$datetime_to_display, $dateTimeZoneUser ); >> Completing the circle slowly .... >> $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. Documenting that may be useful ;) But I switched to other formats and the bottom line seems to be that like DATE_ATOM - '@'. is timezone independent, so one has to apply the required timezone AFTER loading the 'epoch'. $dateTimeZoneUser is only used if there is no 'timezone' information contained IN the supplied time? More missing information from the documentation? > 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> Yep ... a right pain converting from ADOdb Date structure and format Along with the problem that DateTime does not handle genealogical dates either - which ADOdb::Date does. > 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 shit 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: The problem is how to add daylight saving *TO* ADOdb dates That is without breaking anything else. > <?php > > setlocale(LC_TIME, "nl_NL.ISO8859-1"); > $d = new DateTime(); > echo strftime("%A, %d %B %Y", strtotime($d->format(DATE_ATOM))), "\n"; > > ?> BUT what I need is the GMT/BST flag - or the equivalent in that local :( I'm structuring a question on internals about the things that do not work right in DateTime ;) -- Lester Caine - G8HFL ----------------------------- Contact - http://lsces.co.uk/lsces/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/ Model Engineers Digital Workshop - http://medw.co.uk// Firebird - http://www.firebirdsql.org/index.php |
|
|||
|
Lester Caine wrote:
>> <?php >> >> setlocale(LC_TIME, "nl_NL.ISO8859-1"); >> $d = new DateTime(); >> echo strftime("%A, %d %B %Y", strtotime($d->format(DATE_ATOM))), "\n"; >> >> ?> Does not work! Have to do date_default_timezone_set( $user_timezone ) ); if ( is_numeric( $user_time )) { $dateTimeUser = new DateTime( '@'.$user_time ); } else { $dateTimeUser = new DateTime( $user_time ); } $disptime = strtotime($dateTimeUser->format(DATE_W3C)); echo strftime( $format, $disptime ); Even to get the right day! But as yet I have not seen ANYTHING other than English text :( The next battle is to get it translated. -- Lester Caine - G8HFL ----------------------------- Contact - http://lsces.co.uk/lsces/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/ Model Engineers Digital Workshop - http://medw.co.uk// Firebird - http://www.firebirdsql.org/index.php |
|
|||
|
Lester Caine schreef:
> Lester Caine wrote: >>> <?php >>> >>> setlocale(LC_TIME, "nl_NL.ISO8859-1"); >>> $d = new DateTime(); >>> echo strftime("%A, %d %B %Y", strtotime($d->format(DATE_ATOM))), "\n"; >>> >>> ?> > > Does not work! it does, only there is nothing setting the timezone in that example. > Have to do > > date_default_timezone_set( $user_timezone ) ); > if ( is_numeric( $user_time )) { > $dateTimeUser = new DateTime( '@'.$user_time ); > } else { > $dateTimeUser = new DateTime( $user_time ); > } > $disptime = strtotime($dateTimeUser->format(DATE_W3C)); > echo strftime( $format, $disptime ); > > Even to get the right day! > > But as yet I have not seen ANYTHING other than English text :( > The next battle is to get it translated. you have to use setlocale() with the relevant locale for the translation your looking for before strftime() will give up something in english. don't pity you in your date battles, date's are a pita :-P > |