Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Timezone.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 = self::getTimezonesForCountry($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. public static function getTimezonesForCountry(string $countryCode): array
  45. {
  46. return DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, strtoupper($countryCode));
  47. }
  48. }