blob: b8c3dd07618fe0aac037a8a60b0f4c308a0d64d1 [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.
24All PHP available timezones are supported.
Derek Jones8ede1a22011-10-05 13:34:52 -050025
Iban Eguia7bf0a4f2012-03-27 18:36:15 +020026.. php:method:: now($timezone = NULL)
27
28 :param string $timezone: The timezone you want to be returned
29 :returns: integer
30
31::
32
33 $tz = "Australia/Victoria";
34 echo now($tz);
35
36If a timezone is not provided, it will return time() based on "timezone" setting.
Derek Jones8ede1a22011-10-05 13:34:52 -050037
38mdate()
39=======
40
41This function is identical to PHPs `date() <http://www.php.net/date>`_
42function, except that it lets you use MySQL style date codes, where each
43code letter is preceded with a percent sign: %Y %m %d etc.
44
45The benefit of doing dates this way is that you don't have to worry
46about escaping any characters that are not date codes, as you would
47normally have to do with the date() function. Example
48
49.. php:method:: mdate($datestr = '', $time = '')
50
51 :param string $datestr: Date String
52 :param integer $time: time
53 :returns: integer
54
55
56::
57
58 $datestring = "Year: %Y Month: %m Day: %d - %h:%i %a";
59 $time = time();
60 echo mdate($datestring, $time);
61
62If a timestamp is not included in the second parameter the current time
63will be used.
64
65standard_date()
66===============
67
68Lets you generate a date string in one of several standardized formats.
69Example
70
71.. php:method:: standard_date($fmt = 'DATE_RFC822', $time = '')
72
73 :param string $fmt: the chosen format
74 :param string $time: Unix timestamp
75 :returns: string
76
77::
78
79 $format = 'DATE_RFC822';
80 $time = time();
81 echo standard_date($format, $time);
82
83The first parameter must contain the format, the second parameter must
84contain the date as a Unix timestamp.
85
86Supported formats:
87
88+----------------+------------------------+-----------------------------------+
89| Constant | Description | Example |
90+================+========================+===================================+
91| DATE_ATOM | Atom | 2005-08-15T16:13:03+0000 |
92+----------------+------------------------+-----------------------------------+
93| DATE_COOKIE | HTTP Cookies | Sun, 14 Aug 2005 16:13:03 UTC |
94+----------------+------------------------+-----------------------------------+
95| DATE_ISO8601 | ISO-8601 | 2005-08-14T16:13:03+00:00 |
96+----------------+------------------------+-----------------------------------+
97| DATE_RFC822 | RFC 822 | Sun, 14 Aug 05 16:13:03 UTC |
98+----------------+------------------------+-----------------------------------+
99| DATE_RFC850 | RFC 850 | Sunday, 14-Aug-05 16:13:03 UTC |
100+----------------+------------------------+-----------------------------------+
101| DATE_RFC1036 | RFC 1036 | Sunday, 14-Aug-05 16:13:03 UTC |
102+----------------+------------------------+-----------------------------------+
103| DATE_RFC1123 | RFC 1123 | Sun, 14 Aug 2005 16:13:03 UTC |
104+----------------+------------------------+-----------------------------------+
105| DATE_RFC2822 | RFC 2822 | Sun, 14 Aug 2005 16:13:03 +0000 |
106+----------------+------------------------+-----------------------------------+
107| DATE_RSS | RSS | Sun, 14 Aug 2005 16:13:03 UTC |
108+----------------+------------------------+-----------------------------------+
109| DATE_W3C | W3C | 2005-08-14T16:13:03+0000 |
110+----------------+------------------------+-----------------------------------+
111
112
113local_to_gmt()
114==============
115
116Takes a Unix timestamp as input and returns it as GMT.
117
118.. php:method:: local_to_gmt($time = '')
119
120 :param integer $time: Unix timestamp
121 :returns: string
122
123Example:
124
125::
126
127 $now = time();
128 $gmt = local_to_gmt($now);
129
130gmt_to_local()
131==============
132
133Takes a Unix timestamp (referenced to GMT) as input, and converts it to
134a localized timestamp based on the timezone and Daylight Saving time
135submitted.
136
137.. php:method:: gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
138
139 :param integer $time: Unix timestamp
140 :param string $timezone: timezone
141 :param boolean $dst: whether DST is active
142 :returns: integer
143
144Example
145
146::
147
148 $timestamp = '1140153693';
149 $timezone = 'UM8';
150 $daylight_saving = TRUE;
151 echo gmt_to_local($timestamp, $timezone, $daylight_saving);
152
153
154.. note:: For a list of timezones see the reference at the bottom of this page.
155
156
157mysql_to_unix()
158===============
159
160Takes a MySQL Timestamp as input and returns it as Unix.
161
162.. php:method:: mysql_to_unix($time = '')
163
164 :param integer $time: Unix timestamp
165 :returns: integer
166
167Example
168
169::
170
Fumito Mizunobb859fd2011-10-14 20:05:34 +0900171 $mysql = '20061124092345';
172 $unix = mysql_to_unix($mysql);
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
174unix_to_human()
175===============
176
177Takes a Unix timestamp as input and returns it in a human readable
178format with this prototype
179
180.. php:method:: unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
181
182 :param integer $time: Unix timestamp
183 :param boolean $seconds: whether to show seconds
184 :param string $fmt: format: us or euro
185 :returns: integer
186
187Example
188
189::
190
191 YYYY-MM-DD HH:MM:SS AM/PM
192
193This can be useful if you need to display a date in a form field for
194submission.
195
196The time can be formatted with or without seconds, and it can be set to
197European or US format. If only the timestamp is submitted it will return
198the time without seconds formatted for the U.S. Examples
199
200::
201
202 $now = time();
203 echo unix_to_human($now); // U.S. time, no seconds
204 echo unix_to_human($now, TRUE, 'us'); // U.S. time with seconds
205 echo unix_to_human($now, TRUE, 'eu'); // Euro time with seconds
206
207human_to_unix()
208===============
209
210The opposite of the above function. Takes a "human" time as input and
211returns it as Unix. This function is useful if you accept "human"
212formatted dates submitted via a form. Returns FALSE (boolean) if the
213date string passed to it is not formatted as indicated above.
214
215.. php:method:: human_to_unix($datestr = '')
216
217 :param integer $datestr: Date String
218 :returns: integer
219
220Example:
221
222::
223
224 $now = time();
225 $human = unix_to_human($now);
226 $unix = human_to_unix($human);
227
228nice_date()
229===========
230
231This function can take a number poorly-formed date formats and convert
232them into something useful. It also accepts well-formed dates.
233
234The function will return a Unix timestamp by default. You can,
235optionally, pass a format string (the same type as the PHP date function
236accepts) as the second parameter.
237
238.. php:method:: nice_date($bad_date = '', $format = FALSE)
239
240 :param integer $bad_date: The terribly formatted date-like string
241 :param string $format: Date format to return (same as php date function)
242 :returns: string
243
244Example
245
246::
247
248 $bad_time = 199605 // Should Produce: 1996-05-01
249 $better_time = nice_date($bad_time,'Y-m-d');
250 $bad_time = 9-11-2001 // Should Produce: 2001-09-11
251 $better_time = nice_date($human,'Y-m-d');
252
253timespan()
254==========
255
256Formats a unix timestamp so that is appears similar to this
257
258::
259
260 1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes
261
262The first parameter must contain a Unix timestamp. The second parameter
263must contain a timestamp that is greater that the first timestamp. If
Roger Herbertb81f9092012-03-12 12:46:02 +0000264the second parameter empty, the current time will be used. The third
265parameter is optional and limits the number of time units to display.
266The most common purpose for this function is to show how much time has
267elapsed from some point in time in the past to now.
Derek Jones8ede1a22011-10-05 13:34:52 -0500268
Roger Herbertb81f9092012-03-12 12:46:02 +0000269.. php:method:: timespan($seconds = 1, $time = '', $units = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500270
271 :param integer $seconds: a number of seconds
272 :param string $time: Unix timestamp
Roger Herbertb81f9092012-03-12 12:46:02 +0000273 :param integer $units: a number of time units to display
Derek Jones8ede1a22011-10-05 13:34:52 -0500274 :returns: string
275
276Example
277
278::
279
280 $post_date = '1079621429';
281 $now = time();
Roger Herbertb81f9092012-03-12 12:46:02 +0000282 $units = 2;
283 echo timespan($post_date, $now, $units);
Derek Jones8ede1a22011-10-05 13:34:52 -0500284
285.. note:: The text generated by this function is found in the following language
286 file: language/<your_lang>/date_lang.php
287
288days_in_month()
289===============
290
291Returns the number of days in a given month/year. Takes leap years into
292account.
293
294.. php:method:: days_in_month($month = 0, $year = '')
295
296 :param integer $month: a numeric month
297 :param integer $year: a numeric year
298 :returns: integer
299
300Example
301
302::
303
304 echo days_in_month(06, 2005);
305
306If the second parameter is empty, the current year will be used.
307
308timezones()
309===========
310
311Takes a timezone reference (for a list of valid timezones, see the
312"Timezone Reference" below) and returns the number of hours offset from
313UTC.
314
315.. php:method:: timezones($tz = '')
316
317 :param string $tz: a numeric timezone
318 :returns: string
319
320Example
321
322::
323
324 echo timezones('UM5');
325
326
327This function is useful when used with `timezone_menu()`.
328
329timezone_menu()
330===============
331
332Generates a pull-down menu of timezones, like this one:
333
334
335.. raw:: html
336
337 <form action="#">
338 <select name="timezones">
339 <option value='UM12'>(UTC - 12:00) Enitwetok, Kwajalien</option>
340 <option value='UM11'>(UTC - 11:00) Nome, Midway Island, Samoa</option>
341 <option value='UM10'>(UTC - 10:00) Hawaii</option>
342 <option value='UM9'>(UTC - 9:00) Alaska</option>
343 <option value='UM8'>(UTC - 8:00) Pacific Time</option>
344 <option value='UM7'>(UTC - 7:00) Mountain Time</option>
345 <option value='UM6'>(UTC - 6:00) Central Time, Mexico City</option>
346 <option value='UM5'>(UTC - 5:00) Eastern Time, Bogota, Lima, Quito</option>
347 <option value='UM4'>(UTC - 4:00) Atlantic Time, Caracas, La Paz</option>
348 <option value='UM25'>(UTC - 3:30) Newfoundland</option>
349 <option value='UM3'>(UTC - 3:00) Brazil, Buenos Aires, Georgetown, Falkland Is.</option>
350 <option value='UM2'>(UTC - 2:00) Mid-Atlantic, Ascention Is., St Helena</option>
351 <option value='UM1'>(UTC - 1:00) Azores, Cape Verde Islands</option>
352 <option value='UTC' selected='selected'>(UTC) Casablanca, Dublin, Edinburgh, London, Lisbon, Monrovia</option>
353 <option value='UP1'>(UTC + 1:00) Berlin, Brussels, Copenhagen, Madrid, Paris, Rome</option>
354 <option value='UP2'>(UTC + 2:00) Kaliningrad, South Africa, Warsaw</option>
355 <option value='UP3'>(UTC + 3:00) Baghdad, Riyadh, Moscow, Nairobi</option>
356 <option value='UP25'>(UTC + 3:30) Tehran</option>
357 <option value='UP4'>(UTC + 4:00) Adu Dhabi, Baku, Muscat, Tbilisi</option>
358 <option value='UP35'>(UTC + 4:30) Kabul</option>
359 <option value='UP5'>(UTC + 5:00) Islamabad, Karachi, Tashkent</option>
360 <option value='UP45'>(UTC + 5:30) Bombay, Calcutta, Madras, New Delhi</option>
361 <option value='UP6'>(UTC + 6:00) Almaty, Colomba, Dhaka</option>
362 <option value='UP7'>(UTC + 7:00) Bangkok, Hanoi, Jakarta</option>
363 <option value='UP8'>(UTC + 8:00) Beijing, Hong Kong, Perth, Singapore, Taipei</option>
364 <option value='UP9'>(UTC + 9:00) Osaka, Sapporo, Seoul, Tokyo, Yakutsk</option>
365 <option value='UP85'>(UTC + 9:30) Adelaide, Darwin</option>
366 <option value='UP10'>(UTC + 10:00) Melbourne, Papua New Guinea, Sydney, Vladivostok</option>
367 <option value='UP11'>(UTC + 11:00) Magadan, New Caledonia, Solomon Islands</option>
368 <option value='UP12'>(UTC + 12:00) Auckland, Wellington, Fiji, Marshall Island</option>
369 </select>
370 </form>
371
372
373This menu is useful if you run a membership site in which your users are
374allowed to set their local timezone value.
375
376The first parameter lets you set the "selected" state of the menu. For
377example, to set Pacific time as the default you will do this
378
379.. php:method:: timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
380
381 :param string $default: timezone
382 :param string $class: classname
383 :param string $name: menu name
384 :returns: string
385
386Example:
387
388::
389
390 echo timezone_menu('UM8');
391
392Please see the timezone reference below to see the values of this menu.
393
394The second parameter lets you set a CSS class name for the menu.
395
396.. note:: The text contained in the menu is found in the following
397 language file: `language/<your_lang>/date_lang.php`
398
399
400Timezone Reference
401==================
402
403The following table indicates each timezone and its location.
404
405+------------+----------------------------------------------------------------+
406| Time Zone | Location |
407+============+================================================================+
408| UM12 | (UTC - 12:00) Enitwetok, Kwajalien |
409+------------+----------------------------------------------------------------+
410| UM11 | (UTC - 11:00) Nome, Midway Island, Samoa |
411+------------+----------------------------------------------------------------+
412| UM10 | (UTC - 10:00) Hawaii |
413+------------+----------------------------------------------------------------+
414| UM9 | (UTC - 9:00) Alaska |
415+------------+----------------------------------------------------------------+
416| UM8 | (UTC - 8:00) Pacific Time |
417+------------+----------------------------------------------------------------+
418| UM7 | (UTC - 7:00) Mountain Time |
419+------------+----------------------------------------------------------------+
420| UM6 | (UTC - 6:00) Central Time, Mexico City |
421+------------+----------------------------------------------------------------+
422| UM5 | (UTC - 5:00) Eastern Time, Bogota, Lima, Quito |
423+------------+----------------------------------------------------------------+
424| UM4 | (UTC - 4:00) Atlantic Time, Caracas, La Paz |
425+------------+----------------------------------------------------------------+
426| UM25 | (UTC - 3:30) Newfoundland |
427+------------+----------------------------------------------------------------+
428| UM3 | (UTC - 3:00) Brazil, Buenos Aires, Georgetown, Falkland Is. |
429+------------+----------------------------------------------------------------+
430| UM2 | (UTC - 2:00) Mid-Atlantic, Ascention Is., St Helena |
431+------------+----------------------------------------------------------------+
432| UM1 | (UTC - 1:00) Azores, Cape Verde Islands |
433+------------+----------------------------------------------------------------+
434| UTC | (UTC) Casablanca, Dublin, Edinburgh, London, Lisbon, Monrovia |
435+------------+----------------------------------------------------------------+
436| UP1 | (UTC + 1:00) Berlin, Brussels, Copenhagen, Madrid, Paris, Rome |
437+------------+----------------------------------------------------------------+
438| UP2 | (UTC + 2:00) Kaliningrad, South Africa, Warsaw |
439+------------+----------------------------------------------------------------+
440| UP3 | (UTC + 3:00) Baghdad, Riyadh, Moscow, Nairobi |
441+------------+----------------------------------------------------------------+
442| UP25 | (UTC + 3:30) Tehran |
443+------------+----------------------------------------------------------------+
444| UP4 | (UTC + 4:00) Adu Dhabi, Baku, Muscat, Tbilisi |
445+------------+----------------------------------------------------------------+
446| UP35 | (UTC + 4:30) Kabul |
447+------------+----------------------------------------------------------------+
448| UP5 | (UTC + 5:00) Islamabad, Karachi, Tashkent |
449+------------+----------------------------------------------------------------+
450| UP45 | (UTC + 5:30) Bombay, Calcutta, Madras, New Delhi |
451+------------+----------------------------------------------------------------+
452| UP6 | (UTC + 6:00) Almaty, Colomba, Dhaka |
453+------------+----------------------------------------------------------------+
454| UP7 | (UTC + 7:00) Bangkok, Hanoi, Jakarta |
455+------------+----------------------------------------------------------------+
456| UP8 | (UTC + 8:00) Beijing, Hong Kong, Perth, Singapore, Taipei |
457+------------+----------------------------------------------------------------+
458| UP9 | (UTC + 9:00) Osaka, Sapporo, Seoul, Tokyo, Yakutsk |
459+------------+----------------------------------------------------------------+
460| UP85 | (UTC + 9:30) Adelaide, Darwin |
461+------------+----------------------------------------------------------------+
462| UP10 | (UTC + 10:00) Melbourne, Papua New Guinea, Sydney, Vladivostok |
463+------------+----------------------------------------------------------------+
464| UP11 | (UTC + 11:00) Magadan, New Caledonia, Solomon Islands |
465+------------+----------------------------------------------------------------+
466| UP12 | (UTC + 12:00) Auckland, Wellington, Fiji, Marshall Island |
467+------------+----------------------------------------------------------------+