You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Timezone.php 1.4KB

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