{"id":14975179,"url":"https://github.com/mikebarlow/timezones","last_synced_at":"2025-10-14T03:34:18.757Z","repository":{"id":57054491,"uuid":"99966946","full_name":"mikebarlow/Timezones","owner":"mikebarlow","description":"Timezone helpers for converting dates to and from UTC. Built in support also for Laravel.","archived":false,"fork":false,"pushed_at":"2024-12-06T21:24:10.000Z","size":33,"stargazers_count":6,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-01T05:31:57.979Z","etag":null,"topics":["dates","datetime","laravel","timezones","utc"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mikebarlow.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-10T21:40:35.000Z","updated_at":"2024-12-06T21:24:13.000Z","dependencies_parsed_at":"2022-08-24T05:21:22.603Z","dependency_job_id":null,"html_url":"https://github.com/mikebarlow/Timezones","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikebarlow%2FTimezones","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikebarlow%2FTimezones/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikebarlow%2FTimezones/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikebarlow%2FTimezones/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikebarlow","download_url":"https://codeload.github.com/mikebarlow/Timezones/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238497557,"owners_count":19482274,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dates","datetime","laravel","timezones","utc"],"created_at":"2024-09-24T13:51:40.091Z","updated_at":"2025-10-14T03:34:18.751Z","avatar_url":"https://github.com/mikebarlow.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Timezones\n\n[![Author](http://img.shields.io/badge/author-@mikebarlow-red.svg?style=flat-square)](https://twitter.com/mikebarlow)\n[![Latest Version](https://img.shields.io/github/release/mikebarlow/Timezones.svg?style=flat-square)](https://github.com/mikebarlow/Timezones/releases)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/mikebarlow/Timezones/blob/master/LICENSE)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/mikebarlow/Timezones/php.yml?branch=main\u0026style=flat-square)](https://github.com/mikebarlow/Timezones/actions)\n\n## Introduction\n\nTimezones is a PSR-2 compliant package used for easily converting dates into UTC or into your local timezones. Bundled is also support for Laravel Facades and blade directives to make usage even easier!\n\n## Installation\n\n### Composer\n\nSimply require the package via composer into your app.\n\n    composer require mbarlow/timezones 2.*\n\n### Setup\n\nTo get started simply instantiate the Timezones object with the `new` keyword in PHP.\n\n```php\n    $timezones = new \\MBarlow\\Timezones\\Timezones;\n```\n\n#### Laravel\n\nBundled with this package is a Laravel Service provider that is setup for auto-discovery.\n\nIf you are not using Laravel 5.5 then you can add the following Service Provider to your `config/app.php` config.\n\n```php\n[\n    /*\n     * Package Service Providers...\n     */    \n    \\MBarlow\\Timezones\\TimezonesServiceProvider::class,\n];\n```\n\nThe service provider will bind the Timezones Object to the container allowing you to type hint in the object.\n\n```php\nuse MBarlow\\Timezones\\Timezones;\nclass MyController \n{\n    public function myControllerAction(Timezones $timezones)                \n    {\n        // \n    }\n}\n```\n\n## Usage\n\nTo use, first instantiate the object or typehint the object into your method.\n\n### convertToUTC\n\n```php\n    $timezones = new \\MBarlow\\Timezones\\Timezones;\n\n    $dateTime = '2020-08-14 12:23:00';\n    $tz = 'America/New_York';\n    $format = 'Y-m-d H:i:s';\n\n    $timezones-\u003econvertToUTC($dateTime, $tz, $format);\n```\n\n```php\nuse MBarlow\\Timezones\\Timezones;\nclass MyController \n{\n    public function myControllerAction(Timezones $timezones)                \n    {\n        $dateTime = '2020-08-14 12:23:00';\n        $tz = 'America/New_York';\n        $format = 'Y-m-d H:i:s';\n    \n        $timezones-\u003econvertToUTC($dateTime, $tz, $format);\n    }\n}\n```\n\nThis method should be used when parsing an user inputted date to convert it to UTC ready for storing in your database.\n\n`$dateTime`is a required field and can either be an instance of `\\DateTime` that was created with the correct local timezone or it can be a string which will be passed into an instance of `\\DateTime`.\n\nIf `$dateTime` is an instance of `\\DateTime` then `$tz` is not required and should default to `null`. If `$dateTime` is passed through as a string then `$tz` should either be an instance of `\\DateTimeZone` or a string describing a [valid timezone](http://php.net/manual/en/timezones.php).\n\n`$format` is an optional field that defaults to standard MySQL datetime format of `YYYY-MM-DD HH:MM:SS`.\n\n### convertToLocal\n\n```php\n    $timezones = new \\MBarlow\\Timezones\\Timezones;\n\n    $dateTime = '2020-08-14 12:23:00';\n    $tz = 'America/New_York';\n    $format = 'jS F Y H:i';\n\n    $timezones-\u003econvertToLocal($dateTime, $tz, $format);\n```\n\n```php\nuse MBarlow\\Timezones\\Timezones;\nclass MyController \n{\n    public function myControllerAction(Timezones $timezones)                \n    {\n        $dateTime = '2020-08-14 12:23:00';\n        $tz = 'America/New_York';\n        $format = 'jS F Y H:i';\n    \n        $timezones-\u003econvertToLocal($dateTime, $tz, $format);\n    }\n}\n```\n\nThis method should be used in your \"views or in your API endpoints\" to transform a UTC date stored within your database into the local timezone for the user viewing the page or requesting the data.\n\n`$dateTime` is a required field and can either be an instance of `\\DateTime` that was created with the correct UTC timezone or it can be a string which will be passed into an instance of `\\DateTime`.\n\n`$tz` is a required field and can either be an instance of `\\DateTimeZone` or a string describing a [valid timezone](http://php.net/manual/en/timezones.php) and should be the timezone of the end user.\n\n`$format` is an optional field that defaults to standard MySQL datetime format of `YYYY-MM-DD HH:MM:SS`.\n\n### timezoneList\n\nThis method can be used for API endpoints or within your templates to generate an array of formatted timezones. The generated array will return an associative array with the timezone as the key and a nice label with offset as the value.\n\n```php\n  $timezones = new \\MBarlow\\Timezones\\Timezones;\n\n  print_r($timezones-\u003etimezoneList());\n  /*\n    Array\n    (\n        [Pacific/Midway] =\u003e (UTC -11:00) Midway\n        [Pacific/Niue] =\u003e (UTC -11:00) Niue\n        [Pacific/Pago_Pago] =\u003e (UTC -11:00) Pago Pago\n        [Pacific/Honolulu] =\u003e (UTC -10:00) Honolulu\n        [Pacific/Rarotonga] =\u003e (UTC -10:00) Rarotonga\n        [Pacific/Tahiti] =\u003e (UTC -10:00) Tahiti\n        [Pacific/Marquesas] =\u003e (UTC -09:30) Marquesas\n        [America/Adak] =\u003e (UTC -09:00) Adak\n        [Pacific/Gambier] =\u003e (UTC -09:00) Gambier\n        [America/Anchorage] =\u003e (UTC -08:00) Anchorage\n        [America/Juneau] =\u003e (UTC -08:00) Juneau\n        [America/Metlakatla] =\u003e (UTC -08:00) Metlakatla\n        [America/Nome] =\u003e (UTC -08:00) Nome\n        [Pacific/Pitcairn] =\u003e (UTC -08:00) Pitcairn\n        [America/Sitka] =\u003e (UTC -08:00) Sitka\n  \n         ....\n     )      \n   */\n```\n\n```php\n$timezones = new \\MBarlow\\Timezones\\Timezones;\n$list = $timezones-\u003etimezoneList();\n\necho '\u003cselect=\"timezone\"\u003e';\nforeach ($list as $timezone =\u003e $label) {\n    echo '\u003coption value=\"' . $timezone . '\"\u003e' . $label .  '\u003c/option\u003e';\n}\necho '\u003c/select\u003e';\n```\n\n## Laravel Extras\n\nIf you have installed the package into a Laravel application a few extra goodies are available.\n\n### Facade\n\nShould you prefer to use a Facade, one has been bundled and made available with the Service Provider. The facade is available at `\\Timezones'.\n\nBoth methods described in the \"Usage\" section above are available.\n\n    \\Timezones::convertToUTC();\n    \\Timezones::convertToLocal();\n\n### Blade Directive\n\nA blade directive will also be registered by the Service Provider. This only supports the `convertToLocal` method and is designed for use within your blade templates to convert a UTC datetime into the end users time and display on the page in the correct format.\n\n    @displayDate($dateTime, $tz, $format)\n\nAll parameters match those defined for the `convertToLocal` method however all fields are required when using the blade directive.\n\n## Testing\n\nIf you wish to run the tests, clone out the repository\n\n```bash\n    git clone git@github.com:mikebarlow/Timezones.git\n```\n\nChange to the root of the repository and run composer install with the dev dependencies\n\n```bash\n    cd Timezones\n    composer install\n```\n\nA script is defined in the `composer.json` to run both the code sniffer and the unit tests\n\n```bash \n    composer run test\n```\n\nOr run them individually as required\n\n```bash\n    ./vendor/bin/phpunit\n    \n    ./vendor/bin/phpcs --standard=PSR2 --ignore=src/Facade/* src\n```\n\n## Changelog\n\nYou can view the changelog [HERE](https://github.com/mikebarlow/Timezones/blob/master/CHANGELOG.md)\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/mikebarlow/Timezones/blob/master/CONTRIBUTING.md) for details.\n\n## License\n\nThe MIT License (MIT). Please see [License File](https://github.com/mikebarlow/Timezones/blob/master/LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikebarlow%2Ftimezones","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikebarlow%2Ftimezones","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikebarlow%2Ftimezones/lists"}