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 21, 2018 11:14am')
	->convertTo('Asia/Manila', 'F d, Y g:ia'); //--> February 22, 2018 3:14am

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 21, 2018 11:14am
echo eden('timezone', 'America/Los_Angeles', 'February 21, 2018 11:14am')->getUTC(); //--> UTC-8:00
echo eden('timezone', 'GMT-8', 'February 21, 2018 11:14am')->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 21, 2018 11:14am')->getOffsetDates('F d, Y g:ia', 60);

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

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 21, 2018 11:14am')->getUTCDates('F d, Y g:ia', 120);

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

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

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.