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

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

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

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

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

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

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.