How to get time zone from user address

I have a booking system in cakephp where the time is about gmt +10 (eastern australia) and i need to alter the time from where the user resides in the world. The user supplies the address.

How do i find the users time zone so i can alter the booking time in my time to theirs.

$adjstime=date(“h:i a”,(strtotime ($changetime , strtotime ( $item->start_time) ) ));

Use the Google API? There’s nothing built into standard PHP that would be able to do this for you.

yes i saw this but in cakephp 3.X how do i install this ? as a helper?

i need to access this in a controller which i am unsure how to do this? i have a js function already but i need php code not js code

this is for cakephp 2.X only and not in a controller

$address=$student[‘address_street’].’ ‘.$student[‘address_suburb’].’ ‘.$student[‘address_postcode’].’ ‘.$student[‘address_state’];
//the below doesnt get data and it needs to
$latlong=‘https://maps.googleapis.com/maps/api/geocode/json?address=’. $address .’?key=myapikey’;

Google maps geo-coding (get lat lng from address) now comes with a cost. You can do a limited number of free lookups per month (I think it’s 100,000 but don’t quote me!), and with your API key can do the lookup in one line: -

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY
Note just the suburb/city + country will be enough for your circumstance.
The Google sites / Stack Overflow have many examples of implementation (I’m pretty sure you don’t just assign the URL to a variable, you need to get the site).

You will also want to harden access to your API key, IP protect it by putting your webpage’s host IP as the only allowable client IP.

With the returned lat lng you’ll need another lookup (which I don’t know of) to get the timezone.


There is also to possibility of using the IP of your user, using something like this:-

https://ipgeolocation.io/timezone-api.html
https://www.iplocate.com/en/
https://ipapi.co/

And some of those sites seem to offer free API, so do your research!
Of course if your user runs a VPN it’ll be a bit weird for them!!

As both these solutions are 1 or 2 lines of PHP you don’t need a CakePHP plugin, just directly call the code yourself.

yes i saw stackoverflow and no i didnt see a recent cakephp example .

How do i include geocode in my controller?

Its was the setup i didnt get and yes i have an api key as i can do this in jquery currently on client side but not from a controller

Well it’s PHP, doesn’t matter that its a controller. Unless, you want a live lookup, then you’ll need AJAX in your site to do that. But otherwise, I’m imagining they put their address in, and hit Submit.

In your controller that handles the submit you get their address data, and stuff that into your Google Maps API call.

$json = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=$city+$country&key=$my_gmapi_key');
$data = json_decode($json);

Strip the lat lng out of that data and throw that at whatever site which can get you timezone from lat lng, using no doubt similar code. (May need to put some exception handling in!)

If you’re stuck write what code you’ve got (and don’t include your key!!!) and we’ll see if we can make it behave.

(In fact, with your key, don’t even include it in your CakePHP project just in case for the day you share your project. Stick it in a settings file in a folder above the cake app and in your cfg/bootstrap.php put require '../../settings.php' and refer to that for all your keys & passwords.)

$address=$student[‘address_street’].’ ‘.$student[‘address_suburb’].’ ‘.$student[‘address_postcode’].’ '.$student[‘address_state’];

        $json='https://maps.googleapis.com/maps/api/geocode/json?address='. $address .'?key=xxx';      
       $data = json_decode($json);
       debug($data)

//nothing outputted

i cant get this api to work with the existing address

Then go back to basics. Get it working and move forwards. So step 1, make sure your key actually work. In a bash/command prompt do: -

curl "https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=MY_KEY_HERE"

And you’ll get a nice big fat JSON

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.422064,
               "lng" : -122.0840915
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4234129802915,
                  "lng" : -122.0827425197085
               },
               "southwest" : {
                  "lat" : 37.4207150197085,
                  "lng" : -122.0854404802915
               }
            }
         },
         "place_id" : "ChIJ2QHGpW-7j4ARe7z3NxQzggI",
         "plus_code" : {
            "compound_code" : "CWC8+R9 Mountain View, CA, USA",
            "global_code" : "849VCWC8+R9"
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

Next, create a test.php file on your webpage, all by itself. Just this: -

<?php
$json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=MY_KEY_HERE');
echo $json;

Then, browse to that file and check it dumps the same JSON.
(Note if you’re doing this in your CakePHP app it’ll need to go in webroot [or equivalent].)

Then do the JSON decode, then get the lat lng and echo that.
Next, copy that exact code into to submit side of your controller, so it still pulls 1600 Amphitheatre Parkway regardless of what is entered on the form, and debug that result.
Then pull live data.

I apologise if you’ve already done all that, or a similar process, but first you need to be sure your API is sound and behaves on your site. Also factor in that on the Credentials page Google says any change can take up to 4 hours to implement (but I’ve also found them to be immediate).

Harden your key, lock it down to your IP and webpage IP. If you’re running CloudFlare you’ll need to use HTTP referrers (which don’t always behave for me, and need separate rules for HTTP & HTTPS) - or turn CloudFlare off. After hardening test again! And don’t forget to delete your test.php!!

thanks i have it working following your advice

Thanks for sharing this informations!