- How do I install and configure spatie/google-time-zone in Laravel?
- Install it with composer require spatie/google-time-zone. Laravel auto-registers the service provider, but you should publish the config with php artisan vendor:publish --provider=Spatie\GoogleTimeZone\GoogleTimeZoneServiceProvider --tag=config. Then set GOOGLE_MAPS_TIMEZONE_API_KEY in your environment or config.
- How do I convert latitude and longitude to a time zone with this package?
- Use GoogleTimeZone::getTimeZoneForCoordinates('51.2194475', '4.4024643') through the facade or inject the service. The result is an array with dstOffset, rawOffset, timeZoneId, and timeZoneName, such as Europe/Brussels and Central European Standard Time. This works in Laravel and plain PHP.
- Do I need a Google Maps API key for spatie/google-time-zone?
- Yes. The package calls Google's Time Zone service, so you need a Google Maps API key with the Time Zone API enabled. In Laravel, set GOOGLE_MAPS_TIMEZONE_API_KEY in your environment or published config. Be aware that Google's free tier is rate-limited and high volume can incur costs.
- Can I use spatie/google-time-zone outside Laravel?
- Yes, spatie/google-time-zone works in plain PHP as well as Laravel. The Laravel service provider, facade, and publishable config are conveniences rather than requirements. You can use the package classes directly and provide your Google API key.
- Does spatie/google-time-zone cache time zone lookups?
- No, there is no built-in caching in the package. Each lookup makes a request to Google's Time Zone API, so use Laravel's Cache::remember or persist results in your database. Caching by coordinates or known locations reduces latency, quota usage, and cost.
- How should I handle Google Time Zone API errors and quota limits?
- The package provides minimal built-in handling for API failures, so wrap calls in try/catch and log errors. Add retries with backoff for transient network issues and monitor Google API quotas and billing. Keep a cached or manual fallback for when Google's API is unavailable.
- How do I test Laravel code that uses spatie/google-time-zone?
- Mock the GoogleTimeZone facade or inject a stubbed service in PHPUnit or Mockery tests. Add feature tests for boundary coordinates, invalid inputs, and API failure responses. Avoid calling the live Google API in automated tests.
- Is spatie/google-time-zone suitable for high-volume production apps?
- It can be used in production, but each lookup depends on Google API latency, quotas, and pricing. Cache results aggressively, pre-fetch time zones for fixed locations, and monitor usage and failures. For very high volume or offline requirements, add a local time zone database fallback.
- What are alternatives to spatie/google-time-zone?
- Alternatives include local IANA time zone boundary datasets and other geolocation or time zone APIs. spatie/google-time-zone is a thin wrapper around Google's Time Zone service, so it fits when that API's data and pricing work for you. Compare offline capability, accuracy, and cost before switching.
- Which Laravel versions does spatie/google-time-zone support, and is it maintained?
- Check the package's composer.json and Packagist release notes for exact PHP and Laravel constraints. Modern Laravel versions can auto-discover the service provider, but older releases may need a compatible package tag. The package is MIT licensed and published by Spatie; review GitHub releases and issue activity to judge maintenance before adopting it.