spatie/google-time-zone

Get time zones for coordinates

1.1.0 2019-09-17 12:49 UTC

README

Latest Version MIT Licensed Build Status Quality Score StyleCI Total Downloads

This package can convert GPS coordinates to time zones using Google's Time Zone service. Here's a quick example:

GoogleTimeZone::getTimeZoneForCoordinates('51.2194475', '4.4024643');

// Will return this array
[
    "dstOffset" => 0
    "rawOffset" => 3600
    "timeZoneId" => "Europe/Brussels"
    "timeZoneName" => "Central European Standard Time"
]

Installation

You can install this package through composer.

composer require spatie/google-time-zone

Laravel installation

Though the package works fine in non-Laravel projects we've included some niceties for our fellow Artisans.

In Laravel will autoregister the package itself, but you should still publish the config file yourself.

php artisan vendor:publish --provider="Spatie\GoogleTimeZone\GoogleTimeZoneServiceProvider" --tag="config"

This is the content of the config file:

return [
    /*
     * The api key used when sending timezone requests to Google.
     */
    'key' => env('GOOGLE_MAPS_TIMEZONE_API_KEY', ''),

    /*
     * The language param used to set response translations for textual data.
     *
     * More info: https://developers.google.com/maps/faq#languagesupport
     */
    'language' => '',
];

Usage

Here's how you can get the time zone for a given set of coordinates.

$googleTimeZone = new GoogleTimeZone();

$googleTimeZone->setApiKey(config('google-time-zone.key'));

$googleTimeZone->getTimeZoneForCoordinates('51.2194475', '4.4024643');

/*
// Will return this array
[
    "dstOffset" => 0
    "rawOffset" => 3600
    "timeZoneId" => "Europe/Brussels"
    "timeZoneName" => "Central European Standard Time"
]
*/

You can get the result back in a specific language.

$googleTimeZone
   ->setLanguage('nl')
   ->getTimeZoneForCoordinates('51.2194475', '4.4024643');

/*
// Will return this array
[
      "dstOffset" => 0
      "rawOffset" => 3600
      "timeZoneId" => "Europe/Brussels"
      "timeZoneName" => "Midden-Europese standaardtijd"
]
*/

It is possible to specify a timestamp for the location so that daylight savings can be taken into account. We will set this timestamp to the current time by default.

$googleTimeZone
   ->setTimestamp(new DateTime('13 august 2018'))
   ->getTimeZoneForCoordinates('51.2194475', '4.4024643');

/*
// Will return this array
[
      "dstOffset" => 3600
      "rawOffset" => 3600
      "timeZoneId" => "Europe/Brussels"
      "timeZoneName" => "Central European Summer Time"
]
*/

If you are using the package with Laravel, you can simply call getTimeZoneForCoordinates.

GoogleTimeZone::getTimeZoneForCoordinates('51.2194475', '4.4024643');

/*
// Will return this array
[
    "dstOffset" => 0
    "rawOffset" => 3600
    "timeZoneId" => "Europe/Brussels"
    "timeZoneName" => "Central European Standard Time"
]
*/

When no time zone was found null will be returned.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

Support us

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License

The MIT License (MIT). Please see License File for more information.