I’ve been looking around on how to get the timezone of a specific address. I kinda stumbled upon GeoNames and they actually have an API that can provide the timezone using the longitude and latitude. You can check their web services and learn more about what they offer for free.

To start, create your account at GeoNames, confirm your email address, then go to your manage account page. If you don’t confirm your account, you won’t be able to login. The reason for creating an account here is because we will be needing their vast library in capturing the different addresses.

If you go to your account, you will see this message at the bottom:


Simply enable then you are good to go.

Function

The function uses the cURL library and SimpleXML extension of PHP.

<?php

function dp_get_timezone($latitude,$longitude,$username) {
		//error checking
		if (!is_numeric($latitude)) { return 'invalid latitude'; }
		if (!is_numeric($longitude)) { return 'invalide longitude'; }
		if (!$username) { return 'invalid username'; }

		//connect to web service
		$url = 'http://api.geonames.org/timezone?lat='.$latitude.'&lng='.$longitude.'&style=full&username='.urlencode($username);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$xml = curl_exec($ch);
		curl_close($ch);
		if (!$xml) { $GLOBALS['error'] = 'No data: '.$url; return false; }

		//parse XML response
		$data = new SimpleXMLElement($xml);
		// echo '<pre>'.print_r($data,true).'</pre>'; die();
		$timezone = trim(strip_tags($data->timezone->timezoneId));
		if ($timezone) { return $timezone; }
		else { $GLOBALS['error'] = 'No timezone: '.$url; return false; }
}

?>


For us to use the function, simply call it.

<?php

$timezone = dp_get_timezone($map['lat'],$map['lng'],'username'); 

?>


Change username to the username you created at GeoNames earlier.

$timezone will now contain ‘Asia/Singapore‘.

If you want to get the current date and time using this timezone, we can use it for the DateTime function.

<?php

$now_date = new DateTime('now');
$timezone = dp_get_timezone($map['lat'],$map['lng'],'username');
$now_date->setTimeZone(new DateTimeZone($timezone));
echo $now_date->format('d m Y H:i:s');

?>


Thank you!


Hope this article helps. Good luck!

If you have any additional information or some questions, I’ll be happy to listen. Comment down or simply send me an email.

For more information, you can check these out.

https://inkplant.com/code/get-timezone
https://www.php.net/manual/en/class.datetime.php
https://www.php.net/manual/en/function.geoip-time-zone-by-country-and-region.php


Ryner Galaus

Ryner SG

An ordinary person with a tiny brain hoping to be useful in his own way. A homebody hoping to one day create and build his dreams while in service to people. An underachiever who was once called nobody slowly growing up. A web developer learning and earning his way through life. Ryner is what my family and friends call me, and I hope you do too. By the way, I am also affiliated with Frontrow International, so for inquiries or orders, message me.

1 Comment

What is PHP? - Summary – Ryner World · March 31, 2024 at 11:38 am

[…] Get User Timezone using PHP […]

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *