blob: 1b7177fc260ef1ef48de8f00bafb0a1bcd9ad863 [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 Eguia74009652012-06-13 22:57:50 +020023Returns the current time as a Unix timestamp, referenced either to your
24server's local time or any PHP suported timezone, based on the "time reference"
25setting in your config file. If you do not intend to set your master time reference
26to any other PHP suported timezone (which you'll typically do if you run a site that
27lets each user set their own timezone settings) there is no benefit to using this
28function over PHP's time() function.
Derek Jones8ede1a22011-10-05 13:34:52 -050029
Iban Eguia7bf0a4f2012-03-27 18:36:15 +020030.. php:method:: now($timezone = NULL)
31
32 :param string $timezone: The timezone you want to be returned
33 :returns: integer
34
35::
Iban Eguiafeb14da2012-06-12 16:09:36 +020036 echo now("Australia/Victoria");
Iban Eguia7bf0a4f2012-03-27 18:36:15 +020037
Iban Eguiafeb14da2012-06-12 16:09:36 +020038If a timezone is not provided, it will return time() based on "time_reference" setting.
Derek Jones8ede1a22011-10-05 13:34:52 -050039
40mdate()
41=======
42
43This function is identical to PHPs `date() <http://www.php.net/date>`_
44function, except that it lets you use MySQL style date codes, where each
45code letter is preceded with a percent sign: %Y %m %d etc.
46
47The benefit of doing dates this way is that you don't have to worry
48about escaping any characters that are not date codes, as you would
49normally have to do with the date() function. Example
50
51.. php:method:: mdate($datestr = '', $time = '')
52
53 :param string $datestr: Date String
54 :param integer $time: time
55 :returns: integer
56
57
58::
59
60 $datestring = "Year: %Y Month: %m Day: %d - %h:%i %a";
61 $time = time();
62 echo mdate($datestring, $time);
63
64If a timestamp is not included in the second parameter the current time
65will be used.
66
67standard_date()
68===============
69
70Lets you generate a date string in one of several standardized formats.
71Example
72
73.. php:method:: standard_date($fmt = 'DATE_RFC822', $time = '')
74
75 :param string $fmt: the chosen format
76 :param string $time: Unix timestamp
77 :returns: string
78
79::
80
81 $format = 'DATE_RFC822';
82 $time = time();
83 echo standard_date($format, $time);
84
85The first parameter must contain the format, the second parameter must
86contain the date as a Unix timestamp.
87
88Supported formats:
89
90+----------------+------------------------+-----------------------------------+
91| Constant | Description | Example |
92+================+========================+===================================+
93| DATE_ATOM | Atom | 2005-08-15T16:13:03+0000 |
94+----------------+------------------------+-----------------------------------+
95| DATE_COOKIE | HTTP Cookies | Sun, 14 Aug 2005 16:13:03 UTC |
96+----------------+------------------------+-----------------------------------+
97| DATE_ISO8601 | ISO-8601 | 2005-08-14T16:13:03+00:00 |
98+----------------+------------------------+-----------------------------------+
99| DATE_RFC822 | RFC 822 | Sun, 14 Aug 05 16:13:03 UTC |
100+----------------+------------------------+-----------------------------------+
101| DATE_RFC850 | RFC 850 | Sunday, 14-Aug-05 16:13:03 UTC |
102+----------------+------------------------+-----------------------------------+
103| DATE_RFC1036 | RFC 1036 | Sunday, 14-Aug-05 16:13:03 UTC |
104+----------------+------------------------+-----------------------------------+
105| DATE_RFC1123 | RFC 1123 | Sun, 14 Aug 2005 16:13:03 UTC |
106+----------------+------------------------+-----------------------------------+
107| DATE_RFC2822 | RFC 2822 | Sun, 14 Aug 2005 16:13:03 +0000 |
108+----------------+------------------------+-----------------------------------+
109| DATE_RSS | RSS | Sun, 14 Aug 2005 16:13:03 UTC |
110+----------------+------------------------+-----------------------------------+
111| DATE_W3C | W3C | 2005-08-14T16:13:03+0000 |
112+----------------+------------------------+-----------------------------------+
113
114
115local_to_gmt()
116==============
117
118Takes a Unix timestamp as input and returns it as GMT.
119
120.. php:method:: local_to_gmt($time = '')
121
122 :param integer $time: Unix timestamp
123 :returns: string
124
125Example:
126
127::
128
129 $now = time();
130 $gmt = local_to_gmt($now);
131
132gmt_to_local()
133==============
134
135Takes a Unix timestamp (referenced to GMT) as input, and converts it to
136a localized timestamp based on the timezone and Daylight Saving time
137submitted.
138
139.. php:method:: gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
140
141 :param integer $time: Unix timestamp
142 :param string $timezone: timezone
143 :param boolean $dst: whether DST is active
144 :returns: integer
145
146Example
147
148::
149
150 $timestamp = '1140153693';
151 $timezone = 'UM8';
152 $daylight_saving = TRUE;
153 echo gmt_to_local($timestamp, $timezone, $daylight_saving);
154
155
156.. note:: For a list of timezones see the reference at the bottom of this page.
157
158
159mysql_to_unix()
160===============
161
162Takes a MySQL Timestamp as input and returns it as Unix.
163
164.. php:method:: mysql_to_unix($time = '')
165
166 :param integer $time: Unix timestamp
167 :returns: integer
168
169Example
170
171::
172
Fumito Mizunobb859fd2011-10-14 20:05:34 +0900173 $mysql = '20061124092345';
174 $unix = mysql_to_unix($mysql);
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
176unix_to_human()
177===============
178
179Takes a Unix timestamp as input and returns it in a human readable
180format with this prototype
181
182.. php:method:: unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
183
184 :param integer $time: Unix timestamp
185 :param boolean $seconds: whether to show seconds
186 :param string $fmt: format: us or euro
187 :returns: integer
188
189Example
190
191::
192
193 YYYY-MM-DD HH:MM:SS AM/PM
194
195This can be useful if you need to display a date in a form field for
196submission.
197
198The time can be formatted with or without seconds, and it can be set to
199European or US format. If only the timestamp is submitted it will return
200the time without seconds formatted for the U.S. Examples
201
202::
203
204 $now = time();
205 echo unix_to_human($now); // U.S. time, no seconds
206 echo unix_to_human($now, TRUE, 'us'); // U.S. time with seconds
207 echo unix_to_human($now, TRUE, 'eu'); // Euro time with seconds
208
209human_to_unix()
210===============
211
212The opposite of the above function. Takes a "human" time as input and
213returns it as Unix. This function is useful if you accept "human"
214formatted dates submitted via a form. Returns FALSE (boolean) if the
215date string passed to it is not formatted as indicated above.
216
217.. php:method:: human_to_unix($datestr = '')
218
219 :param integer $datestr: Date String
220 :returns: integer
221
222Example:
223
224::
225
226 $now = time();
227 $human = unix_to_human($now);
228 $unix = human_to_unix($human);
229
230nice_date()
231===========
232
233This function can take a number poorly-formed date formats and convert
234them into something useful. It also accepts well-formed dates.
235
236The function will return a Unix timestamp by default. You can,
237optionally, pass a format string (the same type as the PHP date function
238accepts) as the second parameter.
239
240.. php:method:: nice_date($bad_date = '', $format = FALSE)
241
242 :param integer $bad_date: The terribly formatted date-like string
243 :param string $format: Date format to return (same as php date function)
244 :returns: string
245
246Example
247
248::
249
250 $bad_time = 199605 // Should Produce: 1996-05-01
251 $better_time = nice_date($bad_time,'Y-m-d');
252 $bad_time = 9-11-2001 // Should Produce: 2001-09-11
253 $better_time = nice_date($human,'Y-m-d');
254
255timespan()
256==========
257
258Formats a unix timestamp so that is appears similar to this
259
260::
261
262 1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes
263
264The first parameter must contain a Unix timestamp. The second parameter
265must contain a timestamp that is greater that the first timestamp. If
Roger Herbertb81f9092012-03-12 12:46:02 +0000266the second parameter empty, the current time will be used. The third
267parameter is optional and limits the number of time units to display.
268The most common purpose for this function is to show how much time has
269elapsed from some point in time in the past to now.
Derek Jones8ede1a22011-10-05 13:34:52 -0500270
Roger Herbertb81f9092012-03-12 12:46:02 +0000271.. php:method:: timespan($seconds = 1, $time = '', $units = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500272
273 :param integer $seconds: a number of seconds
274 :param string $time: Unix timestamp
Roger Herbertb81f9092012-03-12 12:46:02 +0000275 :param integer $units: a number of time units to display
Derek Jones8ede1a22011-10-05 13:34:52 -0500276 :returns: string
277
278Example
279
280::
281
282 $post_date = '1079621429';
283 $now = time();
Roger Herbertb81f9092012-03-12 12:46:02 +0000284 $units = 2;
285 echo timespan($post_date, $now, $units);
Derek Jones8ede1a22011-10-05 13:34:52 -0500286
287.. note:: The text generated by this function is found in the following language
288 file: language/<your_lang>/date_lang.php
289
290days_in_month()
291===============
292
293Returns the number of days in a given month/year. Takes leap years into
294account.
295
296.. php:method:: days_in_month($month = 0, $year = '')
297
298 :param integer $month: a numeric month
299 :param integer $year: a numeric year
300 :returns: integer
301
302Example
303
304::
305
306 echo days_in_month(06, 2005);
307
308If the second parameter is empty, the current year will be used.
309
310timezones()
311===========
312
313Takes a timezone reference (for a list of valid timezones, see the
314"Timezone Reference" below) and returns the number of hours offset from
315UTC.
316
317.. php:method:: timezones($tz = '')
318
319 :param string $tz: a numeric timezone
320 :returns: string
321
322Example
323
324::
325
326 echo timezones('UM5');
327
328
329This function is useful when used with `timezone_menu()`.
330
331timezone_menu()
332===============
333
334Generates a pull-down menu of timezones, like this one:
335
336
337.. raw:: html
338
339 <form action="#">
340 <select name="timezones">
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000341 <option value='UM12'>(UTC -12:00) Baker/Howland Island</option>
342 <option value='UM11'>(UTC -11:00) Samoa Time Zone, Niue</option>
343 <option value='UM10'>(UTC -10:00) Hawaii-Aleutian Standard Time, Cook Islands, Tahiti</option>
344 <option value='UM95'>(UTC -9:30) Marquesas Islands</option>
345 <option value='UM9'>(UTC -9:00) Alaska Standard Time, Gambier Islands</option>
346 <option value='UM8'>(UTC -8:00) Pacific Standard Time, Clipperton Island</option>
347 <option value='UM7'>(UTC -7:00) Mountain Standard Time</option>
348 <option value='UM6'>(UTC -6:00) Central Standard Time</option>
349 <option value='UM5'>(UTC -5:00) Eastern Standard Time, Western Caribbean Standard Time</option>
350 <option value='UM45'>(UTC -4:30) Venezuelan Standard Time</option>
351 <option value='UM4'>(UTC -4:00) Atlantic Standard Time, Eastern Caribbean Standard Time</option>
352 <option value='UM35'>(UTC -3:30) Newfoundland Standard Time</option>
353 <option value='UM3'>(UTC -3:00) Argentina, Brazil, French Guiana, Uruguay</option>
354 <option value='UM2'>(UTC -2:00) South Georgia/South Sandwich Islands</option>
355 <option value='UM1'>(UTC -1:00) Azores, Cape Verde Islands</option>
356 <option value='UTC' selected='selected'>(UTC) Greenwich Mean Time, Western European Time</option>
357 <option value='UP1'>(UTC +1:00) Central European Time, West Africa Time</option>
358 <option value='UP2'>(UTC +2:00) Central Africa Time, Eastern European Time, Kaliningrad Time</option>
359 <option value='UP3'>(UTC +3:00) Moscow Time, East Africa Time</option>
360 <option value='UP35'>(UTC +3:30) Iran Standard Time</option>
361 <option value='UP4'>(UTC +4:00) Azerbaijan Standard Time, Samara Time</option>
362 <option value='UP45'>(UTC +4:30) Afghanistan</option>
363 <option value='UP5'>(UTC +5:00) Pakistan Standard Time, Yekaterinburg Time</option>
364 <option value='UP55'>(UTC +5:30) Indian Standard Time, Sri Lanka Time</option>
365 <option value='UP575'>(UTC +5:45) Nepal Time</option>
366 <option value='UP6'>(UTC +6:00) Bangladesh Standard Time, Bhutan Time, Omsk Time</option>
367 <option value='UP65'>(UTC +6:30) Cocos Islands, Myanmar</option>
368 <option value='UP7'>(UTC +7:00) Krasnoyarsk Time, Cambodia, Laos, Thailand, Vietnam</option>
369 <option value='UP8'>(UTC +8:00) Australian Western Standard Time, Beijing Time, Irkutsk Time</option>
370 <option value='UP875'>(UTC +8:45) Australian Central Western Standard Time</option>
371 <option value='UP9'>(UTC +9:00) Japan Standard Time, Korea Standard Time, Yakutsk Time</option>
372 <option value='UP95'>(UTC +9:30) Australian Central Standard Time</option>
373 <option value='UP10'>(UTC +10:00) Australian Eastern Standard Time, Vladivostok Time</option>
374 <option value='UP105'>(UTC +10:30) Lord Howe Island</option>
375 <option value='UP11'>(UTC +11:00) Magadan Time, Solomon Islands, Vanuatu</option>
376 <option value='UP115'>(UTC +11:30) Norfolk Island</option>
377 <option value='UP12'>(UTC +12:00) Fiji, Gilbert Islands, Kamchatka Time, New Zealand Standard Time</option>
378 <option value='UP1275'>(UTC +12:45) Chatham Islands Standard Time</option>
379 <option value='UP13'>(UTC +13:00) Phoenix Islands Time, Tonga</option>
380 <option value='UP14'>(UTC +14:00) Line Islands</option>
Derek Jones8ede1a22011-10-05 13:34:52 -0500381 </select>
382 </form>
383
384
385This menu is useful if you run a membership site in which your users are
386allowed to set their local timezone value.
387
388The first parameter lets you set the "selected" state of the menu. For
389example, to set Pacific time as the default you will do this
390
391.. php:method:: timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
392
393 :param string $default: timezone
394 :param string $class: classname
395 :param string $name: menu name
396 :returns: string
397
398Example:
399
400::
401
402 echo timezone_menu('UM8');
403
404Please see the timezone reference below to see the values of this menu.
405
406The second parameter lets you set a CSS class name for the menu.
407
408.. note:: The text contained in the menu is found in the following
409 language file: `language/<your_lang>/date_lang.php`
410
411
412Timezone Reference
413==================
414
415The following table indicates each timezone and its location.
416
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000417Note some of the location lists have been abridged for clarity and formatting.
418
Derek Jones8ede1a22011-10-05 13:34:52 -0500419+------------+----------------------------------------------------------------+
420| Time Zone | Location |
421+============+================================================================+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000422| UM12 | (UTC - 12:00) Baker/Howland Island |
Derek Jones8ede1a22011-10-05 13:34:52 -0500423+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000424| UM11 | (UTC - 11:00) Samoa Time Zone, Niue |
Derek Jones8ede1a22011-10-05 13:34:52 -0500425+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000426| UM10 | (UTC - 10:00) Hawaii-Aleutian Standard Time, Cook Islands |
Derek Jones8ede1a22011-10-05 13:34:52 -0500427+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000428| UM95 | (UTC - 09:30) Marquesas Islands |
Derek Jones8ede1a22011-10-05 13:34:52 -0500429+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000430| UM9 | (UTC - 09:00) Alaska Standard Time, Gambier Islands |
Derek Jones8ede1a22011-10-05 13:34:52 -0500431+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000432| UM8 | (UTC - 08:00) Pacific Standard Time, Clipperton Island |
Derek Jones8ede1a22011-10-05 13:34:52 -0500433+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000434| UM7 | (UTC - 11:00) Mountain Standard Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500435+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000436| UM6 | (UTC - 06:00) Central Standard Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500437+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000438| UM5 | (UTC - 05:00) Eastern Standard Time, Western Caribbean |
Derek Jones8ede1a22011-10-05 13:34:52 -0500439+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000440| UM45 | (UTC - 04:30) Venezuelan Standard Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500441+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000442| UM4 | (UTC - 04:00) Atlantic Standard Time, Eastern Caribbean |
Derek Jones8ede1a22011-10-05 13:34:52 -0500443+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000444| UM35 | (UTC - 03:30) Newfoundland Standard Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500445+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000446| UM3 | (UTC - 03:00) Argentina, Brazil, French Guiana, Uruguay |
Derek Jones8ede1a22011-10-05 13:34:52 -0500447+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000448| UM2 | (UTC - 02:00) South Georgia/South Sandwich Islands |
Derek Jones8ede1a22011-10-05 13:34:52 -0500449+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000450| UM1 | (UTC -1:00) Azores, Cape Verde Islands |
Derek Jones8ede1a22011-10-05 13:34:52 -0500451+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000452| UTC | (UTC) Greenwich Mean Time, Western European Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500453+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000454| UP1 | (UTC +1:00) Central European Time, West Africa Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500455+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000456| UP2 | (UTC +2:00) Central Africa Time, Eastern European Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500457+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000458| UP3 | (UTC +3:00) Moscow Time, East Africa Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500459+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000460| UP35 | (UTC +3:30) Iran Standard Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500461+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000462| UP4 | (UTC +4:00) Azerbaijan Standard Time, Samara Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500463+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000464| UP45 | (UTC +4:30) Afghanistan |
Derek Jones8ede1a22011-10-05 13:34:52 -0500465+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000466| UP5 | (UTC +5:00) Pakistan Standard Time, Yekaterinburg Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500467+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000468| UP55 | (UTC +5:30) Indian Standard Time, Sri Lanka Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500469+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000470| UP575 | (UTC +5:45) Nepal Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500471+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000472| UP6 | (UTC +6:00) Bangladesh Standard Time, Bhutan Time, Omsk Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500473+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000474| UP65 | (UTC +6:30) Cocos Islands, Myanmar |
Derek Jones8ede1a22011-10-05 13:34:52 -0500475+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000476| UP7 | (UTC +7:00) Krasnoyarsk Time, Cambodia, Laos, Thailand, Vietnam|
Derek Jones8ede1a22011-10-05 13:34:52 -0500477+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000478| UP8 | (UTC +8:00) Australian Western Standard Time, Beijing Time |
Derek Jones8ede1a22011-10-05 13:34:52 -0500479+------------+----------------------------------------------------------------+
Kwaan Onlinee90b6842012-01-31 10:15:30 +0000480| UP875 | (UTC +8:45) Australian Central Western Standard Time |
481+------------+----------------------------------------------------------------+
482| UP9 | (UTC +9:00) Japan Standard Time, Korea Standard Time, Yakutsk |
483+------------+----------------------------------------------------------------+
484| UP95 | (UTC +9:30) Australian Central Standard Time |
485+------------+----------------------------------------------------------------+
486| UP10 | (UTC +10:00) Australian Eastern Standard Time, Vladivostok Time|
487+------------+----------------------------------------------------------------+
488| UP105 | (UTC +10:30) Lord Howe Island |
489+------------+----------------------------------------------------------------+
490| UP11 | (UTC +11:00) Magadan Time, Solomon Islands, Vanuatu |
491+------------+----------------------------------------------------------------+
492| UP115 | (UTC +11:30) Norfolk Island |
493+------------+----------------------------------------------------------------+
494| UP12 | (UTC +12:00) Fiji, Gilbert Islands, Kamchatka, New Zealand |
495+------------+----------------------------------------------------------------+
496| UP1275 | (UTC +12:45) Chatham Islands Standard Time |
497+------------+----------------------------------------------------------------+
498| UP13 | (UTC +13:00) Phoenix Islands Time, Tonga |
499+------------+----------------------------------------------------------------+
500| UP14 | (UTC +14:00) Line Islands |
Derek Jones8ede1a22011-10-05 13:34:52 -0500501+------------+----------------------------------------------------------------+