• jamie2017

    (@jamie2017)


    Hello,

    I am trying to work out how I can calculate day light saving time, after I have worked out the timezone. For example. $timezone = 0, when I put in the longitude and latitude of Cambridge in the United Kingdom. This is as expected. But, I want to write a code so it also takes into account daylight saving time. On the 30th July 1985, I would expect an overall output ($time) of 1, as that is the summer time daylight saving adjustment.

    I cannot see why the code below wont work, any ideas ? I am sure the rawoffset command is meant to make the adjustment.

    <?php
    $town = urldecode($_GET['town']);
    $country = urldecode($_GET['country']);
    
    $location = json_decode(@file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?key=API_KEY_HERE&address='.urlencode($town.', '.(!empty($state) ? $state.', ' : '').$country)), true)['results'][0]['geometry']['location'];
    
    $timezone = json_decode(@file_get_contents('https://maps.googleapis.com/maps/api/timezone/json?key=API_KEY_HERE&location='.$location['lat'].','.$location['lng'].'&timestamp='.date('U')), true);
    
    $time = $timezone['rawOffset'] / 60 / 60;
    
    echo $time;   
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • Joy

    (@joyously)

    This is not a WordPress question. Perhaps you could get an answer on StackOverflow or the notes on the PHP manual.

    Moderator bcworkz

    (@bcworkz)

    I agree this is not a WP question, but since I have a little experience with Google APIs…

    I suggest using WP_Http class methods instead of file_get_contents(). It’s a little more reliable IMO. Usually when API calls like that fail, it’s because you’ve not properly set up the right authentication with Google. With some APIs it’s more than getting an API key. For example, geocode requires server IP restriction even though other options appear to be available. Even if you you think you did so correctly, Google may not agree. Verify the returned data is really what you expected. It could instead be an error message telling you what is wrong.

    Thread Starter jamie2017

    (@jamie2017)

    Hello,

    Thank you so much for your feedback. The code is used for a WordPress website I have.
    But it is used in a custom made plugin within my WordPress website. I will make sure I post elsewhere in the future.

    Thank you for your feedback bcworkz. Yes, maybe I should try putting on more API key restrictions to see if that is the issue. I was wondering if you have any experience with the ‘rawoffset’ command ? I have spent quite a few hours looking at documentation. However, for a beginner like me, documentation does not always tell me everything I need to know.

    Thank you

    Jamie

    Moderator bcworkz

    (@bcworkz)

    Even though you’re using WP as your code base, your issue is with the Google API. AFAICT your WP code looks fine. It’s not that we don’t want to help, it’s that we don’t have the requisite expertise and you’re better off asking in a venue that has the expertise. There is a lot of good expertise in StackOverflow. I believe there’s also a related Google support forum as well.

    I’ve not used the timezone API, so cannot comment on rawoffset. I suggest inputting some coords where you know the desired results, then output the Google response and attempt to correlate what you know with the responses. Maybe compare an area that uses DST with one that does not. For example, the U.S. states Arizona and Utah are both in the Mountain timezone (-7 UTC w/o DST, -6 w/), but Arizona does not use DST, so during the Summer their clocks match that of California in the Pacific timezone (-8 UTC w/o DST, -7 w/).

    IME most API issues can be resolved by examining the response under known conditions.

    Thread Starter jamie2017

    (@jamie2017)

    Hi bcworkz.

    Thank you for the advice. I appreciate it.

    Jamie

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Calculating Timezone and DST offset’ is closed to new replies.