選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Timezone.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Utilities\Localization;
  3. use App\Enums\Setting\TimeFormat;
  4. use App\Models\Setting\Localization;
  5. use DateTimeZone;
  6. use IntlTimeZone;
  7. use Symfony\Component\Intl\Timezones;
  8. class Timezone
  9. {
  10. public static function getTimezoneOptions(?string $countryCode = null): array
  11. {
  12. if (empty($countryCode)) {
  13. return [];
  14. }
  15. $countryTimezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, strtoupper($countryCode));
  16. if (empty($countryTimezones)) {
  17. return [];
  18. }
  19. $localizedTimezoneNames = Timezones::getNames();
  20. $results = [];
  21. foreach ($countryTimezones as $timezoneIdentifier) {
  22. $timezoneConical = IntlTimeZone::getCanonicalID($timezoneIdentifier);
  23. $translatedName = $localizedTimezoneNames[$timezoneConical] ?? $timezoneConical;
  24. $cityName = self::extractCityName($translatedName);
  25. $localTime = self::getLocalTime($timezoneIdentifier);
  26. $timezoneAbbreviation = now($timezoneIdentifier)->format('T');
  27. $results[$timezoneIdentifier] = "{$cityName} ({$timezoneAbbreviation}) {$localTime}";
  28. }
  29. return $results;
  30. }
  31. public static function extractCityName(string $translatedName): string
  32. {
  33. if (preg_match('/\((.*?)\)/', $translatedName, $match)) {
  34. return trim($match[1]);
  35. }
  36. return $translatedName;
  37. }
  38. public static function getLocalTime(string $timezone): string
  39. {
  40. $localizationModel = Localization::firstOrFail();
  41. $time_format = $localizationModel->time_format->value ?? TimeFormat::DEFAULT;
  42. return now($timezone)->translatedFormat($time_format);
  43. }
  44. }