Forum Moderators: coopster

Message Too Old, No Replies

PHP to display timezone/offset on webpage

         

Asia_Expat

9:15 am on Oct 12, 2009 (gmt 0)

10+ Year Member



I understand there were some changes in PHP 5 regarding the date functionality. If anyone has the time, I'd like an idiots guide to displaying the time for each country section of my website. My server is in Singapore and set to Singapore time... but I want to display the current time in Hong Kong for example at the top of the Hong Kong section.

I'm also interested in having a line of clocks next to each other i.e. showing the time in London, New York, Paris, Bangkok etc etc.... how could I achieve this?

Note that I don't want to use Javascript and I'm not bothered if the time doesn't update browser side.

andrewsmd

4:06 pm on Oct 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would just manually figure out how many hours forward/back you need to go for each given time zone and create a function that will return that time based on an input using the strtotime function. Something like this

//this function takes in a string
//time zone and returns the current time
//for that. right now it takes in either
//yourZone1 and yourZone2 but you could
//modify these to take in whatever string
//makes the most sense for you. i.e. london/england
//or usCentralTime
function getTime($timeZone){

//this is assuming that you have figured
//out that that yourZone1 is the current
//time on your php server + 4 hours
if($timeZone == "yourZone1"){

return("The current time for $timeZone is " . date("Y-m-d H:i:s", strtotime("+4 hours")));

}//if

//this is assuming that you have figured
//out that that yourZone2 is the current
//time on your php server - 3 hours
else if($timeZone == "yourZone2"){

return("The current time for $timeZone is " . date("Y-m-d H:i:s", strtotime("-3 hours")));

}//if
//else you didn't pass in a correct time
else{
return("You didn't pass in a time that I know.");
}

}//getTime

Jonesy

6:23 pm on Oct 12, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



putenv("TZ=Asia/Hong_Kong");

The rest of the script runs in Hong Kong time.
Or, change it again in mid-script, if you need to.

Read The Fine Manual: getenv() , putenv()

Don't simply use aritmetic and 'how many hours forward/back you need to go' to move from timezone to timezone. You will get bitten by various Daylight Savings, Summer Time, etc. switches.

Jonesy

Asia_Expat

10:51 am on Oct 14, 2009 (gmt 0)

10+ Year Member



Hmmm... looks convoluted and confusing. Thanks for the comments, I'll get it figured.