blob: 8a9eb1bfd348b8c973644ba74095459b7d5d6df6 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Derek Allardafd99ac2008-01-19 19:59:14 +00002<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
adminb0dd10f2006-08-25 17:25:49 +00003<head>
4
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : Date Helper</title>
adminb0dd10f2006-08-25 17:25:49 +00006
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
Derek Allardb3412372007-10-25 12:15:16 +000013<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000014
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
Derek Allard3d879d52008-01-18 19:41:32 +000019<meta name='author' content='ExpressionEngine Dev Team' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000021
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
28<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Derek Allard197d10b2008-02-12 04:20:38 +000031<td><h1>CodeIgniter User Guide Version 1.6.1</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000033</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38
39<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
Derek Jones7a9193a2008-01-21 18:39:20 +000043<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45Directory Helper
46</td>
Derek Allardbc030912007-06-24 18:25:29 +000047<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
adminb0dd10f2006-08-25 17:25:49 +000048</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52<br clear="all" />
53
54
55<!-- START CONTENT -->
56<div id="content">
57
58
59<h1>Date Helper</h1>
60
61<p>The Date Helper file contains functions that help you work with dates.</p>
62
63
64<h2>Loading this Helper</h2>
65
66<p>This helper is loaded using the following code:</p>
67<code>$this->load->helper('date');</code>
68
69
70<p>The following functions are available:</p>
71
72<h2>now()</h2>
73
74<p>Returns the current time as a Unix timestamp, referenced either to your server's local time or GMT, based on the "time reference"
75setting in your config file. If you do not intend to set your master time reference to GMT (which you'll typically do if you
76run a site that lets each user set their own timezone settings) there is no benefit to using this function over PHP's time() function.
77</p>
78
79
80
81
82<h2>mdate()</h2>
83
admine334c472006-10-21 19:44:22 +000084<p>This function is identical to PHPs <a href="http://www.php.net/date">date()</a> function, except that it lets you
adminb0dd10f2006-08-25 17:25:49 +000085use MySQL style date codes, where each code letter is preceded with a percent sign: %Y %m %d etc.</p>
86
87<p>The benefit of doing dates this way is that you don't have to worry about escaping any characters that
88are not date codes, as you would normally have to do with the date() function. Example:</p>
89
90<code>$datestring = "Year: %Y Month: %m Day: %d - %h:%i %a";<br />
91$time = time();<br />
92<br />
93echo mdate($datestring, $time);</code>
94
95<p>If a timestamp is not included in the second parameter the current time will be used.</p>
96
97
admine8f6eb62006-10-02 06:46:16 +000098<h2>standard_date()</h2>
99
100<p>Lets you generate a date string in one of several standardized formats. Example:</p>
101
102<code>
103$format = 'DATE_RFC822';<br />
104$time = time();<br />
105<br />
106echo standard_date($format, $time);
107</code>
108
109<p>The first parameter must contain the format, the second parameter must contain the date as a Unix timestamp.</p>
110
111<p>Supported formats:</p>
112
113<ul>
114<li>DATE_ATOM</li>
115<li>DATE_COOKIE</li>
116<li>DATE_ISO8601</li>
117<li>DATE_RFC822</li>
118<li>DATE_RFC850</li>
119<li>DATE_RFC1036</li>
120<li>DATE_RFC1123</li>
121<li>DATE_RFC2822</li>
122<li>DATE_RSS</li>
123<li>DATE_W3C</li>
124</ul>
125
126
127
128
adminb0dd10f2006-08-25 17:25:49 +0000129<h2>local_to_gmt()</h2>
130
131<p>Takes a Unix timestamp as input and returns it as GMT. Example:</p>
132
133<code>$now = time();<br />
134<br />
135$gmt = local_to_gmt($now);</code>
136
137
138<h2>gmt_to_local()</h2>
139
admine334c472006-10-21 19:44:22 +0000140<p>Takes a Unix timestamp (referenced to GMT) as input, and converts it to a localized timestamp based on the
adminb0dd10f2006-08-25 17:25:49 +0000141timezone and Daylight Saving time submitted. Example:</p>
142
143<code>
144$timestamp = '1140153693';<br />
145$timezone = 'UM8';<br />
146$daylight_saving = TRUE;<br />
147<br />
Derek Allarda4d6b3f2007-10-28 15:33:52 +0000148echo gmt_to_local($timestamp, $timezone, $daylight_saving);</code>
adminb0dd10f2006-08-25 17:25:49 +0000149
150<p><strong>Note:</strong> For a list of timezones see the reference at the bottom of this page.</p>
151
152<h2>mysql_to_unix()</h2>
153
154<p>Takes a MySQL Timestamp as input and returns it as Unix. Example:</p>
155
156<code>$mysql = '20061124092345';<br />
157<br />
158$unix = mysql_to_unix($mysql);</code>
159
160
161<h2>unix_to_human()</h2>
162
163<p>Takes a Unix timestamp as input and returns it in a human readable format with this prototype:</p>
164
165<code>YYYY-MM-DD HH:MM:SS AM/PM</code>
166
167<p>This can be useful if you need to display a date in a form field for submission.</p>
168
admine334c472006-10-21 19:44:22 +0000169<p>The time can be formatted with or without seconds, and it can be set to European or US format. If only
Derek Allardc6441282007-07-04 23:54:32 +0000170the timestamp is submitted it will return the time without seconds formatted for the U.S. Examples:</p>
adminb0dd10f2006-08-25 17:25:49 +0000171
172<code>$now = time();<br />
173<br />
174echo unix_to_human($now); // U.S. time, no seconds<br />
175<br />
176echo unix_to_human($now, TRUE, 'us'); // U.S. time with seconds<br />
177<br />
178echo unix_to_human($now, TRUE, 'eu'); // Euro time with seconds</code>
179
180
181<h2>human_to_unix()</h2>
182
admine334c472006-10-21 19:44:22 +0000183<p>The opposite of the above function. Takes a "human" time as input and returns it as Unix. This function is
184useful if you accept "human" formatted dates submitted via a form. Returns FALSE (boolean) if
adminb0dd10f2006-08-25 17:25:49 +0000185the date string passed to it is not formatted as indicated above. Example:</p>
186
187<code>$now = time();<br />
188<br />
189$human = unix_to_human($now);<br />
190<br />
191$unix = human_to_unix($human);</code>
192
193
194
195
196
197<h2>timespan()</h2>
198
199<p>Formats a unix timestamp so that is appears similar to this:</p>
200
201<code>1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes</code>
202
203<p>The first parameter must contain a Unix timestamp. The second parameter must contain a
admine7e1dcd2006-10-21 18:04:01 +0000204timestamp that is greater that the first timestamp. If the second parameter empty, the current time will be used. The most common purpose
adminb0dd10f2006-08-25 17:25:49 +0000205for this function is to show how much time has elapsed from some point in time in the past to now. Example:</p>
206
207<code>$post_date = '1079621429';<br />
208$now = time();<br />
209<br />
210echo timespan($post_date, $now);</code>
211
212<p class="important"><strong>Note:</strong> The text generated by this function is found in the following language file: language/&lt;your_lang&gt;/date_lang.php</p>
213
214
215<h2>days_in_month()</h2>
216
217<p>Returns the number of days in a given month/year. Takes leap years into account. Example:</p>
218<code>echo days_in_month(06, 2005);</code>
219
220<p>If the second parameter is empty, the current year will be used.</p>
Derek Allardfc40afd2007-08-14 11:41:16 +0000221<h2>timezones()</h2>
222<p> Takes a timezone reference (for a list of valid timezones, see the &quot;Timezone Reference&quot; below) and returns the number of hours offset from UTC.</p>
223<p><code>echo timezones('UM5');</code></p>
224<p>This function is useful when used with timezone_menu(). </p>
adminb0dd10f2006-08-25 17:25:49 +0000225<h2>timezone_menu()</h2>
adminb0dd10f2006-08-25 17:25:49 +0000226<p>Generates a pull-down menu of timezones, like this one:</p>
227
228<form>
229<select name="timezones">
230<option value='UM12'>(UTC - 12:00) Enitwetok, Kwajalien</option>
231<option value='UM11'>(UTC - 11:00) Nome, Midway Island, Samoa</option>
232<option value='UM10'>(UTC - 10:00) Hawaii</option>
233<option value='UM9'>(UTC - 9:00) Alaska</option>
234<option value='UM8'>(UTC - 8:00) Pacific Time</option>
235<option value='UM7'>(UTC - 7:00) Mountain Time</option>
236<option value='UM6'>(UTC - 6:00) Central Time, Mexico City</option>
237<option value='UM5'>(UTC - 5:00) Eastern Time, Bogota, Lima, Quito</option>
238<option value='UM4'>(UTC - 4:00) Atlantic Time, Caracas, La Paz</option>
239<option value='UM25'>(UTC - 3:30) Newfoundland</option>
240<option value='UM3'>(UTC - 3:00) Brazil, Buenos Aires, Georgetown, Falkland Is.</option>
241<option value='UM2'>(UTC - 2:00) Mid-Atlantic, Ascention Is., St Helena</option>
242<option value='UM1'>(UTC - 1:00) Azores, Cape Verde Islands</option>
243<option value='UTC' selected='selected'>(UTC) Casablanca, Dublin, Edinburgh, London, Lisbon, Monrovia</option>
244<option value='UP1'>(UTC + 1:00) Berlin, Brussels, Copenhagen, Madrid, Paris, Rome</option>
245<option value='UP2'>(UTC + 2:00) Kaliningrad, South Africa, Warsaw</option>
246<option value='UP3'>(UTC + 3:00) Baghdad, Riyadh, Moscow, Nairobi</option>
247<option value='UP25'>(UTC + 3:30) Tehran</option>
248<option value='UP4'>(UTC + 4:00) Adu Dhabi, Baku, Muscat, Tbilisi</option>
249<option value='UP35'>(UTC + 4:30) Kabul</option>
250<option value='UP5'>(UTC + 5:00) Islamabad, Karachi, Tashkent</option>
251<option value='UP45'>(UTC + 5:30) Bombay, Calcutta, Madras, New Delhi</option>
252<option value='UP6'>(UTC + 6:00) Almaty, Colomba, Dhakra</option>
253<option value='UP7'>(UTC + 7:00) Bangkok, Hanoi, Jakarta</option>
254<option value='UP8'>(UTC + 8:00) Beijing, Hong Kong, Perth, Singapore, Taipei</option>
255<option value='UP9'>(UTC + 9:00) Osaka, Sapporo, Seoul, Tokyo, Yakutsk</option>
256<option value='UP85'>(UTC + 9:30) Adelaide, Darwin</option>
257<option value='UP10'>(UTC + 10:00) Melbourne, Papua New Guinea, Sydney, Vladivostok</option>
258<option value='UP11'>(UTC + 11:00) Magadan, New Caledonia, Solomon Islands</option>
259<option value='UP12'>(UTC + 12:00) Auckland, Wellington, Fiji, Marshall Island</option>
260</select>
261</form>
262
263<p>This menu is useful if you run a membership site in which your users are allowed to set their local timezone value.</p>
264
admine7e1dcd2006-10-21 18:04:01 +0000265<p>The first parameter lets you set the "selected" state of the menu. For example, to set Pacific time as the default you will do this:</p>
adminb0dd10f2006-08-25 17:25:49 +0000266
267<code>echo timezone_menu('UM8');</code>
268
269<p>Please see the timezone reference below to see the values of this menu.</p>
270
271<p>The second parameter lets you set a CSS class name for the menu.</p>
272
273<p class="important"><strong>Note:</strong> The text contained in the menu is found in the following language file: language/&lt;your_lang&gt;/date_lang.php</p>
274
275
276
277<h2>Timezone Reference</h2>
278
279<p>The following table indicates each timezone and its location.</p>
280
281<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
282<tr>
283<th>Time Zone</th>
284<th>Location</th>
285</tr><tr>
286
287<td class="td">UM12</td><td class="td">(UTC - 12:00) Enitwetok, Kwajalien</td>
288</tr><tr>
289<td class="td">UM11</td><td class="td">(UTC - 11:00) Nome, Midway Island, Samoa</td>
290</tr><tr>
291<td class="td">UM10</td><td class="td">(UTC - 10:00) Hawaii</td>
292</tr><tr>
293<td class="td">UM9</td><td class="td">(UTC - 9:00) Alaska</td>
294</tr><tr>
295<td class="td">UM8</td><td class="td">(UTC - 8:00) Pacific Time</td>
296</tr><tr>
297<td class="td">UM7</td><td class="td">(UTC - 7:00) Mountain Time</td>
298</tr><tr>
299<td class="td">UM6</td><td class="td">(UTC - 6:00) Central Time, Mexico City</td>
300</tr><tr>
301<td class="td">UM5</td><td class="td">(UTC - 5:00) Eastern Time, Bogota, Lima, Quito</td>
302</tr><tr>
303<td class="td">UM4</td><td class="td">(UTC - 4:00) Atlantic Time, Caracas, La Paz</td>
304</tr><tr>
305<td class="td">UM25</td><td class="td">(UTC - 3:30) Newfoundland</td>
306</tr><tr>
307<td class="td">UM3</td><td class="td">(UTC - 3:00) Brazil, Buenos Aires, Georgetown, Falkland Is.</td>
308</tr><tr>
309<td class="td">UM2</td><td class="td">(UTC - 2:00) Mid-Atlantic, Ascention Is., St Helena</td>
310</tr><tr>
311<td class="td">UM1</td><td class="td">(UTC - 1:00) Azores, Cape Verde Islands</td>
312</tr><tr>
Rick Ellis325197e2006-11-20 17:29:05 +0000313<td class="td">UTC</td><td class="td">(UTC) Casablanca, Dublin, Edinburgh, London, Lisbon, Monrovia</td>
adminb0dd10f2006-08-25 17:25:49 +0000314</tr><tr>
315<td class="td">UP1</td><td class="td">(UTC + 1:00) Berlin, Brussels, Copenhagen, Madrid, Paris, Rome</td>
316</tr><tr>
317<td class="td">UP2</td><td class="td">(UTC + 2:00) Kaliningrad, South Africa, Warsaw</td>
318</tr><tr>
319<td class="td">UP3</td><td class="td">(UTC + 3:00) Baghdad, Riyadh, Moscow, Nairobi</td>
320</tr><tr>
321<td class="td">UP25</td><td class="td">(UTC + 3:30) Tehran</td>
322</tr><tr>
323<td class="td">UP4</td><td class="td">(UTC + 4:00) Adu Dhabi, Baku, Muscat, Tbilisi</td>
324</tr><tr>
325<td class="td">UP35</td><td class="td">(UTC + 4:30) Kabul</td>
326</tr><tr>
327<td class="td">UP5</td><td class="td">(UTC + 5:00) Islamabad, Karachi, Tashkent</td>
328</tr><tr>
329<td class="td">UP45</td><td class="td">(UTC + 5:30) Bombay, Calcutta, Madras, New Delhi</td>
330</tr><tr>
331<td class="td">UP6</td><td class="td">(UTC + 6:00) Almaty, Colomba, Dhakra</td>
332</tr><tr>
333<td class="td">UP7</td><td class="td">(UTC + 7:00) Bangkok, Hanoi, Jakarta</td>
334</tr><tr>
335<td class="td">UP8</td><td class="td">(UTC + 8:00) Beijing, Hong Kong, Perth, Singapore, Taipei</td>
336</tr><tr>
337<td class="td">UP9</td><td class="td">(UTC + 9:00) Osaka, Sapporo, Seoul, Tokyo, Yakutsk</td>
338</tr><tr>
339<td class="td">UP85</td><td class="td">(UTC + 9:30) Adelaide, Darwin</td>
340</tr><tr>
341<td class="td">UP10</td><td class="td">(UTC + 10:00) Melbourne, Papua New Guinea, Sydney, Vladivostok</td>
342</tr><tr>
343<td class="td">UP11</td><td class="td">(UTC + 11:00) Magadan, New Caledonia, Solomon Islands</td>
344</tr><tr>
345<td class="td">UP12</td><td class="td">(UTC + 12:00) Auckland, Wellington, Fiji, Marshall Island</td>
346</tr>
347</table>
348
349
350</div>
351<!-- END CONTENT -->
352
353
354<div id="footer">
355<p>
356Previous Topic:&nbsp;&nbsp;<a href="cookie_helper.html">Cookie Helper</a>
357&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
358<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
359<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
360Next Topic:&nbsp;&nbsp;<a href="directory_helper.html">Directory Helper</a>
Derek Allardc6441282007-07-04 23:54:32 +0000361</p>
Derek Jones07870432008-02-13 03:49:26 +0000362<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000363</div>
364
365</body>
366</html>