blob: 7bbfd4f15c4639162fccf4dfd6f924c7dd5d4113 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###########
2Date Helper
3###########
4
5The Date Helper file contains functions that help you work with dates.
6
7.. contents:: Page Contents
8
9Loading this Helper
10===================
11
12This helper is loaded using the following code
13
14::
15
16 $this->load->helper('date');
17
18The following functions are available:
19
20now()
21=====
22
Iban Eguia7bf0a4f2012-03-27 18:36:15 +020023Returns the current time as a Unix timestamp, based on the "timezone" parameter.
Iban Eguiae15e3dd2012-06-09 23:52:27 +020024All PHP available timezones are supported. You can also use 'local' timezone, and
25it will return time().
Derek Jones8ede1a22011-10-05 13:34:52 -050026
Iban Eguia7bf0a4f2012-03-27 18:36:15 +020027.. php:method:: now($timezone = NULL)
28
29 :param string $timezone: The timezone you want to be returned
30 :returns: integer
31
32::
Iban Eguiafeb14da2012-06-12 16:09:36 +020033 echo now("Australia/Victoria");
Iban Eguia7bf0a4f2012-03-27 18:36:15 +020034
Iban Eguiafeb14da2012-06-12 16:09:36 +020035If a timezone is not provided, it will return time() based on "time_reference" setting.
Derek Jones8ede1a22011-10-05 13:34:52 -050036
37mdate()
38=======
39
40This function is identical to PHPs `date() <http://www.php.net/date>`_
41function, except that it lets you use MySQL style date codes, where each
42code letter is preceded with a percent sign: %Y %m %d etc.
43
44The benefit of doing dates this way is that you don't have to worry
45about escaping any characters that are not date codes, as you would
46normally have to do with the date() function. Example
47
48.. php:method:: mdate($datestr = '', $time = '')
49
50 :param string $datestr: Date String
51 :param integer $time: time
52 :returns: integer
53
54
55::
56
57 $datestring = "Year: %Y Month: %m Day: %d - %h:%i %a";
58 $time = time();
59 echo mdate($datestring, $time);
60
61If a timestamp is not included in the second parameter the current time
62will be used.
63
64standard_date()
65===============
66
67Lets you generate a date string in one of several standardized formats.
68Example
69
70.. php:method:: standard_date($fmt = 'DATE_RFC822', $time = '')
71
72 :param string $fmt: the chosen format
73 :param string $time: Unix timestamp
74 :returns: string
75
76::
77
78 $format = 'DATE_RFC822';
79 $time = time();
80 echo standard_date($format, $time);
81
82The first parameter must contain the format, the second parameter must
83contain the date as a Unix timestamp.
84
85Supported formats:
86
87+----------------+------------------------+-----------------------------------+
88| Constant | Description | Example |
89+================+========================+===================================+
90| DATE_ATOM | Atom | 2005-08-15T16:13:03+0000 |
91+----------------+------------------------+-----------------------------------+
92| DATE_COOKIE | HTTP Cookies | Sun, 14 Aug 2005 16:13:03 UTC |
93+----------------+------------------------+-----------------------------------+
94| DATE_ISO8601 | ISO-8601 | 2005-08-14T16:13:03+00:00 |
95+----------------+------------------------+-----------------------------------+
96| DATE_RFC822 | RFC 822 | Sun, 14 Aug 05 16:13:03 UTC |
97+----------------+------------------------+-----------------------------------+
98| DATE_RFC850 | RFC 850 | Sunday, 14-Aug-05 16:13:03 UTC |
99+----------------+------------------------+-----------------------------------+
100| DATE_RFC1036 | RFC 1036 | Sunday, 14-Aug-05 16:13:03 UTC |
101+----------------+------------------------+-----------------------------------+
102| DATE_RFC1123 | RFC 1123 | Sun, 14 Aug 2005 16:13:03 UTC |
103+----------------+------------------------+-----------------------------------+
104| DATE_RFC2822 | RFC 2822 | Sun, 14 Aug 2005 16:13:03 +0000 |
105+----------------+------------------------+-----------------------------------+
106| DATE_RSS | RSS | Sun, 14 Aug 2005 16:13:03 UTC |
107+----------------+------------------------+-----------------------------------+
108| DATE_W3C | W3C | 2005-08-14T16:13:03+0000 |
109+----------------+------------------------+-----------------------------------+
110
111
112local_to_gmt()
113==============
114
115Takes a Unix timestamp as input and returns it as GMT.
116
117.. php:method:: local_to_gmt($time = '')
118
119 :param integer $time: Unix timestamp
120 :returns: string
121
122Example:
123
124::
125
126 $now = time();
127 $gmt = local_to_gmt($now);
128
129gmt_to_local()
130==============
131
132Takes a Unix timestamp (referenced to GMT) as input, and converts it to
133a localized timestamp based on the timezone and Daylight Saving time
134submitted.
135
136.. php:method:: gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
137
138 :param integer $time: Unix timestamp
139 :param string $timezone: timezone
140 :param boolean $dst: whether DST is active
141 :returns: integer
142
143Example
144
145::
146
147 $timestamp = '1140153693';
148 $timezone = 'UM8';
149 $daylight_saving = TRUE;
150 echo gmt_to_local($timestamp, $timezone, $daylight_saving);
151
152
153.. note:: For a list of timezones see the reference at the bottom of this page.
154
155
156mysql_to_unix()
157===============
158
159Takes a MySQL Timestamp as input and returns it as Unix.
160
161.. php:method:: mysql_to_unix($time = '')
162
163 :param integer $time: Unix timestamp
164 :returns: integer
165
166Example
167
168::
169
Fumito Mizunobb859fd2011-10-14 20:05:34 +0900170 $mysql = '20061124092345';
171 $unix = mysql_to_unix($mysql);
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
173unix_to_human()
174===============
175
176Takes a Unix timestamp as input and returns it in a human readable
177format with this prototype
178
179.. php:method:: unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
180
181 :param integer $time: Unix timestamp
182 :param boolean $seconds: whether to show seconds
183 :param string $fmt: format: us or euro
184 :returns: integer
185
186Example
187
188::
189
190 YYYY-MM-DD HH:MM:SS AM/PM
191
192This can be useful if you need to display a date in a form field for
193submission.
194
195The time can be formatted with or without seconds, and it can be set to
196European or US format. If only the timestamp is submitted it will return
197the time without seconds formatted for the U.S. Examples
198
199::
200
201 $now = time();
202 echo unix_to_human($now); // U.S. time, no seconds
203 echo unix_to_human($now, TRUE, 'us'); // U.S. time with seconds
204 echo unix_to_human($now, TRUE, 'eu'); // Euro time with seconds
205
206human_to_unix()
207===============
208
209The opposite of the above function. Takes a "human" time as input and
210returns it as Unix. This function is useful if you accept "human"
211formatted dates submitted via a form. Returns FALSE (boolean) if the
212date string passed to it is not formatted as indicated above.
213
214.. php:method:: human_to_unix($datestr = '')
215
216 :param integer $datestr: Date String
217 :returns: integer
218
219Example:
220
221::
222
223 $now = time();
224 $human = unix_to_human($now);
225 $unix = human_to_unix($human);
226
227nice_date()
228===========
229
230This function can take a number poorly-formed date formats and convert
231them into something useful. It also accepts well-formed dates.
232
233The function will return a Unix timestamp by default. You can,
234optionally, pass a format string (the same type as the PHP date function
235accepts) as the second parameter.
236
237.. php:method:: nice_date($bad_date = '', $format = FALSE)
238
239 :param integer $bad_date: The terribly formatted date-like string
240 :param string $format: Date format to return (same as php date function)
241 :returns: string
242
243Example
244
245::
246
247 $bad_time = 199605 // Should Produce: 1996-05-01
248 $better_time = nice_date($bad_time,'Y-m-d');
249 $bad_time = 9-11-2001 // Should Produce: 2001-09-11
250 $better_time = nice_date($human,'Y-m-d');
251
252timespan()
253==========
254
255Formats a unix timestamp so that is appears similar to this
256
257::
258
259 1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes
260
261The first parameter must contain a Unix timestamp. The second parameter
262must contain a timestamp that is greater that the first timestamp. If
Roger Herbertb81f9092012-03-12 12:46:02 +0000263the second parameter empty, the current time will be used. The third
264parameter is optional and limits the number of time units to display.
265The most common purpose for this function is to show how much time has
266elapsed from some point in time in the past to now.
Derek Jones8ede1a22011-10-05 13:34:52 -0500267
Roger Herbertb81f9092012-03-12 12:46:02 +0000268.. php:method:: timespan($seconds = 1, $time = '', $units = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500269
270 :param integer $seconds: a number of seconds
271 :param string $time: Unix timestamp
Roger Herbertb81f9092012-03-12 12:46:02 +0000272 :param integer $units: a number of time units to display
Derek Jones8ede1a22011-10-05 13:34:52 -0500273 :returns: string
274
275Example
276
277::
278
279 $post_date = '1079621429';
280 $now = time();
Roger Herbertb81f9092012-03-12 12:46:02 +0000281 $units = 2;
282 echo timespan($post_date, $now, $units);
Derek Jones8ede1a22011-10-05 13:34:52 -0500283
284.. note:: The text generated by this function is found in the following language
285 file: language/<your_lang>/date_lang.php
286
287days_in_month()
288===============
289
290Returns the number of days in a given month/year. Takes leap years into
291account.
292
293.. php:method:: days_in_month($month = 0, $year = '')
294
295 :param integer $month: a numeric month
296 :param integer $year: a numeric year
297 :returns: integer
298
299Example
300
301::
302
303 echo days_in_month(06, 2005);
304
305If the second parameter is empty, the current year will be used.
306
307timezones()
308===========
309
310Takes a timezone reference (for a list of valid timezones, see the
311"Timezone Reference" below) and returns the number of hours offset from
312UTC.
313
314.. php:method:: timezones($tz = '')
315
316 :param string $tz: a numeric timezone
317 :returns: string
318
319Example
320
321::
322
323 echo timezones('UM5');
324
325
326This function is useful when used with `timezone_menu()`.
327
328timezone_menu()
329===============
330
331Generates a pull-down menu of timezones, like this one:
332
333
334.. raw:: html
335
336 <form action="#">
337 <select name="timezones">
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000338 <option value='UM12'>(UTC -12:00) Baker/Howland Island</option>
339 <option value='UM11'>(UTC -11:00) Samoa Time Zone, Niue</option>
340 <option value='UM10'>(UTC -10:00) Hawaii-Aleutian Standard Time, Cook Islands, Tahiti</option>
341 <option value='UM95'>(UTC -9:30) Marquesas Islands</option>
342 <option value='UM9'>(UTC -9:00) Alaska Standard Time, Gambier Islands</option>
343 <option value='UM8'>(UTC -8:00) Pacific Standard Time, Clipperton Island</option>
344 <option value='UM7'>(UTC -7:00) Mountain Standard Time</option>
345 <option value='UM6'>(UTC -6:00) Central Standard Time</option>
346 <option value='UM5'>(UTC -5:00) Eastern Standard Time, Western Caribbean Standard Time</option>
347 <option value='UM45'>(UTC -4:30) Venezuelan Standard Time</option>
348 <option value='UM4'>(UTC -4:00) Atlantic Standard Time, Eastern Caribbean Standard Time</option>
349 <option value='UM35'>(UTC -3:30) Newfoundland Standard Time</option>
350 <option value='UM3'>(UTC -3:00) Argentina, Brazil, French Guiana, Uruguay</option>
351 <option value='UM2'>(UTC -2:00) South Georgia/South Sandwich Islands</option>
352 <option value='UM1'>(UTC -1:00) Azores, Cape Verde Islands</option>
353 <option value='UTC' selected='selected'>(UTC) Greenwich Mean Time, Western European Time</option>
354 <option value='UP1'>(UTC +1:00) Central European Time, West Africa Time</option>
355 <option value='UP2'>(UTC +2:00) Central Africa Time, Eastern European Time, Kaliningrad Time</option>
356 <option value='UP3'>(UTC +3:00) Moscow Time, East Africa Time</option>
357 <option value='UP35'>(UTC +3:30) Iran Standard Time</option>
358 <option value='UP4'>(UTC +4:00) Azerbaijan Standard Time, Samara Time</option>
359 <option value='UP45'>(UTC +4:30) Afghanistan</option>
360 <option value='UP5'>(UTC +5:00) Pakistan Standard Time, Yekaterinburg Time</option>
361 <option value='UP55'>(UTC +5:30) Indian Standard Time, Sri Lanka Time</option>
362 <option value='UP575'>(UTC +5:45) Nepal Time</option>
363 <option value='UP6'>(UTC +6:00) Bangladesh Standard Time, Bhutan Time, Omsk Time</option>
364 <option value='UP65'>(UTC +6:30) Cocos Islands, Myanmar</option>
365 <option value='UP7'>(UTC +7:00) Krasnoyarsk Time, Cambodia, Laos, Thailand, Vietnam</option>
366 <option value='UP8'>(UTC +8:00) Australian Western Standard Time, Beijing Time, Irkutsk Time</option>
367 <option value='UP875'>(UTC +8:45) Australian Central Western Standard Time</option>
368 <option value='UP9'>(UTC +9:00) Japan Standard Time, Korea Standard Time, Yakutsk Time</option>
369 <option value='UP95'>(UTC +9:30) Australian Central Standard Time</option>
370 <option value='UP10'>(UTC +10:00) Australian Eastern Standard Time, Vladivostok Time</option>
371 <option value='UP105'>(UTC +10:30) Lord Howe Island</option>
372 <option value='UP11'>(UTC +11:00) Magadan Time, Solomon Islands, Vanuatu</option>
373 <option value='UP115'>(UTC +11:30) Norfolk Island</option>
374 <option value='UP12'>(UTC +12:00) Fiji, Gilbert Islands, Kamchatka Time, New Zealand Standard Time</option>
375 <option value='UP1275'>(UTC +12:45) Chatham Islands Standard Time</option>
376 <option value='UP13'>(UTC +13:00) Phoenix Islands Time, Tonga</option>
377 <option value='UP14'>(UTC +14:00) Line Islands</option>
Derek Jones8ede1a22011-10-05 13:34:52 -0500378 </select>
379 </form>
380
381
382This menu is useful if you run a membership site in which your users are
383allowed to set their local timezone value.
384
385The first parameter lets you set the "selected" state of the menu. For
386example, to set Pacific time as the default you will do this
387
388.. php:method:: timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
389
390 :param string $default: timezone
391 :param string $class: classname
392 :param string $name: menu name
393 :returns: string
394
395Example:
396
397::
398
399 echo timezone_menu('UM8');
400
401Please see the timezone reference below to see the values of this menu.
402
403The second parameter lets you set a CSS class name for the menu.
404
405.. note:: The text contained in the menu is found in the following
406 language file: `language/<your_lang>/date_lang.php`
407
408
409Timezone Reference
410==================
411
412The following table indicates each timezone and its location.
413
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000414Note some of the location lists have been abridged for clarity and formatting.
415
Derek Jones8ede1a22011-10-05 13:34:52 -0500416+------------+----------------------------------------------------------------+
417| Time Zone | Location |
418+============+================================================================+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000419| UM12 | (UTC - 12:00) Baker/Howland Island |
Derek Jones8ede1a22011-10-05 13:34:52 -0500420+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000421| UM11 | (UTC - 11:00) Samoa Time Zone, Niue |
Derek Jones8ede1a22011-10-05 13:34:52 -0500422+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000423| UM10 | (UTC - 10:00) Hawaii-Aleutian Standard Time, Cook Islands |
Derek Jones8ede1a22011-10-05 13:34:52 -0500424+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000425| UM95 | (UTC - 09:30) Marquesas Islands |
Derek Jones8ede1a22011-10-05 13:34:52 -0500426+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000427| UM9 | (UTC - 09:00) Alaska Standard Time, Gambier Islands |
Derek Jones8ede1a22011-10-05 13:34:52 -0500428+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000429| UM8 | (UTC - 08:00) Pacific Standard Time, Clipperton Island |
Derek Jones8ede1a22011-10-05 13:34:52 -0500430+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000431| UM7 | (UTC - 11:00) Mountain Standard Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500432+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000433| UM6 | (UTC - 06:00) Central Standard Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500434+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000435| UM5 | (UTC - 05:00) Eastern Standard Time, Western Caribbean |
Derek Jones8ede1a22011-10-05 13:34:52 -0500436+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000437| UM45 | (UTC - 04:30) Venezuelan Standard Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500438+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000439| UM4 | (UTC - 04:00) Atlantic Standard Time, Eastern Caribbean |
Derek Jones8ede1a22011-10-05 13:34:52 -0500440+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000441| UM35 | (UTC - 03:30) Newfoundland Standard Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500442+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000443| UM3 | (UTC - 03:00) Argentina, Brazil, French Guiana, Uruguay |
Derek Jones8ede1a22011-10-05 13:34:52 -0500444+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000445| UM2 | (UTC - 02:00) South Georgia/South Sandwich Islands |
Derek Jones8ede1a22011-10-05 13:34:52 -0500446+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000447| UM1 | (UTC -1:00) Azores, Cape Verde Islands |
Derek Jones8ede1a22011-10-05 13:34:52 -0500448+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000449| UTC | (UTC) Greenwich Mean Time, Western European Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500450+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000451| UP1 | (UTC +1:00) Central European Time, West Africa Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500452+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000453| UP2 | (UTC +2:00) Central Africa Time, Eastern European Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500454+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000455| UP3 | (UTC +3:00) Moscow Time, East Africa Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500456+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000457| UP35 | (UTC +3:30) Iran Standard Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500458+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000459| UP4 | (UTC +4:00) Azerbaijan Standard Time, Samara Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500460+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000461| UP45 | (UTC +4:30) Afghanistan |
Derek Jones8ede1a22011-10-05 13:34:52 -0500462+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000463| UP5 | (UTC +5:00) Pakistan Standard Time, Yekaterinburg Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500464+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000465| UP55 | (UTC +5:30) Indian Standard Time, Sri Lanka Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500466+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000467| UP575 | (UTC +5:45) Nepal Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500468+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000469| UP6 | (UTC +6:00) Bangladesh Standard Time, Bhutan Time, Omsk Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500470+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000471| UP65 | (UTC +6:30) Cocos Islands, Myanmar |
Derek Jones8ede1a22011-10-05 13:34:52 -0500472+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000473| UP7 | (UTC +7:00) Krasnoyarsk Time, Cambodia, Laos, Thailand, Vietnam|
Derek Jones8ede1a22011-10-05 13:34:52 -0500474+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000475| UP8 | (UTC +8:00) Australian Western Standard Time, Beijing Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500476+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000477| UP875 | (UTC +8:45) Australian Central Western Standard Time |
478+------------+----------------------------------------------------------------+
479| UP9 | (UTC +9:00) Japan Standard Time, Korea Standard Time, Yakutsk |
480+------------+----------------------------------------------------------------+
481| UP95 | (UTC +9:30) Australian Central Standard Time |
482+------------+----------------------------------------------------------------+
483| UP10 | (UTC +10:00) Australian Eastern Standard Time, Vladivostok Time|
484+------------+----------------------------------------------------------------+
485| UP105 | (UTC +10:30) Lord Howe Island |
486+------------+----------------------------------------------------------------+
487| UP11 | (UTC +11:00) Magadan Time, Solomon Islands, Vanuatu |
488+------------+----------------------------------------------------------------+
489| UP115 | (UTC +11:30) Norfolk Island |
490+------------+----------------------------------------------------------------+
491| UP12 | (UTC +12:00) Fiji, Gilbert Islands, Kamchatka, New Zealand |
492+------------+----------------------------------------------------------------+
493| UP1275 | (UTC +12:45) Chatham Islands Standard Time |
494+------------+----------------------------------------------------------------+
495| UP13 | (UTC +13:00) Phoenix Islands Time, Tonga |
496+------------+----------------------------------------------------------------+
497| UP14 | (UTC +14:00) Line Islands |
Derek Jones8ede1a22011-10-05 13:34:52 -0500498+------------+----------------------------------------------------------------+