blob: eca1fc021ef66b1b0c48ddb10dd03648aa9d06b6 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Eric Barnesdc3e4be2011-12-19 13:48:29 -05008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Eric Barnesdc3e4be2011-12-19 13:48:29 -050010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter Date Helpers
42 *
43 * @package CodeIgniter
44 * @subpackage Helpers
45 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/helpers/date_helper.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49
50// ------------------------------------------------------------------------
51
Derek Allard2067d1a2008-11-13 22:59:24 +000052if ( ! function_exists('now'))
53{
Timothy Warren01b129a2012-04-27 11:36:50 -040054 /**
55 * Get "now" time
56 *
Iban Eguia74009652012-06-13 22:57:50 +020057 * Returns time() based on the timezone parameter or on the
58 * "time_reference" setting
Timothy Warren01b129a2012-04-27 11:36:50 -040059 *
Iban Eguia895e98c2012-06-08 23:01:31 +020060 * @param string
Timothy Warren01b129a2012-04-27 11:36:50 -040061 * @return int
62 */
Iban Eguia83105952012-03-27 18:18:15 +020063 function now($timezone = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000064 {
Iban Eguiac88daba2012-06-11 13:58:30 +020065 if (empty($timezone))
Derek Allard2067d1a2008-11-13 22:59:24 +000066 {
Iban Eguiafeb14da2012-06-12 16:09:36 +020067 $timezone = config_item('time_reference');
Derek Allard2067d1a2008-11-13 22:59:24 +000068 }
Greg Akerc964e722011-08-29 19:31:29 -050069
Iban Eguiac88daba2012-06-11 13:58:30 +020070 if ($timezone === 'local' OR $timezone === date_default_timezone_get())
Iban Eguiae15e3dd2012-06-09 23:52:27 +020071 {
Iban Eguiac88daba2012-06-11 13:58:30 +020072 return time();
Iban Eguiae15e3dd2012-06-09 23:52:27 +020073 }
Derek Allard2067d1a2008-11-13 22:59:24 +000074
Iban Eguiac88daba2012-06-11 13:58:30 +020075 $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 Allard2067d1a2008-11-13 22:59:24 +000079 }
80}
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082// ------------------------------------------------------------------------
83
Derek Allard2067d1a2008-11-13 22:59:24 +000084if ( ! function_exists('mdate'))
85{
Timothy Warren01b129a2012-04-27 11:36:50 -040086 /**
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 Allard2067d1a2008-11-13 22:59:24 +0000102 function mdate($datestr = '', $time = '')
103 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100104 if ($datestr === '')
Greg Akerf9168392011-08-29 19:29:05 -0500105 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500106 return '';
Greg Akerf9168392011-08-29 19:29:05 -0500107 }
Andrey Andreeveef24062012-06-14 16:17:48 +0300108 elseif (empty($time))
109 {
110 $time = now();
111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Greg Akerf9168392011-08-29 19:29:05 -0500113 $datestr = str_replace(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500114 '%\\',
115 '',
Andrey Andreevae31eb52012-05-17 14:54:15 +0300116 preg_replace('/([a-z]+?){1}/i', '\\\\\\1', $datestr)
Greg Akerf9168392011-08-29 19:29:05 -0500117 );
Greg Akerc964e722011-08-29 19:31:29 -0500118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 return date($datestr, $time);
120 }
121}
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123// ------------------------------------------------------------------------
124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125if ( ! function_exists('standard_date'))
126{
Timothy Warren01b129a2012-04-27 11:36:50 -0400127 /**
128 * Standard Date
129 *
130 * Returns a date formatted according to the submitted standard.
131 *
Andrey Andreevac570332012-07-04 13:04:10 +0300132 * As of PHP 5.2, the DateTime extension provides constants that
133 * serve for the exact same purpose and are used with date().
Andrey Andreevac570332012-07-04 13:04:10 +0300134 *
Andrey Andreev29d909d2012-10-27 01:05:09 +0300135 * @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 Andreevac570332012-07-04 13:04:10 +0300138 *
Andrey Andreev29d909d2012-10-27 01:05:09 +0300139 * @example date(DATE_RFC822, now()); // default
140 * @example date(DATE_W3C, $time); // a different format and time
Andrey Andreevac570332012-07-04 13:04:10 +0300141 *
Andrey Andreev29d909d2012-10-27 01:05:09 +0300142 * @param string $fmt = 'DATE_RFC822' the chosen format
143 * @param int $time = NULL Unix timestamp
Timothy Warren01b129a2012-04-27 11:36:50 -0400144 * @return string
145 */
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300146 function standard_date($fmt = 'DATE_RFC822', $time = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300148 if (empty($time))
149 {
150 $time = now();
151 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000152
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300153 // Procedural style pre-defined constants from the DateTime extension
154 if (strpos($fmt, 'DATE_') !== 0 OR defined($fmt) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
156 return FALSE;
157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300159 return date(constant($fmt), $time);
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
161}
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163// ------------------------------------------------------------------------
164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165if ( ! function_exists('timespan'))
166{
Timothy Warren01b129a2012-04-27 11:36:50 -0400167 /**
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 Herbert8d69aa12012-03-14 08:44:55 +0000178 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
180 $CI =& get_instance();
181 $CI->lang->load('date');
182
Andrey Andreeveef24062012-06-14 16:17:48 +0300183 is_numeric($seconds) OR $seconds = 1;
184 is_numeric($time) OR $time = time();
185 is_numeric($units) OR $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000186
Greg Akerf9168392011-08-29 19:29:05 -0500187 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200188
Roger Herbert04c146d2012-03-11 15:31:01 +0000189 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500190 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200193 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300194 $str[] = $years.' '.$CI->lang->line($years > 1 ? 'date_years' : 'date_year');
Barry Mienydd671972010-10-04 16:33:58 +0200195 }
196
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500197 $seconds -= $years * 31557600;
198 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200199
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000200 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
202 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200203 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300204 $str[] = $months.' '.$CI->lang->line($months > 1 ? 'date_months' : 'date_month');
Barry Mienydd671972010-10-04 16:33:58 +0200205 }
206
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500207 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
209
210 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200211
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000212 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200215 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300216 $str[] = $weeks.' '.$CI->lang->line($weeks > 1 ? 'date_weeks' : 'date_week');
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200220 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000221
222 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200223
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000224 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
226 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200227 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300228 $str[] = $days.' '.$CI->lang->line($days > 1 ? 'date_days' : 'date_day');
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 $seconds -= $days * 86400;
232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200235
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000236 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
238 if ($hours > 0)
239 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300240 $str[] = $hours.' '.$CI->lang->line($hours > 1 ? 'date_hours' : 'date_hour');
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 $seconds -= $hours * 3600;
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200247
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000248 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
250 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200251 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300252 $str[] = $minutes.' '.$CI->lang->line($minutes > 1 ? 'date_minutes' : 'date_minute');
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 $seconds -= $minutes * 60;
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Roger Herbert597eb212012-03-14 09:06:17 +0000258 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300260 $str[] = $seconds.' '.$CI->lang->line($seconds > 1 ? 'date_seconds' : 'date_second');
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Roger Herbert04c146d2012-03-11 15:31:01 +0000263 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
265}
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267// ------------------------------------------------------------------------
268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269if ( ! function_exists('days_in_month'))
270{
Timothy Warren01b129a2012-04-27 11:36:50 -0400271 /**
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 Allard2067d1a2008-11-13 22:59:24 +0000281 function days_in_month($month = 0, $year = '')
282 {
283 if ($month < 1 OR $month > 12)
284 {
285 return 0;
286 }
Andrey Andreeveef24062012-06-14 16:17:48 +0300287 elseif ( ! is_numeric($year) OR strlen($year) !== 4)
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 $year = date('Y');
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Andrey Andreevbe368a82014-02-20 15:46:05 +0200292 if (defined('CAL_GREGORIAN'))
293 {
294 return cal_days_in_month(CAL_GREGORIAN, $month, $year);
295 }
296
Andrey Andreeveef24062012-06-14 16:17:48 +0300297 if ($year >= 1970)
298 {
299 return (int) date('t', mktime(12, 0, 0, $month, 1, $year));
300 }
301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 if ($month == 2)
303 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100304 if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
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 Jones36591092010-03-05 10:05:51 -0600314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315// ------------------------------------------------------------------------
316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317if ( ! function_exists('local_to_gmt'))
318{
Timothy Warren01b129a2012-04-27 11:36:50 -0400319 /**
320 * Converts a local Unix timestamp to GMT
321 *
322 * @param int Unix timestamp
323 * @return int
324 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 function local_to_gmt($time = '')
326 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100327 if ($time === '')
Greg Akerf9168392011-08-29 19:29:05 -0500328 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500330 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500331
Greg Akerf9168392011-08-29 19:29:05 -0500332 return mktime(
Andrey Andreevb089e152012-06-16 19:11:40 +0300333 gmdate('G', $time),
Andrey Andreevae31eb52012-05-17 14:54:15 +0300334 gmdate('i', $time),
335 gmdate('s', $time),
Andrey Andreevb089e152012-06-16 19:11:40 +0300336 gmdate('n', $time),
337 gmdate('j', $time),
Andrey Andreevae31eb52012-05-17 14:54:15 +0300338 gmdate('Y', $time)
Greg Akerf9168392011-08-29 19:29:05 -0500339 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
341}
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343// ------------------------------------------------------------------------
344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345if ( ! function_exists('gmt_to_local'))
346{
Timothy Warren01b129a2012-04-27 11:36:50 -0400347 /**
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 Allard2067d1a2008-11-13 22:59:24 +0000359 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200360 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100361 if ($time === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
363 return now();
364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 $time += timezones($timezone) * 3600;
367
Andrey Andreeveef24062012-06-14 16:17:48 +0300368 return ($dst === TRUE) ? $time + 3600 : $time;
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
370}
Derek Jones36591092010-03-05 10:05:51 -0600371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372// ------------------------------------------------------------------------
373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374if ( ! function_exists('mysql_to_unix'))
375{
Timothy Warren01b129a2012-04-27 11:36:50 -0400376 /**
377 * Converts a MySQL Timestamp to Unix
378 *
webmasterar69779af2012-11-17 01:06:41 +0000379 * @param int MySQL timestamp YYYY-MM-DD HH:MM:SS
380 * @return int Unix timstamp
Timothy Warren01b129a2012-04-27 11:36:50 -0400381 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 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 Mienydd671972010-10-04 16:33:58 +0200387
Andrey Andreev3bbbd262012-06-14 13:35:32 +0300388 $time = str_replace(array('-', ':', ' '), '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500391 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 Allard2067d1a2008-11-13 22:59:24 +0000399 }
400}
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402// ------------------------------------------------------------------------
403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404if ( ! function_exists('unix_to_human'))
405{
Timothy Warren01b129a2012-04-27 11:36:50 -0400406 /**
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 Allard2067d1a2008-11-13 22:59:24 +0000416 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
417 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300418 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200419
Alex Bilbie773ccc32012-06-02 11:11:08 +0100420 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 $r .= date('h', $time).':'.date('i', $time);
423 }
424 else
425 {
426 $r .= date('H', $time).':'.date('i', $time);
427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 if ($seconds)
430 {
431 $r .= ':'.date('s', $time);
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Alex Bilbie773ccc32012-06-02 11:11:08 +0100434 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300436 return $r.' '.date('A', $time);
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 return $r;
440 }
441}
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443// ------------------------------------------------------------------------
444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445if ( ! function_exists('human_to_unix'))
446{
Timothy Warren01b129a2012-04-27 11:36:50 -0400447 /**
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 Allard2067d1a2008-11-13 22:59:24 +0000455 function human_to_unix($datestr = '')
456 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100457 if ($datestr === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
459 return FALSE;
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Andrey Andreevae31eb52012-05-17 14:54:15 +0300462 $datestr = preg_replace('/\040+/', ' ', trim($datestr));
Barry Mienydd671972010-10-04 16:33:58 +0200463
Andrey Andreevf11a1932012-06-14 16:35:09 +0300464 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 Allard2067d1a2008-11-13 22:59:24 +0000465 {
466 return FALSE;
467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Andrey Andreeve24eed72012-11-02 23:33:45 +0200469 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 Allard2067d1a2008-11-13 22:59:24 +0000472
Andrey Andreeve24eed72012-11-02 23:33:45 +0200473 if (isset($ampm))
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Andrey Andreeve24eed72012-11-02 23:33:45 +0200475 $ampm = strtolower($ampm);
Barry Mienydd671972010-10-04 16:33:58 +0200476
Andrey Andreevf11a1932012-06-14 16:35:09 +0300477 if ($ampm[0] === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500478 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300479 $hour += 12;
Greg Akerf9168392011-08-29 19:29:05 -0500480 }
Andrey Andreevf11a1932012-06-14 16:35:09 +0300481 elseif ($ampm[0] === 'a' && $hour === 12)
Greg Akerf9168392011-08-29 19:29:05 -0500482 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300483 $hour = 0;
Greg Akerf9168392011-08-29 19:29:05 -0500484 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 return mktime($hour, $min, $sec, $month, $day, $year);
488 }
489}
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491// ------------------------------------------------------------------------
492
Kyle Farris896d95a2011-08-21 23:03:54 -0300493if ( ! function_exists('nice_date'))
494{
Timothy Warren01b129a2012-04-27 11:36:50 -0400495 /**
496 * Turns many "reasonably-date-like" strings into something
497 * that is actually useful. This only works for dates after unix epoch.
498 *
Andrey Andreev610be9d2016-11-23 13:40:16 +0200499 * @deprecated 3.1.3 Use DateTime::createFromFormat($input_format, $input)->format($output_format);
Timothy Warren01b129a2012-04-27 11:36:50 -0400500 * @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 Barnesdc3e4be2011-12-19 13:48:29 -0500504 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300505 {
506 if (empty($bad_date))
507 {
508 return 'Unknown';
509 }
Andrey Andreevd9b44be2012-06-15 16:07:08 +0300510 elseif (empty($format))
511 {
512 $format = 'U';
513 }
Greg Akerf9168392011-08-29 19:29:05 -0500514
Kyle Farris896d95a2011-08-21 23:03:54 -0300515 // Date like: YYYYMM
Andrey Andreevf11a1932012-06-14 16:35:09 +0300516 if (preg_match('/^\d{6}$/i', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300517 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300518 if (in_array(substr($bad_date, 0, 2), array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300519 {
520 $year = substr($bad_date, 0, 4);
521 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500522 }
523 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300524 {
525 $month = substr($bad_date, 0, 2);
526 $year = substr($bad_date, 2, 4);
527 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500528
Andrey Andreevae31eb52012-05-17 14:54:15 +0300529 return date($format, strtotime($year.'-'.$month.'-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300530 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500531
Kyle Farris896d95a2011-08-21 23:03:54 -0300532 // Date Like: YYYYMMDD
Andrey Andreev820d9cd2016-11-23 13:27:42 +0200533 if (preg_match('/^\d{8}$/i', $bad_date, $matches))
Kyle Farris896d95a2011-08-21 23:03:54 -0300534 {
Andrey Andreev820d9cd2016-11-23 13:27:42 +0200535 return DateTime::createFromFormat('Ymd', $bad_date)->format($format);
Kyle Farris896d95a2011-08-21 23:03:54 -0300536 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500537
Kyle Farris896d95a2011-08-21 23:03:54 -0300538 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
Andrey Andreevf11a1932012-06-14 16:35:09 +0300539 if (preg_match('/^(\d{1,2})-(\d{1,2})-(\d{4})$/i', $bad_date, $matches))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500540 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300541 return date($format, strtotime($matches[3].'-'.$matches[1].'-'.$matches[2]));
Kyle Farris896d95a2011-08-21 23:03:54 -0300542 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500543
Kyle Farris896d95a2011-08-21 23:03:54 -0300544 // Any other kind of string, when converted into UNIX time,
545 // produces "0 seconds after epoc..." is probably bad...
546 // return "Invalid Date".
Alex Bilbie773ccc32012-06-02 11:11:08 +0100547 if (date('U', strtotime($bad_date)) === '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500548 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300549 return 'Invalid Date';
Kyle Farris896d95a2011-08-21 23:03:54 -0300550 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500551
Kyle Farris896d95a2011-08-21 23:03:54 -0300552 // It's probably a valid-ish date format already
553 return date($format, strtotime($bad_date));
554 }
555}
556
557// ------------------------------------------------------------------------
558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559if ( ! function_exists('timezone_menu'))
560{
Timothy Warren01b129a2012-04-27 11:36:50 -0400561 /**
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 Whitney7540ded2012-06-22 12:02:10 -0700569 * @param mixed attributes
Timothy Warren01b129a2012-04-27 11:36:50 -0400570 * @return string
571 */
Mat Whitney7540ded2012-06-22 12:02:10 -0700572 function timezone_menu($default = 'UTC', $class = '', $name = 'timezones', $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 {
574 $CI =& get_instance();
575 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200576
Alex Bilbie773ccc32012-06-02 11:11:08 +0100577 $default = ($default === 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000578
579 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200580
Alex Bilbie773ccc32012-06-02 11:11:08 +0100581 if ($class !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 {
583 $menu .= ' class="'.$class.'"';
584 }
Barry Mienydd671972010-10-04 16:33:58 +0200585
Eric Barnesacedd2b2012-07-29 00:15:40 -0400586 $menu .= _stringify_attributes($attributes).">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200587
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 foreach (timezones() as $key => $val)
589 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100590 $selected = ($default === $key) ? ' selected="selected"' : '';
Andrey Andreevae31eb52012-05-17 14:54:15 +0300591 $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 }
593
Andrey Andreevae31eb52012-05-17 14:54:15 +0300594 return $menu.'</select>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 }
596}
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598// ------------------------------------------------------------------------
599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600if ( ! function_exists('timezones'))
601{
Timothy Warren01b129a2012-04-27 11:36:50 -0400602 /**
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 Allard2067d1a2008-11-13 22:59:24 +0000611 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 Mienydd671972010-10-04 16:33:58 +0200615
616 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500617 '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 Mienydd671972010-10-04 16:33:58 +0200658
Alex Bilbie773ccc32012-06-02 11:11:08 +0100659 if ($tz === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
661 return $zones;
662 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500663
Andrey Andreeve92df332012-03-26 22:44:20 +0300664 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 }
666}
667
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200668// ------------------------------------------------------------------------
669
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200670if ( ! function_exists('date_range'))
671{
Andrey Andreev14aa3172012-05-02 13:27:30 +0300672 /**
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 Andreev2f8bf9b2012-10-12 20:37:52 +0300680 * @param mixed is_unix Specifies whether the second parameter
Andrey Andreev14aa3172012-05-02 13:27:30 +0300681 * 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 Andreev2139ecd2012-01-11 23:58:50 +0200687 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 Lerdorf4ad89d82013-05-18 10:18:17 -0400697 if ( ( ! ctype_digit((string) $unix_start) && ($unix_start = @strtotime($unix_start)) === FALSE)
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200698 OR ( ! ctype_digit((string) $mixed) && ($is_unix === FALSE OR ($mixed = @strtotime($mixed)) === FALSE))
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200699 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 Lerdorf4ad89d82013-05-18 10:18:17 -0400706 return array(date($format, $unix_start));
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200707 }
708
709 $range = array();
710
Andrey Andreev30da39b2012-03-10 15:49:17 +0200711 $from = new DateTime();
Andrey Andreeva8382792016-07-28 16:40:12 +0300712 $from->setTimestamp($unix_start);
Andrey Andreev30da39b2012-03-10 15:49:17 +0200713
Andrey Andreev30da39b2012-03-10 15:49:17 +0200714 if ($is_unix)
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200715 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200716 $arg = new DateTime();
Andrey Andreeva8382792016-07-28 16:40:12 +0300717 $arg->setTimestamp($mixed);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200718 }
Andrey Andreev30da39b2012-03-10 15:49:17 +0200719 else
720 {
721 $arg = (int) $mixed;
722 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200723
Andrey Andreeva8382792016-07-28 16:40:12 +0300724 $period = new DatePeriod($from, new DateInterval('P1D'), $arg);
725 foreach ($period as $date)
Andrey Andreev30da39b2012-03-10 15:49:17 +0200726 {
Andrey Andreeva8382792016-07-28 16:40:12 +0300727 $range[] = $date->format($format);
Andrey Andreev30da39b2012-03-10 15:49:17 +0200728 }
Andrey Andreev30da39b2012-03-10 15:49:17 +0200729
Andrey Andreeva8382792016-07-28 16:40:12 +0300730 /* 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 Andreev30da39b2012-03-10 15:49:17 +0200737 $range[] = $arg->format($format);
738 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200739
740 return $range;
741 }
742}