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', 'February 26, 2017 10:50pm')
	->convertTo('Asia/Manila', 'F d, Y g:ia'); //--> February 27, 2017 2:50pm

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'); //--> February 26, 2017 10:50pm
echo eden('timezone', 'America/Los_Angeles', 'February 26, 2017 10:50pm')->getUTC(); //--> UTC-8:00
echo eden('timezone', 'GMT-8', 'February 26, 2017 10:50pm')->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', 'February 26, 2017 10:50pm')->getOffsetDates('F d, Y g:ia', 60);

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

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', 'February 26, 2017 10:50pm')->getUTCDates('F d, Y g:ia', 120);

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

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

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.