PHP Classes

PHP Google Maps Time Zone Convert: Get a location time zone using Google Maps API

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 223 All time: 8,240 This week: 37Up
Version License PHP version Categories
google-maps-timezone 1.2.8MIT/X Consortium ...5.3Localization, PHP 5, Time and Date, W..., G...
Description 

Author

This class can get a location time zone using Google Maps API.

It takes the geographic coordinates of a given location and sends requests to the Google Maps API to determine the time zone of a given location.

The class returns time zone for the given location, as well as that location's time offset from UTC.

Innovation Award
PHP Programming Innovation award nominee
October 2016
Number 4


Prize: One ebook of choice by Packt
Some applications need to be able to show the time for a certain event in the current user time zone. The time zone depends on the geographic location of the user.

The location of the user may be determined from the user IP address by using a geo IP database or Web service.

So, if you have the user location coordinates, you can use the Google Maps API to determine what is the user time zone.

This package can call determine the time zone of a location precisely calling the Google maps API.

Manuel Lemos
Picture of Ivan Melgrati
  Performance   Level  
Innovation award
Innovation award
Nominee: 3x

 

Example

<?php
   
/**
     * This section includes a sample query that demonstrate features of the API.
     * The code below query performs a time zone request for Github's server (San Francisco, California, United States). The timestamp is set to March 8th, 2012.
     *
     * @author Ivan Melgrati
     * @copyright 2018
     * @package GoogleMapsTimeZone
     * @author Ivan Melgrati
     * @version 1.5.0
     */

   
use imelgrat\GoogleMapsTimeZone\GoogleMapsTimeZone;
    require_once (
'../src/GoogleMapsTimeZone.php');
   
   
/**
     * All queries require an API key from Google
     * @link https://developers.google.com/maps/documentation/timezone/get-api-key
     * */
   
define('API_KEY', 'YOUR API KEY HERE');

   
// Create GoogleMapsTimeZone object with default properties
   
$timezone_object = new GoogleMapsTimeZone();
   
   
// Set Google API key
   
$timezone_object->setApiKey(API_KEY);
   
   
// Set XML as query return format
   
$timezone_object->setFormat($timezone_object::FORMAT_XML);
   
   
// Set French as query return language
    // Check https://developers.google.com/maps/faq#languagesupport for a list of supported languages
   
$timezone_object->setLanguage('fr');
   
   
// Set latitude and longitude (New York City)
   
$timezone_object->setLatitude(40.730610)->setLongitude(-73.935242);
   
   
// Set Timestamp (server-side current time)
   
$timezone_object->setTimestamp(time());
   
   
// Perform query
   
$timezone_data = $timezone_object->queryTimeZone();

    echo
'<pre>';
   
print_r($timezone_data);
    echo
'</pre>';

   
   
// Set XML as query return format
   
$timezone_object->setFormat($timezone_object::FORMAT_JSON);
   
   
// Set Arabic as query return language
    // Check https://developers.google.com/maps/faq#languagesupport for a list of supported languages
   
$timezone_object->setLanguage('ar');
   
   
// Perform query
   
$timezone_data = $timezone_object->queryTimeZone();

    echo
'<pre>';
   
var_dump($timezone_data);
    echo
'</pre>';
?>


Details

google-time-zone

GitHub license GitHub release Total Downloads GitHub issues GitHub stars

A PHP wrapper for the Google Maps TimeZone API.

The Google Maps Time Zone API provides a simple interface to request the time zone for a location on the earth, as well as that location's time offset from UTC.

The API provides time offset data for any locations on Earch. Requests for the time zone information are made for a specific latitude/longitude pair and timestamp.

The class automates the query process and returns the name of that time zone (in different languages), the time offset from UTC, and the daylight savings offset in a user-selectable format (XML or JSON).

Developed by Ivan Melgrati

A PHP wrapper for the Google Maps Time Zone API.

Developed by Ivan Melgrati Twitter

Requirements

  • PHP >= 5.3.0
  • In order to be able to use this class, it's necessary to provide an API key or, for business clients, Client ID and signing key.

Installation

Composer

The recommended installation method is through Composer, a dependency manager for PHP. Just add imelgrat/google-time-zone to your project's composer.json file:

{
    "require": {
        "imelgrat/google-time-zone": "*"
    }
}

More details can be found over at Packagist.

Manually

  1. Copy `src/GoogleMapsTimeZone.php` to your codebase, perhaps to the `vendor` directory.
  2. Add the `GoogleMapsTimeZone` class to your autoloader or `require` the file directly.

Then, in order to use the GoogleMapsTimeZone class, you need to invoke the "use" operator to bring the class into skope.

<?php
    use imelgrat\GoogleMapsTimeZone\GoogleMapsTimeZone;
	require_once ('../src/GoogleMapsTimeZone.php');
    
    /
     * All queries require an API key from Google
     * @link https://developers.google.com/maps/documentation/timezone/get-api-key
     /
	define('API_KEY', 'YOUR API KEY HERE');

	// Initialize GoogleMapsTimeZone object (New York City coordinates)
	$timezone_object = new GoogleMapsTimeZone(40.730610, -73.935242, 0, GoogleMapsTimeZone::FORMAT_JSON);
    
    // Set Google API key
	$timezone_object->setApiKey(API_KEY);
    
    // Perform query 
	$timezone_data = $timezone_object->queryTimeZone();
	
	echo '<pre>';
	print_r($timezone_data);
	echo '</pre>';
?>

Feedback

Please open an issue to request a feature or submit a bug report. Or even if you just want to provide some feedback, I'd love to hear. I'm also available on Twitter as @imelgrat.

Contributing

  1. Fork it.
  2. Create your feature branch (`git checkout -b my-new-feature`).
  3. Commit your changes (`git commit -am 'Added some feature'`).
  4. Push to the branch (`git push origin my-new-feature`).
  5. Create a new Pull Request.

  Files folder image Files (69)  
File Role Description
Files folder imagedocs (2 files, 9 directories)
Files folder imageexamples (2 files)
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpdoc.dist.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file VERSION Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:223
This week:0
All time:8,240
This week:37Up