Forum Moderators: coopster

Message Too Old, No Replies

Display country Local Time through php

Display country Local Time through php

         

qasimali82

6:30 am on Jul 30, 2007 (gmt 0)

10+ Year Member



Hello Every One!

I want to display local time after user select his timezone. I am having trouble doing it. Have you got ideas of how to approach for a solution to this.

I hope i a able to convey my problem to you guys.

Regards,
Qasim Ali

appi2

9:23 am on Jul 30, 2007 (gmt 0)

10+ Year Member



This is just a cut and paste with some bits added from php.net [uk2.php.net] sorry lost the exact link!
Works in php >5, don't know about php <5.
Should get you going ;)

<form action="<?php $_SERVER['PHP_SELF'];?>">
<select name="timezonechoice" size="10"><?php
function timezonechoice($selectedzone) {
$all = timezone_identifiers_list();
$i = 0;
foreach($all AS $zone) {
$zone = explode('/',$zone);
$zonen[$i]['continent'] = $zone[0];
$zonen[$i]['city'] = $zone[1];
$i++;
}
asort($zonen);
foreach($zonen AS $zone) {
extract($zone);
if($continent == 'Africa' OR $continent == 'America' OR $continent == 'Antarctica' OR $continent == 'Arctic' OR $continent == 'Asia' OR $continent == 'Atlantic' OR $continent == 'Australia' OR $continent == 'Europe' OR $continent == 'Indian' OR $continent == 'Pacific') {
if(!isset($letztercontinent)) $structure .= '
<optgroup label="'.$continent.'">'; // continent
elseif($letztercontinent!=$continent) $structure .= '</optgroup>
<optgroup label="'.$continent.'">'; // continent
if($city!='') $structure .= "
<option ".((($continent.'/'.$city)==$selectedzone)?'selected="selected "':'')." value=\"".($continent.'/'.$city)."\">".str_replace('_',' ',$city)."</option>"; //Timezone
else $structure .= "
<option ".(($continent==$selectedzone)?'selected="selected "':'')." value=\"".$continent."\">".$continent."</option>"; //Timezone
$letztercontinent = $continent;
}
}
$structure .= '
</optgroup>';
return $structure;
}
echo timezonechoice('Europe/Berlin');
?></select>
<input type="submit">
</form>
<?php
echo $timezonechoice;

date_default_timezone_set("$timezonechoice");


$now = time();

// print_r( localtime(time(),true) );
// print_r( getdate() );

print date(" H:i:s");
print date(" T");

?>

qasimali82

10:01 am on Jul 30, 2007 (gmt 0)

10+ Year Member



Thanks apii2!

Event thought the example was not working but i got the solution. So i wanted to share with u. In the mean while i posted this topic i also found the same thing as you did.

date_default_timezone_set('Asia/Calcutta');
$now = time();

print_r( localtime(time(),true) );
print_r( getdate() );

print date("H:i:s");
print date("T");

This is working great. just need to change the parameter of the funtion date_default_timezone_set($timezone).

The list of timezones can be found here [ch2.php.net...]

This function works with php 5.1 and greater as was stated.

Thanks appi2