Is it possible to show the hours from google maps on a website, so that the website hours updates when we update google maps?

8,069

Solution 1

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 />");
    }
?>

Solution 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>';
Share:
8,069

Related videos on Youtube

franktherabbit
Author by

franktherabbit

Updated on September 18, 2022

Comments

  • franktherabbit
    franktherabbit over 1 year

    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,

    • closetnoc
      closetnoc over 7 years
      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).
    • MrWhite
      MrWhite over 7 years
      @closetnoc I think it's the other way round.
    • franktherabbit
      franktherabbit over 7 years
      @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
  • franktherabbit
    franktherabbit over 7 years
    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
  • Steve
    Steve over 7 years
    But it has the code that will pull the hours, this is not a programming site, you will need to do that yourself
  • Stephen Ostermiller
    Stephen Ostermiller about 7 years
    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.
  • Stephen Ostermiller
    Stephen Ostermiller about 7 years
    Also, it is not possible to mark posts as duplicate across StackExchange sites.
  • Steve
    Steve about 7 years
    @StephenOstermiller - of course a link only answer is helpful. The OP can read the full thread, perhaps something else will be of use. I know you can't mark posts as duplicate across SE sites, I tried that and opted for posting a link first.
  • Stephen Ostermiller
    Stephen Ostermiller about 7 years
  • Steve
    Steve about 7 years
    @StephenOstermiller fair enough, but since it was the only answer, it is better than none.