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', 'August 23, 2017 9:33pm')
	->convertTo('Asia/Manila', 'F d, Y g:ia'); //--> August 24, 2017 1:33pm

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

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

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', 'August 23, 2017 9:33pm')->getUTCDates('F d, Y g:ia', 120);

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

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

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.