├── .editorconfig ├── .github └── workflows │ └── style.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── composer.json └── src ├── Facades └── TimeZoneFacade.php ├── PhpTimeZoneServiceProvider.php ├── TimeZoneService.php └── config └── config.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | insert_final_newline = true 7 | indent_style = space 8 | indent_size = 4 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [*.{yml,yaml,json}] 15 | indent_size = 2 -------------------------------------------------------------------------------- /.github/workflows/style.yml: -------------------------------------------------------------------------------- 1 | name: php style fix psr12 2 | on: 3 | push: 4 | branches: 5 | - main 6 | - 1.3.x 7 | 8 | jobs: 9 | fix_code_style: 10 | runs-on: ubuntu-latest 11 | permissions: 12 | contents: write 13 | 14 | env: 15 | NODE_VERSION: 20 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | with: 20 | ref: ${{ github.head_ref }} 21 | 22 | - name: "laravel-pint" 23 | uses: aglipanci/laravel-pint-action@2.1.0 24 | with: 25 | preset: "psr12" 26 | 27 | - name: Commit changes 28 | uses: stefanzweifel/git-auto-commit-action@v4 29 | with: 30 | commit_message: "style: fix coding style" 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /.idea 3 | /.vagrant 4 | Homestead.json 5 | Homestead.yaml 6 | .env 7 | *.DS_Store 8 | /npm-*.log 9 | composer.phar 10 | composer.lock 11 | .php_cs.cache 12 | *.iml 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [1.3.0] - 2024-06-28 4 | 5 | - **Feature:** Added support for PHP 8.1 and 8.2. 6 | - **Enhancement:** Improved timezone list retrieval performance. 7 | - **Bugfix:** Corrected `listByRegion` method to handle edge cases correctly. 8 | - **Documentation:** Updated README with new usage examples and installation instructions. 9 | 10 | ## [1.2.0] - 2023-01-30 11 | 12 | - **Bugfix:** Fixed `listByRegion` method to accurately filter timezones by region. 13 | - **Enhancement:** Added more detailed error messages for invalid timezone entries. 14 | - **Maintenance:** Updated dependencies to the latest versions for security improvements. 15 | 16 | ## [1.1.0] - 2023-01-30 17 | 18 | - **Bugfix:** Resolved namespace issues with the facade. 19 | - **Enhancement:** Improved handling of default time zones in configurations. 20 | - **Documentation:** Added section for troubleshooting common issues. 21 | 22 | ## [1.0.0] - 2023-01-30 23 | 24 | - **Initial Release:** First release of the php-time-zone package, providing a comprehensive list of all PHP-supported time zones with various utility methods for easy manipulation and retrieval. 25 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Abdullah zahid joy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Php-Time-Zone 2 | 3 | [![Latest Version](https://img.shields.io/github/release/joy2362/php-time-zone.svg?style=flat-square)](https://github.com/jessedp/php-timezones/releases) 4 | [![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 5 | [![Build Status](https://scrutinizer-ci.com/g/joy2362/php-time-zone/badges/build.png?b=main)](https://scrutinizer-ci.com/g/joy2362/php-time-zone/build-status/main) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/joy2362/php-time-zone.svg?style=flat-square)](https://packagist.org/packages/joy2362/php-time-zone) 7 | 8 | A wrapper to enumerate PHP 7.x, 8.x timezones list. 9 | 10 | ## Basics 11 | 12 | - Creates timezone arrays based on PHP's supported timezones with optional grouping by region 13 | - Lists are sorted by offset from high (+14:00) to low (-11:00) 14 | - Return as php arrays for whatever use your heart desires 15 | 16 | ## Installation 17 | 18 | You can install this package using [Composer](https://getcomposer.org). 19 | 20 | ```bash 21 | $ composer require joy2362/php-time-zone 22 | ``` 23 | 24 | Publish config file: 25 | 26 | ```bash 27 | $ php artisan vendor:publish --provider="Joy2362\PhpTimezone\PhpTimeZoneServiceProvider" --tag="config" 28 | ``` 29 | 30 | ## Config 31 | 32 | | Name | Default | Description | 33 | | ----------------- | ------- | ------------------------------------------------- | 34 | | DEFAULT_TIME_ZONE | 'GMT' | label start value support 'GMT' and 'UTC' | 35 | | TIME_DIFF_SYMBOL | '.' | seperator between time different hour and minutes | 36 | | LABEL_FIELD_NAME | 'label' | timezone list array key name for label | 37 | | VALUE_FIELD_NAME | 'value' | timezone list array key name for value | 38 | 39 | ## Usage 40 | 41 | ### 1. Get all timezone list with label and value pair 42 | 43 | ```php 44 | TimeZone::list(); 45 | ``` 46 | 47 | ### 2. Get all timezone list only value 48 | 49 | ```php 50 | TimeZone::listWithoutLabel(); 51 | ``` 52 | 53 | ### 3. Get all timezone list only label 54 | 55 | ```php 56 | TimeZone::listWithoutValue(); 57 | ``` 58 | 59 | ### 4. Get timezone list by a region 60 | 61 | ```php 62 | TimeZone::listByRegion('Asia'); 63 | ``` 64 | 65 | ### 5. Get timezone label from value 66 | 67 | ```php 68 | TimeZone::getLabelFromValue('Asia/Dhaka'); 69 | ``` 70 | 71 | ### 6. Get timezone value from label 72 | 73 | ```php 74 | TimeZone::getValueFromLabel('(GMT +06.00) Asia/Dhaka'); 75 | ``` 76 | 77 | ### 7. Get region list 78 | 79 | ```php 80 | TimeZone::getRegions(); 81 | ``` 82 | 83 | ### 8. Get supported zone list 84 | 85 | ```php 86 | TimeZone::getSupportedTimeZone(); 87 | ``` 88 | 89 | ## Changelog 90 | 91 | Please see [Changelog](./CHANGELOG.md) for more information on what has changed recently. 92 | 93 | ## Contributing 94 | 95 | Pull requests are more than welcome. You must follow the PSR coding standards. 96 | 97 | ## Security 98 | 99 | If you discover any security-related issues, please email abdullahzahidjoy@gmail.com instead of using the issue tracker. 100 | 101 | ## Thanks to 102 | 103 | This is based off some lovely work by: 104 | 105 | - https://github.com/jessedp/php-timezones 106 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "joy2362/php-time-zone", 3 | "description": "A simple package that provide list of all timezone that php support", 4 | "type": "library", 5 | "keywords": ["laravel", "timezone" , "php-time-zone"], 6 | "homepage": "https://github.com/joy2362/php-time-zone", 7 | "require": { 8 | "php": ">=7.2", 9 | "illuminate/support": ">=5.0" 10 | }, 11 | "license": "MIT", 12 | "autoload": { 13 | "psr-4": { 14 | "Joy2362\\PhpTimezone\\": "src/" 15 | } 16 | }, 17 | "authors": [ 18 | { 19 | "name": "joy2362", 20 | "email": "abdullahzahidjoy@gmail.com" 21 | } 22 | ], 23 | "extra": { 24 | "laravel": { 25 | "providers": [ 26 | "Joy2362\\PhpTimezone\\PhpTimeZoneServiceProvider" 27 | ], 28 | "aliases": { 29 | "TimeZone": "Joy2362\\PhpTimezone\\Facades\\TimeZoneFacade" 30 | } 31 | } 32 | }, 33 | "config": { 34 | "sort-packages": true 35 | }, 36 | "minimum-stability": "dev", 37 | "prefer-stable": true 38 | } 39 | -------------------------------------------------------------------------------- /src/Facades/TimeZoneFacade.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 12 | $this->publishes([ 13 | __DIR__.'/./config/config.php' => $this->app->configPath('Timezone.php') 14 | ], 'config'); 15 | } 16 | } 17 | 18 | public function register() 19 | { 20 | $this->mergeConfigFrom(__DIR__.'/config/config.php', 'Timezone'); 21 | $this->app->bind('TimeZoneService', function () { 22 | return new TimeZoneService(); 23 | }); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/TimeZoneService.php: -------------------------------------------------------------------------------- 1 | DateTimeZone::AFRICA, 17 | 'America' => DateTimeZone::AMERICA, 18 | 'Antarctica' => DateTimeZone::ANTARCTICA, 19 | 'Asia' => DateTimeZone::ASIA, 20 | 'Atlantic' => DateTimeZone::ATLANTIC, 21 | 'Australia' => DateTimeZone::AUSTRALIA, 22 | 'Europe' => DateTimeZone::EUROPE, 23 | 'Indian' => DateTimeZone::INDIAN, 24 | 'Pacific' => DateTimeZone::PACIFIC, 25 | ]; 26 | 27 | /** 28 | * @var array|string[] 29 | */ 30 | private array $supportedTimeZone = ['GMT', 'UTC']; 31 | 32 | /** 33 | * @return array 34 | */ 35 | public function getRegions(): array 36 | { 37 | return array_keys($this->regions); 38 | } 39 | 40 | /** 41 | * @return array 42 | */ 43 | public function getSupportedTimeZone(): array 44 | { 45 | return $this->supportedTimeZone; 46 | } 47 | 48 | /** 49 | * @return array 50 | */ 51 | public function list(): array 52 | { 53 | $list = []; 54 | foreach ($this->regions as $region) { 55 | $list = array_merge($list, $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? [])); 56 | } 57 | return $list; 58 | } 59 | 60 | /** 61 | * @return array 62 | */ 63 | public function listWithoutLabel(): array 64 | { 65 | $list = []; 66 | foreach ($this->regions as $region) { 67 | $list = array_merge($list, $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? [], 'value')); 68 | } 69 | return $list; 70 | } 71 | 72 | /** 73 | * @return array 74 | */ 75 | public function listWithoutValue(): array 76 | { 77 | $list = []; 78 | foreach ($this->regions as $region) { 79 | $list = array_merge($list, $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? [], 'label')); 80 | } 81 | return $list; 82 | } 83 | 84 | public function listByRegion($region): array 85 | { 86 | if (!array_key_exists($region, $this->regions)) { 87 | return []; 88 | } 89 | 90 | return $this->getTimeZoneList(DateTimeZone::listIdentifiers($this->regions[$region]) ?? []); 91 | } 92 | 93 | /** 94 | * @param $label 95 | * @return string 96 | */ 97 | public function getValueFromLabel($label): string 98 | { 99 | $str_zone = explode(') ', $label); 100 | return str_replace(' ', '_', $str_zone[1]); 101 | } 102 | 103 | /** 104 | * @param $value 105 | * @return string 106 | */ 107 | public function getLabelFromValue($value): string 108 | { 109 | return $this->getLabel($value); 110 | } 111 | 112 | /** 113 | * @param $timezone 114 | * @return string 115 | */ 116 | private function getLabel($timezone): string 117 | { 118 | try { 119 | $time = new DateTime('', new DateTimeZone($timezone)); 120 | $time_diff = $this->getTimeDiff($time); 121 | $zone = $this->getZone($time); 122 | 123 | $defaultTimeZone = Config::get('Timezone.DEFAULT_TIME_ZONE', 'GMT'); 124 | 125 | $defaultTimeZone = in_array($defaultTimeZone, $this->supportedTimeZone) ? $defaultTimeZone : $this->supportedTimeZone[0]; 126 | return "({$defaultTimeZone} {$time_diff}) {$zone}"; 127 | } catch (Exception $ex) { 128 | return ''; 129 | } 130 | } 131 | 132 | /** 133 | * @param $time 134 | * @return string 135 | */ 136 | private function getTimeDiff($time): string 137 | { 138 | $time_diff_symbol = Config::get('Timezone.TIME_DIFF_SYMBOL', '.'); 139 | $str_time_diff = $time->format('p'); 140 | return str_replace(':', $time_diff_symbol, $str_time_diff); 141 | } 142 | 143 | /** 144 | * @param $time 145 | * @return string 146 | */ 147 | private function getZone($time): string 148 | { 149 | return str_replace('_', ' ', $time->format('e')); 150 | } 151 | 152 | /** 153 | * @param array $timezones 154 | * @param bool $isLabel 155 | * @param bool $isValue 156 | * @return array 157 | */ 158 | private function getTimeZoneList(array $timezones, string $type = 'list'): array 159 | { 160 | $label = Config::get('Timezone.LABEL_FIELD_NAME', 'label'); 161 | $value = Config::get('Timezone.VALUE_FIELD_NAME', 'value'); 162 | 163 | $data = []; 164 | 165 | foreach ($timezones as $timezone) { 166 | 167 | switch ($type) { 168 | case 'label': 169 | $data[] = $this->getLabel($timezone); 170 | break; 171 | 172 | case 'value': 173 | $data[] = $timezone; 174 | break; 175 | 176 | default: 177 | $zone = [ 178 | "{$label}" => $this->getLabel($timezone), 179 | "{$value}" => $timezone, 180 | ]; 181 | $data[] = $zone; 182 | break; 183 | } 184 | } 185 | return $data; 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | 'GMT', //GMT or UTC 5 | 'TIME_DIFF_SYMBOL' => '.', //+00.00 6 | 'LABEL_FIELD_NAME' => 'label' , 7 | 'VALUE_FIELD_NAME' => 'value' 8 | ]; 9 | --------------------------------------------------------------------------------