https://github.com/camroncade/timezone
Functions for helping manage timezones in Laravel. Includes <select> form builder for timezones.
https://github.com/camroncade/timezone
laravel timezone
Last synced: about 1 year ago
JSON representation
Functions for helping manage timezones in Laravel. Includes <select> form builder for timezones.
- Host: GitHub
- URL: https://github.com/camroncade/timezone
- Owner: camroncade
- License: mit
- Created: 2014-07-11T17:40:06.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2020-04-13T10:13:36.000Z (about 6 years ago)
- Last Synced: 2025-03-05T01:35:37.102Z (over 1 year ago)
- Topics: laravel, timezone
- Language: PHP
- Homepage: http://www.camroncade.com/managing-timezones-with-laravel/
- Size: 21.5 KB
- Stars: 160
- Watchers: 4
- Forks: 37
- Open Issues: 13
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Timezone Select Form Builder
This is a helper function that creates a select menu including timezones in human-readable format. Each underlying key is the php-friendly name for the timezone, making it easy to immediately store and use them. The array of timezones and their underlying keys were taken from the repository by [tamaspap](https://github.com/tamaspap/timezones).
## Installation
Recommended installation of this package is through composer. Include the following in your project's `composer.json` file:
"require": {
"camroncade/timezone": "0.1"
}
Next, update Composer from the Terminal:
composer update
Once this operation completes, the final step is to add the service provider. Open `config/app.php`, and add a new item to the service providers array.
'Camroncade\Timezone\TimezoneServiceProvider'
Now it's ready for use!
## Usage
### Timezone Convert Helper Functions
The package includes two helper functions that make it easy to deal with displaying and storing timezones, `convertFromUTC()` and `convertToUTC()`:
Each function accepts two required parameters and a third optional parameter dealing with the format of the returned timestamp.
Timezone::convertFromUTC($timestamp, $timezone, $format);
Timezone::convertToUTC($timestamp, $timezone, $format);
The first parameter accepts a timestamp, the second accepts the name of the timezone that you are converting to/from. The option values associated with the timezones included in the select form builder can be plugged into here as is. Alternatively, you can use any of [PHP's supported timezones](http://php.net/manual/en/timezones.php).
The third parameter is optional, and default is set to `'Y-m-d H:i:s'`, which is how Laravel natively stores datetimes into the database (the `created_at` and `updated_at` columns).
### Select Form Builder
The package also includes a helper function for the creation of the select option. This is created by calling `Timezone::selectForm()`.
The method selectForm accepts four optional parameters, `string selected`, `placeholder`, `array formAttributes`, and `array optionAttributes`. An example of it's use is shown below:
$selected = 'US/Central';
$placeholder = 'Select a timezone';
$formAttributes = array('class' => 'swegForm', 'style' => 'float:left;', 'name' => 'timezone');
$optionAttributes = array('customValue' => 'true');
Timezone::selectForm($selected, $placeholder, $formAttributes, $optionAttributes);
Note that the `$selected` parameter matches the string to the values on each option, not the display text. The paratmeters `$formAttributes` and `$optionAttributes` can be used for styling, roles, and generally anything you need. This is useful for applying Twitter Bootstrap styling.
Simply returning `Timezone::selectForm()` will output the following:
(UTC-11:00) Midway Island
(UTC-11:00) Samoa
(UTC-10:00) Hawaii
(UTC-09:00) Alaska
(UTC-08:00) Pacific Time (US & Canada)
(UTC-08:00) Tijuana
(UTC-07:00) Arizona
(UTC-07:00) Chihuahua
(UTC-07:00) La Paz
(UTC-07:00) Mazatlan
(UTC-07:00) Mountain Time (US & Canada)
(UTC-06:00) Central America
(UTC-06:00) Central Time (US & Canada)
(UTC-06:00) Guadalajara
(UTC-06:00) Mexico City
(UTC-06:00) Monterrey
(UTC-06:00) Saskatchewan
(UTC-05:00) Bogota
(UTC-05:00) Eastern Time (US & Canada)
(UTC-05:00) Indiana (East)
(UTC-05:00) Lima
(UTC-05:00) Quito
(UTC-04:00) Atlantic Time (Canada)
(UTC-04:30) Caracas
(UTC-04:00) La Paz
(UTC-04:00) Santiago
(UTC-03:30) Newfoundland
(UTC-03:00) Brasilia
(UTC-03:00) Buenos Aires
(UTC-03:00) Georgetown
(UTC-03:00) Greenland
(UTC-02:00) Mid-Atlantic
(UTC-01:00) Azores
(UTC-01:00) Cape Verde Is.
(UTC+00:00) Casablanca
(UTC+00:00) Edinburgh
(UTC+00:00) Greenwich Mean Time : Dublin
(UTC+00:00) Lisbon
(UTC+00:00) London
(UTC+00:00) Monrovia
(UTC+00:00) UTC
(UTC+01:00) Amsterdam
(UTC+01:00) Belgrade
(UTC+01:00) Berlin
(UTC+01:00) Bern
(UTC+01:00) Bratislava
(UTC+01:00) Brussels
(UTC+01:00) Budapest
(UTC+01:00) Copenhagen
(UTC+01:00) Ljubljana
(UTC+01:00) Madrid
(UTC+01:00) Paris
(UTC+01:00) Prague
(UTC+01:00) Rome
(UTC+01:00) Sarajevo
(UTC+01:00) Skopje
(UTC+01:00) Stockholm
(UTC+01:00) Vienna
(UTC+01:00) Warsaw
(UTC+01:00) West Central Africa
(UTC+01:00) Zagreb
(UTC+02:00) Athens
(UTC+02:00) Bucharest
(UTC+02:00) Cairo
(UTC+02:00) Harare
(UTC+02:00) Helsinki
(UTC+02:00) Istanbul
(UTC+02:00) Jerusalem
(UTC+02:00) Kyiv
(UTC+02:00) Pretoria
(UTC+02:00) Riga
(UTC+02:00) Sofia
(UTC+02:00) Tallinn
(UTC+02:00) Vilnius
(UTC+03:00) Baghdad
(UTC+03:00) Kuwait
(UTC+03:00) Minsk
(UTC+03:00) Nairobi
(UTC+03:00) Riyadh
(UTC+03:00) Volgograd
(UTC+03:30) Tehran
(UTC+04:00) Abu Dhabi
(UTC+04:00) Baku
(UTC+04:00) Moscow
(UTC+04:00) Muscat
(UTC+04:00) St. Petersburg
(UTC+04:00) Tbilisi
(UTC+04:00) Yerevan
(UTC+04:30) Kabul
(UTC+05:00) Islamabad
(UTC+05:00) Karachi
(UTC+05:00) Tashkent
(UTC+05:30) Chennai
(UTC+05:30) Kolkata
(UTC+05:30) Mumbai
(UTC+05:30) New Delhi
(UTC+05:30) Sri Jayawardenepura
(UTC+05:45) Kathmandu
(UTC+06:00) Almaty
(UTC+06:00) Astana
(UTC+06:00) Dhaka
(UTC+06:00) Ekaterinburg
(UTC+06:30) Rangoon
(UTC+07:00) Bangkok
(UTC+07:00) Hanoi
(UTC+07:00) Jakarta
(UTC+07:00) Novosibirsk
(UTC+08:00) Beijing
(UTC+08:00) Chongqing
(UTC+08:00) Hong Kong
(UTC+08:00) Krasnoyarsk
(UTC+08:00) Kuala Lumpur
(UTC+08:00) Perth
(UTC+08:00) Singapore
(UTC+08:00) Taipei
(UTC+08:00) Ulaan Bataar
(UTC+08:00) Urumqi
(UTC+09:00) Irkutsk
(UTC+09:00) Osaka
(UTC+09:00) Sapporo
(UTC+09:00) Seoul
(UTC+09:00) Tokyo
(UTC+09:30) Adelaide
(UTC+09:30) Darwin
(UTC+10:00) Brisbane
(UTC+10:00) Canberra
(UTC+10:00) Guam
(UTC+10:00) Hobart
(UTC+10:00) Melbourne
(UTC+10:00) Port Moresby
(UTC+10:00) Sydney
(UTC+10:00) Yakutsk
(UTC+11:00) Vladivostok
(UTC+12:00) Auckland
(UTC+12:00) Fiji
(UTC+12:00) International Date Line West
(UTC+12:00) Kamchatka
(UTC+12:00) Magadan
(UTC+12:00) Marshall Is.
(UTC+12:00) New Caledonia
(UTC+12:00) Solomon Is.
(UTC+12:00) Wellington
(UTC+13:00) Nuku'alofa