ilhamrisky/laravel-timezone-by-city
Composer 安装命令:
composer require ilhamrisky/laravel-timezone-by-city
包简介
Laravel package to get timezone by city
README 文档
README
Introduction
Laravel Get Timezone by City package provides a simple way to retrieve timezone information for cities around the world. It utilizes Carbon for date and time manipulation. This README will guide you on how to install and use the package in your Laravel project.
Data Source
The city data provided by this package is sourced from the GeoNames Gazetteer, available at GeoNames Gazetteer. GeoNames is a geographical database that covers all countries and contains over eleven million place names. As of the last update on February 16, 2024, it provides information for 146,892 cities worldwide.
Installation
Require this package with composer using the following command:
composer require ilhamrisky/laravel-timezone-by-city
Usage
Import the namespace and instantiate the class in your Laravel controller or service
use IlhamriSKY\GetTimeZoneByCity\GetTimeZoneByCity; $timeZoneByCity = new GetTimeZoneByCity();
Example
Example 1: Check if a city exists in the dataset
$cityToCheck = 'New York City'; $cityExists = $timeZoneByCity->cityExists($cityToCheck); // $cityExists will be a boolean indicating whether 'New York City' exists in the dataset echo "Does $cityToCheck exist? " . ($cityExists ? 'Yes' : 'No');
Example 2: Get a list of all cities available in the dataset
$allCities = $timeZoneByCity->getAllCities(); // $allCities will be an array containing names of all cities echo "All Cities: " . implode(', ', $allCities);
Example 3: Get all data for a specific city
$cityDetails = $timeZoneByCity->getAllData('London'); // $cityDetails will be an array containing details for 'London' echo "Details for London: " . json_encode($cityDetails);
Example 4: Get the timezone for a specific city
$cityTimeZone = $timeZoneByCity->getTimeZone('Tokyo'); // $cityTimeZone will be a string containing the timezone for 'Tokyo' echo "Timezone for Tokyo: " . $cityTimeZone;
Example 5: Get the UTC offset for a specific city
$cityUtcOffset = $timeZoneByCity->getTimeUTC('Sydney'); // $cityUtcOffset will be a string containing the UTC offset for 'Sydney' echo "UTC Offset for Sydney: " . $cityUtcOffset;
Example 6: Get the latitude and longitude for a specific city
$cityLatLong = $timeZoneByCity->getTimeLatLong('Paris'); // $cityLatLong will be an array containing 'lat' and 'lng' for 'Paris' echo "Latitude and Longitude for Paris: " . json_encode($cityLatLong);
Example 7: Get a list of cities based on their country code
$countryCode = 'US'; $citiesInCountry = $timeZoneByCity->getCitiesByCountry($countryCode); // $citiesInCountry will be an array containing names of cities in the United States echo "Cities in $countryCode: " . implode(', ', $citiesInCountry);
Example 8: Get the current time in a specified city's timezone
$currentTimeInCity = $timeZoneByCity->getCurrentTimeInCity('Berlin'); // $currentTimeInCity will be a Carbon instance representing the current time in Berlin's timezone echo "Current Time in Berlin: " . ($currentTimeInCity ? $currentTimeInCity->toDateTimeString() : 'City not found');
Example 9: Get the city based on latitude and longitude coordinates
$latitude = 40.7128; $longitude = -74.0060; $matchingCity = $timeZoneByCity->getCityByCoordinates($latitude, $longitude); // $matchingCity will be an array containing details for the city matching the coordinates echo "City at ($latitude, $longitude): " . json_encode($matchingCity);
Example 10: Convert time between two cities' timezones
$sourceCity = 'Los Angeles'; $destinationCity = 'London'; $convertedTime = $timeZoneByCity->convertTimeBetweenCities($sourceCity, $destinationCity, 'Y-m-d H:i:s'); // $convertedTime will be a string containing the converted time in London's timezone echo "Converted Time from $sourceCity to $destinationCity: " . ($convertedTime ?? 'Cities not found');
Example 11: Compare local time and city time
$city = 'Semarang'; $compareTime = $timeZoneByCity->compareLocalTimeWithCityTime($city); // $compareTime will be a array containing the compare local time and city time echo "Time in {$city}: {$compareTime['city_datetime']}\n"; echo "Time in local timezone: {$compareTime['local_datetime']}\n"; echo "Time difference: {$compareTime['time_difference']['hours']} hours, {$compareTime['time_difference']['minutes']} minutes, {$compareTime['time_difference']['seconds']} seconds\n";
License (MIT License):
This package is released under the MIT License, which allows you to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the software. The only condition is that the above copyright notice and this permission notice shall be included in all copies or substantial portions of the software.
Feel free to contribute, create issues, or submit pull requests to enhance the functionality or fix any bugs.
ilhamrisky/laravel-timezone-by-city 适用场景与选型建议
ilhamrisky/laravel-timezone-by-city 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.39k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2024 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ilhamrisky/laravel-timezone-by-city 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ilhamrisky/laravel-timezone-by-city 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.39k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-15