6

I was hoping to be pointed in the right direction for this, my boss wants to have the store hours on website linked to the hours on Google Maps/places, that way he can just update the hours and times on google and not have to worry about the site. I am not familiar with google api, so I don't know where to begin for that.

I've tried embedding google maps, but it doesn't include hours, just the address

The website's hosted on github,

3
  • Am I understanding your question right? You want to be able to change your hours and have it show in Google Maps in near real-time or possibly within the knowledge graph card (on the right of a brand search).
    – closetnoc
    Commented Jan 21, 2017 at 5:20
  • @closetnoc I think it's the other way round.
    – MrWhite
    Commented Jan 21, 2017 at 9:40
  • @closetnoc My boss wants to update the hours on Google Maps, then have that update the hours on the website. So our website would read whats on maps Commented Jan 21, 2017 at 21:16

3 Answers 3

3

Google does not make this easy, however you can scrape it with the following php code. First you need the "place id", you can get this using this tool: https://developers.google.com/places/place-id

Start typing the business name and once google finds it, you can copy the place ID from the popup.

Here's the PHP - replace ENTER_PLACE_ID_HERE with the place id you got in the previous step:

<?
    $curlURL = 'https://www.google.com/maps/search/?api=1&query=Google&query_place_id=ENTER_PLACE_ID_HERE';
    $curl = curl_init($curlURL);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
    $response=curl_exec( $curl );
    for($i=0;$i<7;$i++){
        $day = jddayofweek($i,1);
        $start=stripos($response,'[\"'.$day.'\",')+strlen($day)+9;
        echo($day.": ".substr($response,$start,stripos($response,'\"]',$start)-$start)."<br />");
    }
?>
1
  • Doesnt work any more, Seems they append content with js.
    – fdrv
    Commented Apr 27, 2022 at 13:11
2

If you have a Google Places place_id and API key, the following URL will return the hours as a JSON object.

Just be sure to replace "YOUR_PLACE_ID" with your actual Google Places place_id and "YOUR_API_KEY" with your Google Places API key.

https://maps.googleapis.com/maps/api/place/details/json?placeid=YOUR_PLACE_ID&fields=opening_hours&key=YOUR_API_KEY

You will get hours for each day as both day-of-week-number, from 0 to 6, where Sunday = 0, as well as a standard language array. The numbered days are found in result.opening_hours.periods and the standard language array is result.opening_hours.weekday_text

weekday_text is simpler to use while periods will enable you to do things like display the results in different languages.

Here is how it might look in PHP:

$url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=YOUR_PLACE_ID&fields=opening_hours&key=YOUR_API_KEY';

$hours_raw = @file_get_contents($url);
if (!empty($hours_raw)) {
  $hours_json = json_decode($hours_raw, true);
  if (!empty($hours_json['result']['opening_hours'])) {
    $business_hours = $hours_json['result']['opening_hours']['weekday_text'];
  }
}

// $business_hours is an array you can use however you like e.g.
echo '<ul>';
foreach ($business_hours as $weekday) {
  echo '<li>' . $weekday . '</li>';
}
echo '</ul>';
2
  • This is expensive you have a huge traffic.
    – fdrv
    Commented Apr 27, 2022 at 13:12
  • Agree with @fdrv caching the json on the server would be a very good idea.
    – Dan
    Commented Jul 12 at 12:44
-2

This may be migrated, if not, find your answer here

https://stackoverflow.com/questions/20735016/how-to-get-opening-hours-from-google-places-api

7
  • 1
    That seems like he's searching for locations and trying to pull off information. i want to have the same static location, our business listing, and just retrieve the hours from that. i don't necessarily want to include a map, unless i have to Commented Jan 21, 2017 at 3:28
  • But it has the code that will pull the hours, this is not a programming site, you will need to do that yourself
    – Steve
    Commented Jan 21, 2017 at 4:36
  • 1
    A link only answer is not very helpful. Rather than link to SO, you should copy the relevant portions of the post to here. Copying from other StackExchange sites is allowed because of the content license as long as you give attribution. You have to say who posted it and link to their profile page and link back to the original post. Commented Jan 29, 2017 at 12:27
  • Also, it is not possible to mark posts as duplicate across StackExchange sites. Commented Jan 29, 2017 at 12:28
  • 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.