ilhamrisky/laravel-timezone-by-city

Laravel 包,用于根据城市获取时区

v1.0.0 2024-02-16 04:58 UTC

This package is auto-updated.

Last update: 2024-09-16 06:23:35 UTC


README

简介

Laravel Get Timezone by City 包提供了一种简单的方法来检索全球城市的时区信息。它使用 Carbon 进行日期和时间操作。本 README 将指导您如何在 Laravel 项目中安装和使用此包。

数据来源

本包提供的城市数据来源于 GeoNames Gazetteer,可在以下链接找到:GeoNames Gazetteer。GeoNames 是一个地理数据库,涵盖所有国家,包含超过一千一百万个地名。截至 2024 年 2 月 16 日最后更新,它提供了全球 146,892 个城市的详细信息。

安装

使用以下命令通过 composer 安装此包:

composer require ilhamrisky/laravel-timezone-by-city

用法

在您的 Laravel 控制器或服务中导入命名空间并实例化类

use IlhamriSKY\GetTimeZoneByCity\GetTimeZoneByCity;

$timeZoneByCity = new GetTimeZoneByCity();

示例

示例 1:检查城市是否存在于数据集中

$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');

示例 2:获取数据集中所有城市的列表

$allCities = $timeZoneByCity->getAllCities();
// $allCities will be an array containing names of all cities
echo "All Cities: " . implode(', ', $allCities);

示例 3:获取特定城市的所有数据

$cityDetails = $timeZoneByCity->getAllData('London');
// $cityDetails will be an array containing details for 'London'
echo "Details for London: " . json_encode($cityDetails);

示例 4:获取特定城市的时区

$cityTimeZone = $timeZoneByCity->getTimeZone('Tokyo');
// $cityTimeZone will be a string containing the timezone for 'Tokyo'
echo "Timezone for Tokyo: " . $cityTimeZone;

示例 5:获取特定城市的 UTC 偏移量

$cityUtcOffset = $timeZoneByCity->getTimeUTC('Sydney');
// $cityUtcOffset will be a string containing the UTC offset for 'Sydney'
echo "UTC Offset for Sydney: " . $cityUtcOffset;

示例 6:获取特定城市的纬度和经度

$cityLatLong = $timeZoneByCity->getTimeLatLong('Paris');
// $cityLatLong will be an array containing 'lat' and 'lng' for 'Paris'
echo "Latitude and Longitude for Paris: " . json_encode($cityLatLong);

示例 7:根据国家代码获取城市列表

$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);

示例 8:获取指定城市时区的当前时间

$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');

示例 9:根据经纬度坐标获取城市

$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);

示例 10:在两个城市的时区之间转换时间

$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');

示例 11:比较本地时间和城市时间

$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";

许可证(MIT 许可证)

此包在 MIT 许可证下发布,允许您使用、复制、修改、合并、发布、分发、再许可和/或销售软件的副本。唯一条件是必须在所有副本或软件的重要部分中包含上述版权声明和本许可声明。

请随时贡献、创建问题或提交拉取请求以增强功能或修复任何错误。