The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

Google Time Zone

spatie/google-time-zone image

Google Time Zone stats

Downloads
238K
Stars
95
Open Issues
0
Forks
17

View on GitHub →

Get time zones for coordinates

Get the time zone used at the given coordinates

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"
]

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

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, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

License

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

spatie photo

We create open source, digital products and courses for the developer community

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Spatie Google Time Zone Related Articles

Laravel Google Fonts Package image

Laravel Google Fonts Package

Read article
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm