blob: ce37ad9a300e320cbe312b7dd12ed800d36fa554 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Date Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/date_helper.html
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Get "now" time
32 *
33 * Returns time() or its GMT equivalent based on the config file preference
34 *
35 * @access public
36 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +020037 */
Derek Allard2067d1a2008-11-13 22:59:24 +000038if ( ! function_exists('now'))
39{
40 function now()
41 {
42 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020043
Derek Allard2067d1a2008-11-13 22:59:24 +000044 if (strtolower($CI->config->item('time_reference')) == 'gmt')
45 {
46 $now = time();
47 $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
Barry Mienydd671972010-10-04 16:33:58 +020048
Derek Allard2067d1a2008-11-13 22:59:24 +000049 if (strlen($system_time) < 10)
50 {
51 $system_time = time();
52 log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
53 }
Barry Mienydd671972010-10-04 16:33:58 +020054
Derek Allard2067d1a2008-11-13 22:59:24 +000055 return $system_time;
56 }
57 else
58 {
59 return time();
60 }
61 }
62}
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064// ------------------------------------------------------------------------
65
66/**
67 * Convert MySQL Style Datecodes
68 *
69 * This function is identical to PHPs date() function,
70 * except that it allows date codes to be formatted using
71 * the MySQL style, where each code letter is preceded
Derek Jones37f4b9c2011-07-01 17:56:50 -050072 * with a percent sign: %Y %m %d etc...
Derek Allard2067d1a2008-11-13 22:59:24 +000073 *
74 * The benefit of doing dates this way is that you don't
75 * have to worry about escaping your text letters that
76 * match the date codes.
77 *
78 * @access public
79 * @param string
80 * @param integer
81 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +020082 */
Derek Allard2067d1a2008-11-13 22:59:24 +000083if ( ! function_exists('mdate'))
84{
85 function mdate($datestr = '', $time = '')
86 {
87 if ($datestr == '')
88 return '';
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 if ($time == '')
91 $time = now();
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 $datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));
94 return date($datestr, $time);
95 }
96}
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Allard2067d1a2008-11-13 22:59:24 +000098// ------------------------------------------------------------------------
99
100/**
101 * Standard Date
102 *
103 * Returns a date formatted according to the submitted standard.
104 *
105 * @access public
106 * @param string the chosen format
107 * @param integer Unix timestamp
108 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200109 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000110if ( ! function_exists('standard_date'))
111{
112 function standard_date($fmt = 'DATE_RFC822', $time = '')
113 {
114 $formats = array(
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400115 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400117 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400119 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC',
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
121 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400122 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400124 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O'
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 );
126
127 if ( ! isset($formats[$fmt]))
128 {
129 return FALSE;
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 return mdate($formats[$fmt], $time);
133 }
134}
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136// ------------------------------------------------------------------------
137
138/**
139 * Timespan
140 *
141 * Returns a span of seconds in this format:
142 * 10 days 14 hours 36 minutes 47 seconds
143 *
144 * @access public
145 * @param integer a number of seconds
146 * @param integer Unix timestamp
147 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200148 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000149if ( ! function_exists('timespan'))
150{
151 function timespan($seconds = 1, $time = '')
152 {
153 $CI =& get_instance();
154 $CI->lang->load('date');
155
156 if ( ! is_numeric($seconds))
157 {
158 $seconds = 1;
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 if ( ! is_numeric($time))
162 {
163 $time = time();
164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 if ($time <= $seconds)
167 {
168 $seconds = 1;
169 }
170 else
171 {
172 $seconds = $time - $seconds;
173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 $str = '';
176 $years = floor($seconds / 31536000);
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200179 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
Barry Mienydd671972010-10-04 16:33:58 +0200181 }
182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 $seconds -= $years * 31536000;
184 $months = floor($seconds / 2628000);
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 if ($years > 0 OR $months > 0)
187 {
188 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200189 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
Barry Mienydd671972010-10-04 16:33:58 +0200191 }
192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 $seconds -= $months * 2628000;
194 }
195
196 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 if ($years > 0 OR $months > 0 OR $weeks > 0)
199 {
200 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200201 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200206 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000207
208 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 if ($months > 0 OR $weeks > 0 OR $days > 0)
211 {
212 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200213 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 $seconds -= $days * 86400;
218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 if ($days > 0 OR $hours > 0)
223 {
224 if ($hours > 0)
225 {
226 $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 $seconds -= $hours * 3600;
230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 if ($days > 0 OR $hours > 0 OR $minutes > 0)
235 {
236 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200237 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 $seconds -= $minutes * 60;
242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 if ($str == '')
245 {
246 $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 return substr(trim($str), 0, -1);
250 }
251}
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253// ------------------------------------------------------------------------
254
255/**
256 * Number of days in a month
257 *
258 * Takes a month/year as input and returns the number of days
259 * for the given month/year. Takes leap years into consideration.
260 *
261 * @access public
262 * @param integer a numeric month
263 * @param integer a numeric year
264 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200265 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000266if ( ! function_exists('days_in_month'))
267{
268 function days_in_month($month = 0, $year = '')
269 {
270 if ($month < 1 OR $month > 12)
271 {
272 return 0;
273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 if ( ! is_numeric($year) OR strlen($year) != 4)
276 {
277 $year = date('Y');
278 }
Barry Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 if ($month == 2)
281 {
282 if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
283 {
284 return 29;
285 }
286 }
287
288 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
289 return $days_in_month[$month - 1];
290 }
291}
Derek Jones36591092010-03-05 10:05:51 -0600292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293// ------------------------------------------------------------------------
294
295/**
296 * Converts a local Unix timestamp to GMT
297 *
298 * @access public
299 * @param integer Unix timestamp
300 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200301 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000302if ( ! function_exists('local_to_gmt'))
303{
304 function local_to_gmt($time = '')
305 {
306 if ($time == '')
307 $time = time();
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));
310 }
311}
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313// ------------------------------------------------------------------------
314
315/**
316 * Converts GMT time to a localized value
317 *
318 * Takes a Unix timestamp (in GMT) as input, and returns
319 * at the local value based on the timezone and DST setting
320 * submitted
321 *
322 * @access public
323 * @param integer Unix timestamp
324 * @param string timezone
325 * @param bool whether DST is active
326 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200327 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000328if ( ! function_exists('gmt_to_local'))
329{
330 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200331 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 if ($time == '')
333 {
334 return now();
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 $time += timezones($timezone) * 3600;
338
339 if ($dst == TRUE)
340 {
341 $time += 3600;
342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 return $time;
345 }
346}
Derek Jones36591092010-03-05 10:05:51 -0600347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348// ------------------------------------------------------------------------
349
350/**
351 * Converts a MySQL Timestamp to Unix
352 *
353 * @access public
354 * @param integer Unix timestamp
355 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200356 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000357if ( ! function_exists('mysql_to_unix'))
358{
359 function mysql_to_unix($time = '')
360 {
361 // We'll remove certain characters for backward compatibility
362 // since the formatting changed with MySQL 4.1
363 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 $time = str_replace('-', '', $time);
366 $time = str_replace(':', '', $time);
367 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // YYYYMMDDHHMMSS
Derek Jones37f4b9c2011-07-01 17:56:50 -0500370 return mktime(
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 substr($time, 8, 2),
372 substr($time, 10, 2),
373 substr($time, 12, 2),
374 substr($time, 4, 2),
375 substr($time, 6, 2),
376 substr($time, 0, 4)
377 );
378 }
379}
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381// ------------------------------------------------------------------------
382
383/**
384 * Unix to "Human"
385 *
386 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
387 *
388 * @access public
389 * @param integer Unix timestamp
390 * @param bool whether to show seconds
391 * @param string format: us or euro
392 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200393 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000394if ( ! function_exists('unix_to_human'))
395{
396 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
397 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500398 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 if ($fmt == 'us')
401 {
402 $r .= date('h', $time).':'.date('i', $time);
403 }
404 else
405 {
406 $r .= date('H', $time).':'.date('i', $time);
407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 if ($seconds)
410 {
411 $r .= ':'.date('s', $time);
412 }
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 if ($fmt == 'us')
415 {
416 $r .= ' '.date('A', $time);
417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 return $r;
420 }
421}
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423// ------------------------------------------------------------------------
424
425/**
426 * Convert "human" date to GMT
427 *
428 * Reverses the above process
429 *
430 * @access public
431 * @param string format: us or euro
432 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200433 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000434if ( ! function_exists('human_to_unix'))
435{
436 function human_to_unix($datestr = '')
437 {
438 if ($datestr == '')
439 {
440 return FALSE;
441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600444 $datestr = preg_replace("/\040+/", ' ', $datestr);
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 if ( ! preg_match('/^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr))
447 {
448 return FALSE;
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Jones36591092010-03-05 10:05:51 -0600451 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000452
453 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Jones37f4b9c2011-07-01 17:56:50 -0500455 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
456 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
457 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000458
459 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones37f4b9c2011-07-01 17:56:50 -0500462 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000463
Derek Jones1322ec52009-03-11 17:01:14 +0000464 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500466 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
468 else
469 {
470 // Unless specified, seconds get set to zero.
471 $sec = '00';
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 if (isset($split['2']))
475 {
476 $ampm = strtolower($split['2']);
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
479 $hour = $hour + 12;
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500482 $hour = '00';
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 if (strlen($hour) == 1)
485 $hour = '0'.$hour;
486 }
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 return mktime($hour, $min, $sec, $month, $day, $year);
489 }
490}
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492// ------------------------------------------------------------------------
493
494/**
495 * Timezone Menu
496 *
497 * Generates a drop-down menu of timezones.
498 *
499 * @access public
500 * @param string timezone
501 * @param string classname
502 * @param string menu name
503 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200504 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000505if ( ! function_exists('timezone_menu'))
506{
507 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
508 {
509 $CI =& get_instance();
510 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 if ($default == 'GMT')
513 $default = 'UTC';
514
515 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 if ($class != '')
518 {
519 $menu .= ' class="'.$class.'"';
520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 foreach (timezones() as $key => $val)
525 {
526 $selected = ($default == $key) ? " selected='selected'" : '';
527 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
528 }
529
530 $menu .= "</select>";
531
532 return $menu;
533 }
534}
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536// ------------------------------------------------------------------------
537
538/**
539 * Timezones
540 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500541 * Returns an array of timezones. This is a helper function
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 * for various other ones in this library
543 *
544 * @access public
545 * @param string timezone
546 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200547 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000548if ( ! function_exists('timezones'))
549{
550 function timezones($tz = '')
551 {
552 // Note: Don't change the order of these even though
553 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200554
555 $zones = array(
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 'UM12' => -12,
557 'UM11' => -11,
558 'UM10' => -10,
559 'UM95' => -9.5,
560 'UM9' => -9,
561 'UM8' => -8,
562 'UM7' => -7,
563 'UM6' => -6,
564 'UM5' => -5,
565 'UM45' => -4.5,
566 'UM4' => -4,
567 'UM35' => -3.5,
568 'UM3' => -3,
569 'UM2' => -2,
570 'UM1' => -1,
571 'UTC' => 0,
572 'UP1' => +1,
573 'UP2' => +2,
574 'UP3' => +3,
575 'UP35' => +3.5,
576 'UP4' => +4,
577 'UP45' => +4.5,
578 'UP5' => +5,
579 'UP55' => +5.5,
580 'UP575' => +5.75,
581 'UP6' => +6,
582 'UP65' => +6.5,
583 'UP7' => +7,
584 'UP8' => +8,
585 'UP875' => +8.75,
586 'UP9' => +9,
587 'UP95' => +9.5,
588 'UP10' => +10,
589 'UP105' => +10.5,
590 'UP11' => +11,
591 'UP115' => +11.5,
592 'UP12' => +12,
593 'UP1275' => +12.75,
594 'UP13' => +13,
595 'UP14' => +14
596 );
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 if ($tz == '')
599 {
600 return $zones;
601 }
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 if ($tz == 'GMT')
604 $tz = 'UTC';
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
607 }
608}
609
610
611/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000612/* Location: ./system/helpers/date_helper.php */