Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * CodeIgniter Date Helpers |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Helpers |
| 45 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/helpers/date_helper.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | if ( ! function_exists('now')) |
| 53 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 54 | /** |
| 55 | * Get "now" time |
| 56 | * |
Iban Eguia | 7400965 | 2012-06-13 22:57:50 +0200 | [diff] [blame] | 57 | * Returns time() based on the timezone parameter or on the |
| 58 | * "time_reference" setting |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 59 | * |
Iban Eguia | 895e98c | 2012-06-08 23:01:31 +0200 | [diff] [blame] | 60 | * @param string |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 61 | * @return int |
| 62 | */ |
Iban Eguia | 8310595 | 2012-03-27 18:18:15 +0200 | [diff] [blame] | 63 | function now($timezone = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | { |
Iban Eguia | c88daba | 2012-06-11 13:58:30 +0200 | [diff] [blame] | 65 | if (empty($timezone)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | { |
Iban Eguia | feb14da | 2012-06-12 16:09:36 +0200 | [diff] [blame] | 67 | $timezone = config_item('time_reference'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | } |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 69 | |
Iban Eguia | c88daba | 2012-06-11 13:58:30 +0200 | [diff] [blame] | 70 | if ($timezone === 'local' OR $timezone === date_default_timezone_get()) |
Iban Eguia | e15e3dd | 2012-06-09 23:52:27 +0200 | [diff] [blame] | 71 | { |
Iban Eguia | c88daba | 2012-06-11 13:58:30 +0200 | [diff] [blame] | 72 | return time(); |
Iban Eguia | e15e3dd | 2012-06-09 23:52:27 +0200 | [diff] [blame] | 73 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | |
Iban Eguia | c88daba | 2012-06-11 13:58:30 +0200 | [diff] [blame] | 75 | $datetime = new DateTime('now', new DateTimeZone($timezone)); |
| 76 | sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second); |
| 77 | |
| 78 | return mktime($hour, $minute, $second, $month, $day, $year); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | // ------------------------------------------------------------------------ |
| 83 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | if ( ! function_exists('mdate')) |
| 85 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 86 | /** |
| 87 | * Convert MySQL Style Datecodes |
| 88 | * |
| 89 | * This function is identical to PHPs date() function, |
| 90 | * except that it allows date codes to be formatted using |
| 91 | * the MySQL style, where each code letter is preceded |
| 92 | * with a percent sign: %Y %m %d etc... |
| 93 | * |
| 94 | * The benefit of doing dates this way is that you don't |
| 95 | * have to worry about escaping your text letters that |
| 96 | * match the date codes. |
| 97 | * |
| 98 | * @param string |
| 99 | * @param int |
| 100 | * @return int |
| 101 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | function mdate($datestr = '', $time = '') |
| 103 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 104 | if ($datestr === '') |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 105 | { |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 106 | return ''; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 107 | } |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 108 | elseif (empty($time)) |
| 109 | { |
| 110 | $time = now(); |
| 111 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 112 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 113 | $datestr = str_replace( |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 114 | '%\\', |
| 115 | '', |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 116 | preg_replace('/([a-z]+?){1}/i', '\\\\\\1', $datestr) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 117 | ); |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 118 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | return date($datestr, $time); |
| 120 | } |
| 121 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 122 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | // ------------------------------------------------------------------------ |
| 124 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | if ( ! function_exists('standard_date')) |
| 126 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 127 | /** |
| 128 | * Standard Date |
| 129 | * |
| 130 | * Returns a date formatted according to the submitted standard. |
| 131 | * |
Andrey Andreev | ac57033 | 2012-07-04 13:04:10 +0300 | [diff] [blame] | 132 | * As of PHP 5.2, the DateTime extension provides constants that |
| 133 | * serve for the exact same purpose and are used with date(). |
Andrey Andreev | ac57033 | 2012-07-04 13:04:10 +0300 | [diff] [blame] | 134 | * |
Andrey Andreev | 29d909d | 2012-10-27 01:05:09 +0300 | [diff] [blame] | 135 | * @todo Remove in version 3.1+. |
| 136 | * @deprecated 3.0.0 Use PHP's native date() instead. |
| 137 | * @link http://www.php.net/manual/en/class.datetime.php#datetime.constants.types |
Andrey Andreev | ac57033 | 2012-07-04 13:04:10 +0300 | [diff] [blame] | 138 | * |
Andrey Andreev | 29d909d | 2012-10-27 01:05:09 +0300 | [diff] [blame] | 139 | * @example date(DATE_RFC822, now()); // default |
| 140 | * @example date(DATE_W3C, $time); // a different format and time |
Andrey Andreev | ac57033 | 2012-07-04 13:04:10 +0300 | [diff] [blame] | 141 | * |
Andrey Andreev | 29d909d | 2012-10-27 01:05:09 +0300 | [diff] [blame] | 142 | * @param string $fmt = 'DATE_RFC822' the chosen format |
| 143 | * @param int $time = NULL Unix timestamp |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 144 | * @return string |
| 145 | */ |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 146 | function standard_date($fmt = 'DATE_RFC822', $time = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | { |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 148 | if (empty($time)) |
| 149 | { |
| 150 | $time = now(); |
| 151 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 153 | // Procedural style pre-defined constants from the DateTime extension |
| 154 | if (strpos($fmt, 'DATE_') !== 0 OR defined($fmt) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | { |
| 156 | return FALSE; |
| 157 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 159 | return date(constant($fmt), $time); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | // ------------------------------------------------------------------------ |
| 164 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | if ( ! function_exists('timespan')) |
| 166 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 167 | /** |
| 168 | * Timespan |
| 169 | * |
| 170 | * Returns a span of seconds in this format: |
| 171 | * 10 days 14 hours 36 minutes 47 seconds |
| 172 | * |
| 173 | * @param int a number of seconds |
| 174 | * @param int Unix timestamp |
| 175 | * @param int a number of display units |
| 176 | * @return string |
| 177 | */ |
Roger Herbert | 8d69aa1 | 2012-03-14 08:44:55 +0000 | [diff] [blame] | 178 | function timespan($seconds = 1, $time = '', $units = 7) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | { |
| 180 | $CI =& get_instance(); |
| 181 | $CI->lang->load('date'); |
| 182 | |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 183 | is_numeric($seconds) OR $seconds = 1; |
| 184 | is_numeric($time) OR $time = time(); |
| 185 | is_numeric($units) OR $units = 7; |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 186 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 187 | $seconds = ($time <= $seconds) ? 1 : $time - $seconds; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 188 | |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 189 | $str = array(); |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 190 | $years = floor($seconds / 31557600); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | if ($years > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 194 | $str[] = $years.' '.$CI->lang->line($years > 1 ? 'date_years' : 'date_year'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 197 | $seconds -= $years * 31557600; |
| 198 | $months = floor($seconds / 2629743); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 200 | if (count($str) < $units && ($years > 0 OR $months > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | { |
| 202 | if ($months > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 203 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 204 | $str[] = $months.' '.$CI->lang->line($months > 1 ? 'date_months' : 'date_month'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 207 | $seconds -= $months * 2629743; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | $weeks = floor($seconds / 604800); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 212 | if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | { |
| 214 | if ($weeks > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 215 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 216 | $str[] = $weeks.' '.$CI->lang->line($weeks > 1 ? 'date_weeks' : 'date_week'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 218 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | $seconds -= $weeks * 604800; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | |
| 222 | $days = floor($seconds / 86400); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 224 | if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | { |
| 226 | if ($days > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 227 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 228 | $str[] = $days.' '.$CI->lang->line($days > 1 ? 'date_days' : 'date_day'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 230 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | $seconds -= $days * 86400; |
| 232 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 233 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | $hours = floor($seconds / 3600); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 235 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 236 | if (count($str) < $units && ($days > 0 OR $hours > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | { |
| 238 | if ($hours > 0) |
| 239 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 240 | $str[] = $hours.' '.$CI->lang->line($hours > 1 ? 'date_hours' : 'date_hour'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 242 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | $seconds -= $hours * 3600; |
| 244 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | $minutes = floor($seconds / 60); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 248 | if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | { |
| 250 | if ($minutes > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 251 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 252 | $str[] = $minutes.' '.$CI->lang->line($minutes > 1 ? 'date_minutes' : 'date_minute'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 254 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | $seconds -= $minutes * 60; |
| 256 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 257 | |
Roger Herbert | 597eb21 | 2012-03-14 09:06:17 +0000 | [diff] [blame] | 258 | if (count($str) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 260 | $str[] = $seconds.' '.$CI->lang->line($seconds > 1 ? 'date_seconds' : 'date_second'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 262 | |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 263 | return implode(', ', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 266 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | // ------------------------------------------------------------------------ |
| 268 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | if ( ! function_exists('days_in_month')) |
| 270 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 271 | /** |
| 272 | * Number of days in a month |
| 273 | * |
| 274 | * Takes a month/year as input and returns the number of days |
| 275 | * for the given month/year. Takes leap years into consideration. |
| 276 | * |
| 277 | * @param int a numeric month |
| 278 | * @param int a numeric year |
| 279 | * @return int |
| 280 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | function days_in_month($month = 0, $year = '') |
| 282 | { |
| 283 | if ($month < 1 OR $month > 12) |
| 284 | { |
| 285 | return 0; |
| 286 | } |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 287 | elseif ( ! is_numeric($year) OR strlen($year) !== 4) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | { |
| 289 | $year = date('Y'); |
| 290 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 291 | |
Andrey Andreev | be368a8 | 2014-02-20 15:46:05 +0200 | [diff] [blame] | 292 | if (defined('CAL_GREGORIAN')) |
| 293 | { |
| 294 | return cal_days_in_month(CAL_GREGORIAN, $month, $year); |
| 295 | } |
| 296 | |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 297 | if ($year >= 1970) |
| 298 | { |
| 299 | return (int) date('t', mktime(12, 0, 0, $month, 1, $year)); |
| 300 | } |
| 301 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | if ($month == 2) |
| 303 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 304 | if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | { |
| 306 | return 29; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
| 311 | return $days_in_month[$month - 1]; |
| 312 | } |
| 313 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 314 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | // ------------------------------------------------------------------------ |
| 316 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | if ( ! function_exists('local_to_gmt')) |
| 318 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 319 | /** |
| 320 | * Converts a local Unix timestamp to GMT |
| 321 | * |
| 322 | * @param int Unix timestamp |
| 323 | * @return int |
| 324 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | function local_to_gmt($time = '') |
| 326 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 327 | if ($time === '') |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 328 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | $time = time(); |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 330 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 331 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 332 | return mktime( |
Andrey Andreev | b089e15 | 2012-06-16 19:11:40 +0300 | [diff] [blame] | 333 | gmdate('G', $time), |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 334 | gmdate('i', $time), |
| 335 | gmdate('s', $time), |
Andrey Andreev | b089e15 | 2012-06-16 19:11:40 +0300 | [diff] [blame] | 336 | gmdate('n', $time), |
| 337 | gmdate('j', $time), |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 338 | gmdate('Y', $time) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 339 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 342 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | // ------------------------------------------------------------------------ |
| 344 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | if ( ! function_exists('gmt_to_local')) |
| 346 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 347 | /** |
| 348 | * Converts GMT time to a localized value |
| 349 | * |
| 350 | * Takes a Unix timestamp (in GMT) as input, and returns |
| 351 | * at the local value based on the timezone and DST setting |
| 352 | * submitted |
| 353 | * |
| 354 | * @param int Unix timestamp |
| 355 | * @param string timezone |
| 356 | * @param bool whether DST is active |
| 357 | * @return int |
| 358 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 360 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 361 | if ($time === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | { |
| 363 | return now(); |
| 364 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 365 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | $time += timezones($timezone) * 3600; |
| 367 | |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 368 | return ($dst === TRUE) ? $time + 3600 : $time; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 371 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | // ------------------------------------------------------------------------ |
| 373 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | if ( ! function_exists('mysql_to_unix')) |
| 375 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 376 | /** |
| 377 | * Converts a MySQL Timestamp to Unix |
| 378 | * |
webmasterar | 69779af | 2012-11-17 01:06:41 +0000 | [diff] [blame] | 379 | * @param int MySQL timestamp YYYY-MM-DD HH:MM:SS |
| 380 | * @return int Unix timstamp |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 381 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | function mysql_to_unix($time = '') |
| 383 | { |
| 384 | // We'll remove certain characters for backward compatibility |
| 385 | // since the formatting changed with MySQL 4.1 |
| 386 | // YYYY-MM-DD HH:MM:SS |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 387 | |
Andrey Andreev | 3bbbd26 | 2012-06-14 13:35:32 +0300 | [diff] [blame] | 388 | $time = str_replace(array('-', ':', ' '), '', $time); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 389 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | // YYYYMMDDHHMMSS |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 391 | return mktime( |
| 392 | substr($time, 8, 2), |
| 393 | substr($time, 10, 2), |
| 394 | substr($time, 12, 2), |
| 395 | substr($time, 4, 2), |
| 396 | substr($time, 6, 2), |
| 397 | substr($time, 0, 4) |
| 398 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 401 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | // ------------------------------------------------------------------------ |
| 403 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | if ( ! function_exists('unix_to_human')) |
| 405 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 406 | /** |
| 407 | * Unix to "Human" |
| 408 | * |
| 409 | * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM |
| 410 | * |
| 411 | * @param int Unix timestamp |
| 412 | * @param bool whether to show seconds |
| 413 | * @param string format: us or euro |
| 414 | * @return string |
| 415 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us') |
| 417 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 418 | $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 419 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 420 | if ($fmt === 'us') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | { |
| 422 | $r .= date('h', $time).':'.date('i', $time); |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | $r .= date('H', $time).':'.date('i', $time); |
| 427 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | if ($seconds) |
| 430 | { |
| 431 | $r .= ':'.date('s', $time); |
| 432 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 433 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 434 | if ($fmt === 'us') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 436 | return $r.' '.date('A', $time); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 438 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | return $r; |
| 440 | } |
| 441 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 442 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | // ------------------------------------------------------------------------ |
| 444 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | if ( ! function_exists('human_to_unix')) |
| 446 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 447 | /** |
| 448 | * Convert "human" date to GMT |
| 449 | * |
| 450 | * Reverses the above process |
| 451 | * |
| 452 | * @param string format: us or euro |
| 453 | * @return int |
| 454 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | function human_to_unix($datestr = '') |
| 456 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 457 | if ($datestr === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | { |
| 459 | return FALSE; |
| 460 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 461 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 462 | $datestr = preg_replace('/\040+/', ' ', trim($datestr)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 463 | |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 464 | if ( ! preg_match('/^(\d{2}|\d{4})\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | { |
| 466 | return FALSE; |
| 467 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 468 | |
Andrey Andreev | e24eed7 | 2012-11-02 23:33:45 +0200 | [diff] [blame] | 469 | sscanf($datestr, '%d-%d-%d %s %s', $year, $month, $day, $time, $ampm); |
| 470 | sscanf($time, '%d:%d:%d', $hour, $min, $sec); |
| 471 | isset($sec) OR $sec = 0; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | |
Andrey Andreev | e24eed7 | 2012-11-02 23:33:45 +0200 | [diff] [blame] | 473 | if (isset($ampm)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | { |
Andrey Andreev | e24eed7 | 2012-11-02 23:33:45 +0200 | [diff] [blame] | 475 | $ampm = strtolower($ampm); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 476 | |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 477 | if ($ampm[0] === 'p' && $hour < 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 478 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 479 | $hour += 12; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 480 | } |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 481 | elseif ($ampm[0] === 'a' && $hour === 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 482 | { |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 483 | $hour = 0; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 484 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 486 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | return mktime($hour, $min, $sec, $month, $day, $year); |
| 488 | } |
| 489 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 490 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | // ------------------------------------------------------------------------ |
| 492 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 493 | if ( ! function_exists('nice_date')) |
| 494 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 495 | /** |
| 496 | * Turns many "reasonably-date-like" strings into something |
| 497 | * that is actually useful. This only works for dates after unix epoch. |
| 498 | * |
Andrey Andreev | 610be9d | 2016-11-23 13:40:16 +0200 | [diff] [blame] | 499 | * @deprecated 3.1.3 Use DateTime::createFromFormat($input_format, $input)->format($output_format); |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 500 | * @param string The terribly formatted date-like string |
| 501 | * @param string Date format to return (same as php date function) |
| 502 | * @return string |
| 503 | */ |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 504 | function nice_date($bad_date = '', $format = FALSE) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 505 | { |
| 506 | if (empty($bad_date)) |
| 507 | { |
| 508 | return 'Unknown'; |
| 509 | } |
Andrey Andreev | d9b44be | 2012-06-15 16:07:08 +0300 | [diff] [blame] | 510 | elseif (empty($format)) |
| 511 | { |
| 512 | $format = 'U'; |
| 513 | } |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 514 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 515 | // Date like: YYYYMM |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 516 | if (preg_match('/^\d{6}$/i', $bad_date)) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 517 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 518 | if (in_array(substr($bad_date, 0, 2), array('19', '20'))) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 519 | { |
| 520 | $year = substr($bad_date, 0, 4); |
| 521 | $month = substr($bad_date, 4, 2); |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 522 | } |
| 523 | else |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 524 | { |
| 525 | $month = substr($bad_date, 0, 2); |
| 526 | $year = substr($bad_date, 2, 4); |
| 527 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 528 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 529 | return date($format, strtotime($year.'-'.$month.'-01')); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 530 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 531 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 532 | // Date Like: YYYYMMDD |
Andrey Andreev | 820d9cd | 2016-11-23 13:27:42 +0200 | [diff] [blame] | 533 | if (preg_match('/^\d{8}$/i', $bad_date, $matches)) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 534 | { |
Andrey Andreev | 820d9cd | 2016-11-23 13:27:42 +0200 | [diff] [blame] | 535 | return DateTime::createFromFormat('Ymd', $bad_date)->format($format); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 536 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 537 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 538 | // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between) |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 539 | if (preg_match('/^(\d{1,2})-(\d{1,2})-(\d{4})$/i', $bad_date, $matches)) |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 540 | { |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 541 | return date($format, strtotime($matches[3].'-'.$matches[1].'-'.$matches[2])); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 542 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 543 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 544 | // Any other kind of string, when converted into UNIX time, |
| 545 | // produces "0 seconds after epoc..." is probably bad... |
| 546 | // return "Invalid Date". |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 547 | if (date('U', strtotime($bad_date)) === '0') |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 548 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 549 | return 'Invalid Date'; |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 550 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 551 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 552 | // It's probably a valid-ish date format already |
| 553 | return date($format, strtotime($bad_date)); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | // ------------------------------------------------------------------------ |
| 558 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 559 | if ( ! function_exists('timezone_menu')) |
| 560 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 561 | /** |
| 562 | * Timezone Menu |
| 563 | * |
| 564 | * Generates a drop-down menu of timezones. |
| 565 | * |
| 566 | * @param string timezone |
| 567 | * @param string classname |
| 568 | * @param string menu name |
Mat Whitney | 7540ded | 2012-06-22 12:02:10 -0700 | [diff] [blame] | 569 | * @param mixed attributes |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 570 | * @return string |
| 571 | */ |
Mat Whitney | 7540ded | 2012-06-22 12:02:10 -0700 | [diff] [blame] | 572 | function timezone_menu($default = 'UTC', $class = '', $name = 'timezones', $attributes = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 573 | { |
| 574 | $CI =& get_instance(); |
| 575 | $CI->lang->load('date'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 576 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 577 | $default = ($default === 'GMT') ? 'UTC' : $default; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | |
| 579 | $menu = '<select name="'.$name.'"'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 580 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 581 | if ($class !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | { |
| 583 | $menu .= ' class="'.$class.'"'; |
| 584 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 585 | |
Eric Barnes | acedd2b | 2012-07-29 00:15:40 -0400 | [diff] [blame] | 586 | $menu .= _stringify_attributes($attributes).">\n"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 587 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | foreach (timezones() as $key => $val) |
| 589 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 590 | $selected = ($default === $key) ? ' selected="selected"' : ''; |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 591 | $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 594 | return $menu.'</select>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 597 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 598 | // ------------------------------------------------------------------------ |
| 599 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 600 | if ( ! function_exists('timezones')) |
| 601 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 602 | /** |
| 603 | * Timezones |
| 604 | * |
| 605 | * Returns an array of timezones. This is a helper function |
| 606 | * for various other ones in this library |
| 607 | * |
| 608 | * @param string timezone |
| 609 | * @return string |
| 610 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 611 | function timezones($tz = '') |
| 612 | { |
| 613 | // Note: Don't change the order of these even though |
| 614 | // some items appear to be in the wrong order |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 615 | |
| 616 | $zones = array( |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 617 | 'UM12' => -12, |
| 618 | 'UM11' => -11, |
| 619 | 'UM10' => -10, |
| 620 | 'UM95' => -9.5, |
| 621 | 'UM9' => -9, |
| 622 | 'UM8' => -8, |
| 623 | 'UM7' => -7, |
| 624 | 'UM6' => -6, |
| 625 | 'UM5' => -5, |
| 626 | 'UM45' => -4.5, |
| 627 | 'UM4' => -4, |
| 628 | 'UM35' => -3.5, |
| 629 | 'UM3' => -3, |
| 630 | 'UM2' => -2, |
| 631 | 'UM1' => -1, |
| 632 | 'UTC' => 0, |
| 633 | 'UP1' => +1, |
| 634 | 'UP2' => +2, |
| 635 | 'UP3' => +3, |
| 636 | 'UP35' => +3.5, |
| 637 | 'UP4' => +4, |
| 638 | 'UP45' => +4.5, |
| 639 | 'UP5' => +5, |
| 640 | 'UP55' => +5.5, |
| 641 | 'UP575' => +5.75, |
| 642 | 'UP6' => +6, |
| 643 | 'UP65' => +6.5, |
| 644 | 'UP7' => +7, |
| 645 | 'UP8' => +8, |
| 646 | 'UP875' => +8.75, |
| 647 | 'UP9' => +9, |
| 648 | 'UP95' => +9.5, |
| 649 | 'UP10' => +10, |
| 650 | 'UP105' => +10.5, |
| 651 | 'UP11' => +11, |
| 652 | 'UP115' => +11.5, |
| 653 | 'UP12' => +12, |
| 654 | 'UP1275' => +12.75, |
| 655 | 'UP13' => +13, |
| 656 | 'UP14' => +14 |
| 657 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 658 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 659 | if ($tz === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 660 | { |
| 661 | return $zones; |
| 662 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 663 | |
Andrey Andreev | e92df33 | 2012-03-26 22:44:20 +0300 | [diff] [blame] | 664 | return isset($zones[$tz]) ? $zones[$tz] : 0; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 668 | // ------------------------------------------------------------------------ |
| 669 | |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 670 | if ( ! function_exists('date_range')) |
| 671 | { |
Andrey Andreev | 14aa317 | 2012-05-02 13:27:30 +0300 | [diff] [blame] | 672 | /** |
| 673 | * Date range |
| 674 | * |
| 675 | * Returns a list of dates within a specified period. |
| 676 | * |
| 677 | * @param int unix_start UNIX timestamp of period start date |
| 678 | * @param int unix_end|days UNIX timestamp of period end date |
| 679 | * or interval in days. |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 680 | * @param mixed is_unix Specifies whether the second parameter |
Andrey Andreev | 14aa317 | 2012-05-02 13:27:30 +0300 | [diff] [blame] | 681 | * is a UNIX timestamp or a day interval |
| 682 | * - TRUE or 'unix' for a timestamp |
| 683 | * - FALSE or 'days' for an interval |
| 684 | * @param string date_format Output date format, same as in date() |
| 685 | * @return array |
| 686 | */ |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 687 | function date_range($unix_start = '', $mixed = '', $is_unix = TRUE, $format = 'Y-m-d') |
| 688 | { |
| 689 | if ($unix_start == '' OR $mixed == '' OR $format == '') |
| 690 | { |
| 691 | return FALSE; |
| 692 | } |
| 693 | |
| 694 | $is_unix = ! ( ! $is_unix OR $is_unix === 'days'); |
| 695 | |
| 696 | // Validate input and try strtotime() on invalid timestamps/intervals, just in case |
Rasmus Lerdorf | 4ad89d8 | 2013-05-18 10:18:17 -0400 | [diff] [blame] | 697 | if ( ( ! ctype_digit((string) $unix_start) && ($unix_start = @strtotime($unix_start)) === FALSE) |
Andrey Andreev | 7a7ad78 | 2012-11-12 17:21:01 +0200 | [diff] [blame] | 698 | OR ( ! ctype_digit((string) $mixed) && ($is_unix === FALSE OR ($mixed = @strtotime($mixed)) === FALSE)) |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 699 | OR ($is_unix === TRUE && $mixed < $unix_start)) |
| 700 | { |
| 701 | return FALSE; |
| 702 | } |
| 703 | |
| 704 | if ($is_unix && ($unix_start == $mixed OR date($format, $unix_start) === date($format, $mixed))) |
| 705 | { |
Rasmus Lerdorf | 4ad89d8 | 2013-05-18 10:18:17 -0400 | [diff] [blame] | 706 | return array(date($format, $unix_start)); |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | $range = array(); |
| 710 | |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 711 | $from = new DateTime(); |
Andrey Andreev | a838279 | 2016-07-28 16:40:12 +0300 | [diff] [blame] | 712 | $from->setTimestamp($unix_start); |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 713 | |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 714 | if ($is_unix) |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 715 | { |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 716 | $arg = new DateTime(); |
Andrey Andreev | a838279 | 2016-07-28 16:40:12 +0300 | [diff] [blame] | 717 | $arg->setTimestamp($mixed); |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 718 | } |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 719 | else |
| 720 | { |
| 721 | $arg = (int) $mixed; |
| 722 | } |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 723 | |
Andrey Andreev | a838279 | 2016-07-28 16:40:12 +0300 | [diff] [blame] | 724 | $period = new DatePeriod($from, new DateInterval('P1D'), $arg); |
| 725 | foreach ($period as $date) |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 726 | { |
Andrey Andreev | a838279 | 2016-07-28 16:40:12 +0300 | [diff] [blame] | 727 | $range[] = $date->format($format); |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 728 | } |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 729 | |
Andrey Andreev | a838279 | 2016-07-28 16:40:12 +0300 | [diff] [blame] | 730 | /* If a period end date was passed to the DatePeriod constructor, it might not |
| 731 | * be in our results. Not sure if this is a bug or it's just possible because |
| 732 | * the end date might actually be less than 24 hours away from the previously |
| 733 | * generated DateTime object, but either way - we have to append it manually. |
| 734 | */ |
| 735 | if ( ! is_int($arg) && $range[count($range) - 1] !== $arg->format($format)) |
| 736 | { |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 737 | $range[] = $arg->format($format); |
| 738 | } |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 739 | |
| 740 | return $range; |
| 741 | } |
| 742 | } |