The Storyteller

WorldTimeEngine – How about making your own in PHP?

March 1, 2008 · 8 Comments

I recently came by this site WorldTimeEngine where users can search the local time of any place using the name, street address or just latitude and longitude. Since that time I was thinking how easily you can make your own. As long there are some good people over there (For Geocoding API) – its a not a big deal, you know? Lets have a look at the following PHP code.


<?
$place = "Bucharest";

$geodata = getGeoCode($place);
print_r($geodata);
echo getTime($geodata['lat'],$geodata['lng']);

function getTime($lat, $lng)
{
$url = "http://ws.geonames.org/timezone?lat={$lat}&lng={$lng}";
$timedata = file_get_contents($url);
$sxml = simplexml_load_string($timedata);
return $sxml->timezone->time;
}
function getGeoCode($address)
{
$_url = 'http://api.local.yahoo.com/MapsService/V1/geocode';
$_url .= sprintf('?appid=%s&location=%s',"orchid_geocode",rawurlencode($address));
$_result = false;
if($_result = file_get_contents($_url)) {
preg_match('!<Latitude>(.*)</Latitude><Longitude>(.*)</Longitude>!U', $_result, $_match);
$lng = $_match[2];
$lat = $_match[1];
return array("lat"=>$lat,"lng"=>$lng,"address"=>$address);
}
else
return false;
}
?>

Changing the variable “$place” to “Bucharest”,”Dhaka”, “Toronto” and “Oslo” gives me the following result


Array
(
[lat] => 44.434200
[lng] => 26.102955
[address] => Bucharest
)
2008-03-01 08:41

Array
(
[lat] => 23.709801
[lng] => 90.407112
[address] => Dhaka
)
2008-03-01 12:42

Array
(
[lat] => 43.648565
[lng] => -79.385329
[address] => Toronto
)
2008-03-01 01:42

Array
(
[lat] => 59.912280
[lng] => 10.749980
[address] => Oslo
)
2008-03-01 07:43

Nice, Huh?

Categories: OpenSource · PHP · howto · performance

8 responses so far ↓

  • Raquibul Islam // March 3, 2008 at 1:28 pm | Reply

    Nice :) i’m gonna try it now.

  • ranacse05 // March 3, 2008 at 1:45 pm | Reply

    রিজাল্টটা অণ্য ভাবে দেখালে ভালো হত । :)

  • Geoff // March 3, 2008 at 8:01 pm | Reply

    Google has an API for this, too:
    http://www.google.com/search?q=time:bucharest

  • hasin // March 3, 2008 at 11:37 pm | Reply

    @geoff

    Thats true only for known places. And you cannot search time by Lat and Lng

  • valugi // March 6, 2008 at 2:36 pm | Reply

    You can achive the same result using the google maps Api. Actually microsoft has also a maps service and I think they also offer geocoding service. I prefer the googleMaps api.

  • Hasin Hayder’s Blog: WorldTimeEngine - How about making your own in PHP? | Cole Design Studios // March 11, 2008 at 3:07 am | Reply

    [...] Hayder has posted an example of a “world time search” he’s worked up that uses the geonames.org and Yahoo! [...]

  • Nikita // August 15, 2009 at 1:37 pm | Reply

    I’ve found another idea, how to use WorldTimeEngine to forget time-zone issues much more friendly for the users.

  • Nikita // August 15, 2009 at 1:37 pm | Reply

    I’ve found another idea, how to use WorldTimeEngine to forget time-zone issues much more friendly for the users.

    http://realitydrivendeveloper.com/2009/08/worldtimeengine-how-to-show-all-times-in-users-local-time-zone/

Leave a Comment