1- ## About php-timezones
1+ # php-timezones
2+
23[ ![ Build Status] ( https://api.travis-ci.org/jessedp/php-timezones.svg?branch=master )] ( https://travis-ci.org/jessedp/php-timezones )
34
4- A wrapper to enumerate PHP 5.6+, 7+ timezones in a simplified way for use in various ways.
5+ A wrapper to enumerate PHP 5.6+, 7.x timezones in a simplified way for use in various ways.
6+
7+ This is done with Laravel 5.5+ in mind - YMMV elsewhere.
58
6- This is done with Laravel 5.x in mind, but may work elsewhere. YMMV elsewhere.
9+ ## Basics
710
8- ### Basics
911- Creates timezone arrays based on PHP's supported timezones with optional grouping by region
1012- Lists are sorted by offset from high (+14:00) to low (-11:00)
1113- Optionally group the arrays (multi-dim associated array) by region
12- + sorting is the same, but only inside each region
14+ - sorting is the same, but only inside each region
1315- For either case, return those as:
14- + php arrays for whatever use your heart desires
15- + HTML select list
16+ - php arrays for whatever use your heart desires
17+ - HTML select list
1618- 2 utility functions for converting to/from UTC
1719
1820## Installation
1921
2022You can install this package using [ Composer] ( https://getcomposer.org ) .
2123
22- - First, edit your project's ` composer.json ` file to require ` jessedp/php-timezones ` :
24+ ``` bash
25+ $ composer require jessedp/php-timezones
2326
24- ``` php
25- ...
26- "require": {
27+ Using version ^0.2.0 for jessedp/php-timezones
28+ ./composer.json has been updated
2729 ...
28- "jessedp/php-timezones": "0.1"
29- },
30- ```
31-
32- - Next, update Composer from the terminal (or your IDE if it does that):
33-
34- ``` shell
35- $ composer update
36- ```
3730
38- - Finally, after the update completes, add the service provider. Open ` config/app.php ` and add a new item to the ** providers** array:
39-
40- ``` php
41- ...
42- 'providers' => array(
43- ...
44- jessedp\Timezones\TimezonesServiceProvider::class,
45- ),
4631```
4732
4833## Usage
4934
50- #### 1. Render a timezone HTML Select list
35+ ### 1. Render a timezone HTML Select list
5136
5237The method ` Timezones::create() ` has three parameters:
38+
5339``` php
5440Timezones::create($name, $selected, $opts);
5541```
42+
5643- $name ** required** - the * name* of the select element
5744- $selected - sets the selected value of list box, assuming the a value with the option exists
5845- $opts an array of options as key=>value:
59- + attr => * array* of key=>value pairs to be included in the select element (ie, 'id', 'class', etc.)
60- + with_regions => * boolean* whether or not to use option groups for the regions/continents (defaults to false)
61- + regions => array (of strings) specifying the region(s) to include
46+ - attr => * array* of key=>value pairs to be included in the select element (ie, 'id', 'class', etc.)
47+ - with_regions => * boolean* whether or not to use option groups for the regions/continents (defaults to false)
48+ - regions => array (of strings) specifying the region(s) to include
49+
50+ #### Basic Example
6251
63- ###### Basic Example:
6452``` php
6553Timezones::create('timezone');
6654```
6755
6856Returns a string similar to:
6957
58+ ``` html
7059 <select name =" timezone" >
7160 ...
7261 <option value =" Africa/Abidjan" >(GMT/UTC + 00:00) Abidjan</option >
7362 <option value =" Africa/Accra" >(GMT/UTC + 00:00) Accra</option >
7463 ...
7564
76- </select
65+ </select >
66+ ```
67+
68+ #### "Selected" Example
69+
70+ Same as above, but * Asia/Ho_Chi_Minh* will be selected by default
7771
78-
79- ###### "Selected" Example:
80- Same as above, but * Asia/Ho_Chi_Minh* will be selected by default
8172``` php
8273Timezones::create('timezone', 'Asia/Ho_Chi_Minh');
8374```
8475
85- ###### "Options" Example:
76+ #### "Options" Example
77+
8678You may also add multiple attributes with an array.
8779
8880``` php
89- Timezones::create('timezone', null,
81+ Timezones::create('timezone', null,
9082 ['attr'=>[
9183 'id' => 'my_id',
9284 'class' => 'form-control'
@@ -96,6 +88,7 @@ Timezones::create('timezone', null,
9688
9789Which gives us:
9890
91+ ``` html
9992 <select name =" timezone" id =" my_id" class =" form-control" >
10093 <option value =" Pacific/Apia" >(GMT/UTC + 14:00)  ;  ;  ;  ; Pacific/Apia</option ><option value =" Pacific/Kiritimati" >(GMT/UTC + 14:00)  ;  ;  ;  ; Pacific/Kiritimati</option >
10194 ...
@@ -105,10 +98,11 @@ Which gives us:
10598 <option value =" America/New_York" >(GMT/UTC − 05:00)  ;  ;  ;  ; America/New York</option >
10699 ...
107100 </select >
108-
109- ###### "Regions/Grouping" Example:
101+ ```
102+
103+ #### "Regions/Grouping" Example
110104
111- Say you want the option groups but only a couple regions...
105+ Say you want the option groups but only a couple regions...
112106
113107``` php
114108Timezones::create('timezone',null,
@@ -120,6 +114,7 @@ Timezones::create('timezone',null,
120114
121115This will return a string similar to the following:
122116
117+ ``` html
123118 <select name =" timezone" class =" form-control" >
124119 <optgroup label =" Africa" >
125120 <option value =" Africa/Addis_Ababa" >(GMT/UTC + 03:00)  ;  ;  ;  ; Addis Ababa</option >
@@ -136,33 +131,36 @@ This will return a string similar to the following:
136131 ...
137132 </optgroup >
138133 </select >
134+ ```
139135
140-
141- #### 2. Render a timezone array
136+ ### 2. Render a timezone array
142137
143138You can also render timezone list as an array. To do so, just use the ` Timezones::toArray() ` method.
144139
145140Example in Laravel:
141+
146142``` php
147143$timezone_list = Timezones::toArray();
148144```
149145
150- #### 3. Utility methods
146+ ### 3. Utility methods
147+
151148The package includes two methods that make it easy to deal with displaying and storing timezones, ` convertFromUTC() ` and ` convertToUTC() ` :
152149
153150Each function accepts two required parameters and a third optional parameter dealing with the format of the returned timestamp.
154151
152+ ``` php
155153 Timezones::convertFromUTC($timestamp, $timezone, $format);
156154 Timezones::convertToUTC($timestamp, $timezone, $format);
155+ ```
157156
158157The 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 ) .
159158
160159The 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). If you're using this for display purposes, you may find including ` '(e)' ` in the format string which displays the timezone.
161160
161+ ## Thanks to
162162
163- # Thanks to...
164163This is based off some lovely work by:
165164
166165- https://github.com/JackieDo/Timezone-List
167166- https://github.com/camroncade/timezone
168-
0 commit comments