Wednesday 5 November 2008

World Time display in PHP and Javascript

We have a world clock on the wall in the office, displaying the times in 24 different world cities. Unfortunately, it's a (comparitively) cheap model that doesn't know about daylight saving time. (This may be a blessing - it certainly wouldn't know about rule changes for DST.) As a result, it ends up being an hour out of sync with the actual time in various cities. Whilst it's nice to know that the time in Anchorage might be 12:34, I figured it'd be more helpful to know what the time actually is.

Checking up on the displayed times manually seemed like a job only mildly more enjoyable than reading the thing's one-page user manual (which seems to have been translated from Chinese into English via Sanskrit by translators with only a passing knowledge of Chinese, English, Sanskrit or the concept of timekeeping). The quickest way would probably have been entering "time now <city>" into Google for each of the 24 cities. Instead, I've made a world clock script which shows me the current time in all 24 cities.

The script uses PHP to output (hopefully) correct timezone data. It uses JavaScript for all of the actual calculation. I'd have preferred using pure JavaScript, but JS doesn't give you data about timezone offsets other than for your own timezone. The script contains a list of cities, with name, latitude, longitude and timezone and places them automatically on the map, so it's easily customisable for anyone who has a similar need. The source and the world map it uses are linked below.

PHP/Javascript world clock script source
World map

No comments: