12. Timezones

Timezones in Eden have progressed to be so simple to make conversions from one timezone to another. We simplified this process over time to one line of code. Figure 1 shows how it's simply done.

Figure 1. Convert Time
echo eden('timezone', 'GMT-8', 'March 26, 2017 1:15pm')
	->convertTo('Asia/Manila', 'F d, Y g:ia'); //--> March 27, 2017 5:15am

The second argument on line 1 of Figure 1 shows the initial timezone. Infact you can express the timezones using several formats.

  • UTC Formats - UTC-8, UTC-8:30
  • GMT Formats - GMT-8, GMT+0430
  • PHP Formats - America/Los_Angeles, Asia/Manila
  • Timezone Abbreviations - EST, PDT, MNL

The third argument in Figure 1 is optional if you want to specify the time, other wise it will use the current server time. Next we convert the time to Manila using convertTo(). Optionally, you can specify the date format as described at PHP.net. With the timezone object you also can convert timezone formats to GMT, UTC, offset and reformat the time. The figure below describes how to do that.

Figure 2. Convert Timezone Formats
echo eden('timezone', 'GMT-8', time())->getOffset(); //--> -28800 
echo eden('timezone', 'America/Los_Angeles')->getTime('F d, Y g:ia'); //--> March 26, 2017 1:15pm
echo eden('timezone', 'America/Los_Angeles', 'March 26, 2017 1:15pm')->getUTC(); //--> UTC-7:00
echo eden('timezone', 'GMT-8', 'March 26, 2017 1:15pm')->getGMT(); //--> GMT-800 

For international applications with a user base, usually it's important to store their preferred timezone. Generating a list of timezones available is easy with Eden and the following Figures show how to easily generate a timezone list in key value format.

Figure 3. A List of Offsets
$offsets = eden('timezone', 'GMT-8', 'March 26, 2017 1:15pm')->getOffsetDates('F d, Y g:ia', 60);

/* Array
(
    [-43200] => March 26, 2017 9:15am
    [-39600] => March 26, 2017 10:15am
    [-36000] => March 26, 2017 11:15am
    [-32400] => March 26, 2017 12:15pm
    [-28800] => March 26, 2017 1:15pm
    [-25200] => March 26, 2017 2:15pm
    [-21600] => March 26, 2017 3:15pm
    [-18000] => March 26, 2017 4:15pm
    [-14400] => March 26, 2017 5:15pm
    [-10800] => March 26, 2017 6:15pm
    [-7200] => March 26, 2017 7:15pm
    [-3600] => March 26, 2017 8:15pm
    [0] => March 26, 2017 9:15pm
    [3600] => March 26, 2017 10:15pm
    [7200] => March 26, 2017 11:15pm
    [10800] => March 27, 2017 12:15am
    [14400] => March 27, 2017 1:15am
    [18000] => March 27, 2017 2:15am
    [21600] => March 27, 2017 3:15am
    [25200] => March 27, 2017 4:15am
    [28800] => March 27, 2017 5:15am
    [32400] => March 27, 2017 6:15am
    [36000] => March 27, 2017 7:15am
    [39600] => March 27, 2017 8:15am
    [43200] => March 27, 2017 9:15am
)
 */

In the example above, getOffsetDates() will set each value according to the date format specified and will render a list of dates in 60 minute intervals. Although intervals are optional, You can change the intervals to any number representing minutes.

Figure 4. A List of UTC in 2 hour intervals
$offsets = eden('timezone', 'GMT-8', 'March 26, 2017 1:15pm')->getUTCDates('F d, Y g:ia', 120);

/* Array
(
    [UTC-12:00] => March 26, 2017 9:15am
    [UTC-10:00] => March 26, 2017 11:15am
    [UTC-8:00] => March 26, 2017 1:15pm
    [UTC-6:00] => March 26, 2017 3:15pm
    [UTC-4:00] => March 26, 2017 5:15pm
    [UTC-2:00] => March 26, 2017 7:15pm
    [UTC+0:00] => March 26, 2017 9:15pm
    [UTC+2:00] => March 26, 2017 11:15pm
    [UTC+4:00] => March 27, 2017 1:15am
    [UTC+6:00] => March 27, 2017 3:15am
    [UTC+8:00] => March 27, 2017 5:15am
    [UTC+10:00] => March 27, 2017 7:15am
    [UTC+12:00] => March 27, 2017 9:15am
)
 */
Figure 5. A List of GMT in 30 minute intervals
$offsets = eden('timezone', 'GMT-8', 'March 26, 2017 1:15pm')->getGMTDates('F d, Y g:ia', 30);

/* Array
(
    [GMT-1200] => March 26, 2017 9:15am
    [GMT-1130] => March 26, 2017 9:45am
    [GMT-1100] => March 26, 2017 10:15am
    [GMT-1030] => March 26, 2017 10:45am
    [GMT-1000] => March 26, 2017 11:15am
    [GMT-930] => March 26, 2017 11:45am
    [GMT-900] => March 26, 2017 12:15pm
    [GMT-830] => March 26, 2017 12:45pm
    [GMT-800] => March 26, 2017 1:15pm
    [GMT-730] => March 26, 2017 1:45pm
    [GMT-700] => March 26, 2017 2:15pm
    [GMT-630] => March 26, 2017 2:45pm
    [GMT-600] => March 26, 2017 3:15pm
    [GMT-530] => March 26, 2017 3:45pm
    [GMT-500] => March 26, 2017 4:15pm
    [GMT-430] => March 26, 2017 4:45pm
    [GMT-400] => March 26, 2017 5:15pm
    [GMT-330] => March 26, 2017 5:45pm
    [GMT-300] => March 26, 2017 6:15pm
    [GMT-230] => March 26, 2017 6:45pm
    [GMT-200] => March 26, 2017 7:15pm
    [GMT-130] => March 26, 2017 7:45pm
    [GMT-100] => March 26, 2017 8:15pm
    [GMT-030] => March 26, 2017 8:45pm
    [GMT+000] => March 26, 2017 9:15pm
    [GMT+030] => March 26, 2017 9:45pm
    [GMT+100] => March 26, 2017 10:15pm
    [GMT+130] => March 26, 2017 10:45pm
    [GMT+200] => March 26, 2017 11:15pm
    [GMT+230] => March 26, 2017 11:45pm
    [GMT+300] => March 27, 2017 12:15am
    [GMT+330] => March 27, 2017 12:45am
    [GMT+400] => March 27, 2017 1:15am
    [GMT+430] => March 27, 2017 1:45am
    [GMT+500] => March 27, 2017 2:15am
    [GMT+530] => March 27, 2017 2:45am
    [GMT+600] => March 27, 2017 3:15am
    [GMT+630] => March 27, 2017 3:45am
    [GMT+700] => March 27, 2017 4:15am
    [GMT+730] => March 27, 2017 4:45am
    [GMT+800] => March 27, 2017 5:15am
    [GMT+830] => March 27, 2017 5:45am
    [GMT+900] => March 27, 2017 6:15am
    [GMT+930] => March 27, 2017 6:45am
    [GMT+1000] => March 27, 2017 7:15am
    [GMT+1030] => March 27, 2017 7:45am
    [GMT+1100] => March 27, 2017 8:15am
    [GMT+1130] => March 27, 2017 8:45am
    [GMT+1200] => March 27, 2017 9:15am
)
 */

Handing timezones for projects can be a real pain. Eden makes this process easier so you can move on other important matters. The next section continues with Eden's i18n support by showing how it deals with multiple languages. To find out more about this check out 13. Languages.


© 2012 Openovate Labs. All rights reserved.