Řešení časových zón od Ivana Doležala
Z VirtlabWiki
V MySQL mam cas jako typ datetime v UTC. Casovou zonu si uzivatel nastavi pomoci vhodne volby v menu (i kdyz, jak ted googluji, da se zrejme vycist ruznymi zpusoby i z prohlizece) a dale se udrzuje v cookie:
<form action="changezone.php"> <?php printf('<select title="%s" name="selecttimezone">',msg_get_str('SELECT_TIME_ZONE')); foreach( tz_get_zone_list() as $key => $value ) { printf(" <option %s value=\"%s\">%s</option>\n",(tz_get_tz()==$key)?'selected':'',$key,$value); } ?> </form>
a v changezone.php nasledne
tz_set_tz($_REQUEST['selecttimezone']);
kdyz si chci vypsat nejaky cas, provadim v SQL:
select UNIX_TIMESTAMP(liveshow.starttime) as zacatek
a ziskanou hodnotu pak vypisu napriklad pomoci:
tz_strftime("%H:%M",$dennidata['zacatek'])
Kdyz chci vlozit cas, provadim zhruba toto:
$sql=sprintf( "REPLACE INTO liveshow SET starttime=from_unixtime(%s)", tz_mktime($hour,$min,$month,$day,$year))
Je potreba do OS nainstalovat podporu vsech myslitelnych casovych zon, v Debianu jde o jedno
dpkg-reconfigure locales
Doporucuji si ale pak pro kontrolu vypsat seznam vygenerovanych locales
locale -a
global $tz_preffered_tz;
// Check out /usr/share/zoneinfo/
$tz_timezones_list = array(
'HST' => 'HST',
'PST8PDT' => 'PST',
'MST7MDT' => 'MST',
'CST6CDT' => 'CST',
'EST5EDT' => 'EST',
'Universal'=>'UTC',
'WET' => 'WET',
'CET' => 'CET',
'EET' => 'EET',
'W-SU' => 'MSK',
'Singapore'=>'SGT',
'ROK' => 'KST',
'NZ' => 'NZT',
'NZ-CHAT' => 'CHAT'
);
function tz_get_zone_list()
{
global $tz_timezones_list;
return $tz_timezones_list;
}
function tz_name_timezone( $code )
{
global $tz_timezones_list;
if( array_key_exists($code, $tz_timezones_list) )
return $tz_timezones_list[$code];
else
return $code;
}
function tz_set_tz( $zone ) {
if( in_array( $zone ,array_keys( tz_get_zone_list() )) )
{
putenv('TZ='.$zone);
$GLOBALS["tz_preffered_tz"] = $zone;
@setcookie('preffered_tz', $zone, 2100000000, '/', $_SERVER['HTTP_HOST'], 0 );
}
else
{
printf('%s - tz_set_tz(%s)',msg_get_str(TEMP_ERROR), $zone);
}
}
function tz_get_tz() {
if( isset($_COOKIE['preffered_tz']) ) {
$GLOBALS["tz_preffered_tz"] = $_COOKIE['preffered_tz'];
}
else
{
if( ! isset($GLOBALS["tz_preffered_tz"]))
$GLOBALS["tz_preffered_tz"] = 'CET';
}
return $GLOBALS["tz_preffered_tz"];
}
function tz_mktime( $hour, $minute, $second, $month, $day, $year )
{
putenv('TZ='.tz_get_tz());
return mktime( $hour, $minute, $second, $month, $day, $year );
}
function tz_strftime( $source, $now )
{
putenv('TZ='.tz_get_tz());
setlocale(LC_TIME,$_COOKIE['preffered_language'].'.UTF-8');
return strftime( $source, $now );
}