The only way to determine the location on the server side is to use the lookup table for the IP address. There are services that provide this for you, so you can do this:
$ip = !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; $url = "http://freegeoip.net/json/$ip"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); $data = curl_exec($ch); curl_close($ch); if ($data) { $location = json_decode($data); $lat = $location->latitude; $lon = $location->longitude; $sun_info = date_sun_info(time(), $lat, $lon); print_r($sun_info); }
It will not always be very accurate though. In javascript, you will have access to the HTML5 geolocation API or Google Maps, but reverse geocoding with Google requires using a map according to TOS.
NOTE (2019): The service used in the example, FreeGeoIP, was closed, I will leave an answer for posterity, since there are probably other services offering the same service.
source share