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