Skip to content

Commit bbc36c9

Browse files
committed
Dati 0.4.0
1 parent af944af commit bbc36c9

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Includes the following helpers:
88
- [Buildify](https://github.com/nabeghe/buildify-php) <small>v1.0.1</small>
99
- [Cally](https://github.com/nabeghe/cally-php) <small>v0.1.0</small>
1010
- [Colory](https://github.com/nabeghe/colory-php) <small>v0.1.0</small>
11-
- [Dati](https://github.com/nabeghe/dati-php) <small>v0.3.1</small>
11+
- [Dati](https://github.com/nabeghe/dati-php) <small>v0.4.0</small>
1212
- [FileSize](https://github.com/nabeghe/file-size-php) <small>v1.0.0</small>
1313
- [HeadersReader](https://github.com/nabeghe/headers-reader-php) <small>v1.0.1</small>
1414
- [Levex](https://github.com/nabeghe/levex-php) <small>v1.0.1</small>

src/Dati/Dati.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use DateTime;
44
use DateTimeZone;
5+
use IntlTimeZone;
56
use Throwable;
67
use IntlDateFormatter;
78

@@ -479,4 +480,38 @@ public static function toLocal($datetime, $format = null)
479480
{
480481
return static::toTimeZone($datetime, 'GMT', date_default_timezone_get(), $format);
481482
}
483+
484+
/**
485+
* Returns a human-readable display name for a timezone in the given locale.
486+
*
487+
* @param string $locale Locale for formatting (e.g., 'en_US', 'en', 'fa_IR', 'fa')
488+
* @param string|null $timezone Optional timezone ID (e.g., 'Asia/Tehran'). Defaults to system timezone.
489+
* @return string|null Display name of the timezone, or null on error.
490+
*/
491+
public static function getTimeZoneDisplayName(string $locale = 'en', ?string $timezone = null): ?string
492+
{
493+
$timezone ??= date_default_timezone_get();
494+
495+
if (class_exists('IntlDateFormatter')) {
496+
try {
497+
$formatter = new IntlDateFormatter(
498+
$locale,
499+
IntlDateFormatter::FULL,
500+
IntlDateFormatter::FULL,
501+
$timezone,
502+
null,
503+
);
504+
505+
$date_time_zone = new DateTimeZone($timezone);
506+
507+
$display_name = $formatter->getTimeZone()->getDisplayName(false, IntlTimeZone::DISPLAY_LONG, $locale);
508+
if ($display_name !== false) {
509+
return $display_name;
510+
}
511+
} catch (Throwable $e) {
512+
}
513+
}
514+
515+
return null;
516+
}
482517
}

0 commit comments

Comments
 (0)