blob: 73fd8086e918a233b9e292d959672612b387af93 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnesdc3e4be2011-12-19 13:48:29 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnesdc3e4be2011-12-19 13:48:29 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Date Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/date_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('now'))
42{
Timothy Warren01b129a2012-04-27 11:36:50 -040043 /**
44 * Get "now" time
45 *
Iban Eguia74009652012-06-13 22:57:50 +020046 * Returns time() based on the timezone parameter or on the
47 * "time_reference" setting
Timothy Warren01b129a2012-04-27 11:36:50 -040048 *
Iban Eguia895e98c2012-06-08 23:01:31 +020049 * @param string
Timothy Warren01b129a2012-04-27 11:36:50 -040050 * @return int
51 */
Iban Eguia83105952012-03-27 18:18:15 +020052 function now($timezone = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000053 {
Iban Eguiac88daba2012-06-11 13:58:30 +020054 if (empty($timezone))
Derek Allard2067d1a2008-11-13 22:59:24 +000055 {
Iban Eguiafeb14da2012-06-12 16:09:36 +020056 $timezone = config_item('time_reference');
Derek Allard2067d1a2008-11-13 22:59:24 +000057 }
Greg Akerc964e722011-08-29 19:31:29 -050058
Iban Eguiac88daba2012-06-11 13:58:30 +020059 if ($timezone === 'local' OR $timezone === date_default_timezone_get())
Iban Eguiae15e3dd2012-06-09 23:52:27 +020060 {
Iban Eguiac88daba2012-06-11 13:58:30 +020061 return time();
Iban Eguiae15e3dd2012-06-09 23:52:27 +020062 }
Derek Allard2067d1a2008-11-13 22:59:24 +000063
Iban Eguiac88daba2012-06-11 13:58:30 +020064 $datetime = new DateTime('now', new DateTimeZone($timezone));
65 sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
66
67 return mktime($hour, $minute, $second, $month, $day, $year);
Derek Allard2067d1a2008-11-13 22:59:24 +000068 }
69}
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071// ------------------------------------------------------------------------
72
Derek Allard2067d1a2008-11-13 22:59:24 +000073if ( ! function_exists('mdate'))
74{
Timothy Warren01b129a2012-04-27 11:36:50 -040075 /**
76 * Convert MySQL Style Datecodes
77 *
78 * This function is identical to PHPs date() function,
79 * except that it allows date codes to be formatted using
80 * the MySQL style, where each code letter is preceded
81 * with a percent sign: %Y %m %d etc...
82 *
83 * The benefit of doing dates this way is that you don't
84 * have to worry about escaping your text letters that
85 * match the date codes.
86 *
87 * @param string
88 * @param int
89 * @return int
90 */
Derek Allard2067d1a2008-11-13 22:59:24 +000091 function mdate($datestr = '', $time = '')
92 {
Alex Bilbie773ccc32012-06-02 11:11:08 +010093 if ($datestr === '')
Greg Akerf9168392011-08-29 19:29:05 -050094 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -050095 return '';
Greg Akerf9168392011-08-29 19:29:05 -050096 }
Andrey Andreeveef24062012-06-14 16:17:48 +030097 elseif (empty($time))
98 {
99 $time = now();
100 }
Barry Mienydd671972010-10-04 16:33:58 +0200101
Greg Akerf9168392011-08-29 19:29:05 -0500102 $datestr = str_replace(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500103 '%\\',
104 '',
Andrey Andreevae31eb52012-05-17 14:54:15 +0300105 preg_replace('/([a-z]+?){1}/i', '\\\\\\1', $datestr)
Greg Akerf9168392011-08-29 19:29:05 -0500106 );
Greg Akerc964e722011-08-29 19:31:29 -0500107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 return date($datestr, $time);
109 }
110}
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112// ------------------------------------------------------------------------
113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114if ( ! function_exists('standard_date'))
115{
Timothy Warren01b129a2012-04-27 11:36:50 -0400116 /**
117 * Standard Date
118 *
119 * Returns a date formatted according to the submitted standard.
120 *
Andrey Andreevac570332012-07-04 13:04:10 +0300121 * As of PHP 5.2, the DateTime extension provides constants that
122 * serve for the exact same purpose and are used with date().
Andrey Andreevac570332012-07-04 13:04:10 +0300123 *
Andrey Andreev29d909d2012-10-27 01:05:09 +0300124 * @todo Remove in version 3.1+.
125 * @deprecated 3.0.0 Use PHP's native date() instead.
126 * @link http://www.php.net/manual/en/class.datetime.php#datetime.constants.types
Andrey Andreevac570332012-07-04 13:04:10 +0300127 *
Andrey Andreev29d909d2012-10-27 01:05:09 +0300128 * @example date(DATE_RFC822, now()); // default
129 * @example date(DATE_W3C, $time); // a different format and time
Andrey Andreevac570332012-07-04 13:04:10 +0300130 *
Andrey Andreev29d909d2012-10-27 01:05:09 +0300131 * @param string $fmt = 'DATE_RFC822' the chosen format
132 * @param int $time = NULL Unix timestamp
Timothy Warren01b129a2012-04-27 11:36:50 -0400133 * @return string
134 */
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300135 function standard_date($fmt = 'DATE_RFC822', $time = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300137 if (empty($time))
138 {
139 $time = now();
140 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000141
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300142 // Procedural style pre-defined constants from the DateTime extension
143 if (strpos($fmt, 'DATE_') !== 0 OR defined($fmt) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
145 return FALSE;
146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300148 return date(constant($fmt), $time);
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150}
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152// ------------------------------------------------------------------------
153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154if ( ! function_exists('timespan'))
155{
Timothy Warren01b129a2012-04-27 11:36:50 -0400156 /**
157 * Timespan
158 *
159 * Returns a span of seconds in this format:
160 * 10 days 14 hours 36 minutes 47 seconds
161 *
162 * @param int a number of seconds
163 * @param int Unix timestamp
164 * @param int a number of display units
165 * @return string
166 */
Roger Herbert8d69aa12012-03-14 08:44:55 +0000167 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 $CI =& get_instance();
170 $CI->lang->load('date');
171
Andrey Andreeveef24062012-06-14 16:17:48 +0300172 is_numeric($seconds) OR $seconds = 1;
173 is_numeric($time) OR $time = time();
174 is_numeric($units) OR $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000175
Greg Akerf9168392011-08-29 19:29:05 -0500176 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200177
Roger Herbert04c146d2012-03-11 15:31:01 +0000178 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500179 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200182 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300183 $str[] = $years.' '.$CI->lang->line($years > 1 ? 'date_years' : 'date_year');
Barry Mienydd671972010-10-04 16:33:58 +0200184 }
185
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500186 $seconds -= $years * 31557600;
187 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200188
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000189 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
191 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200192 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300193 $str[] = $months.' '.$CI->lang->line($months > 1 ? 'date_months' : 'date_month');
Barry Mienydd671972010-10-04 16:33:58 +0200194 }
195
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500196 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198
199 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200200
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000201 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
203 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200204 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300205 $str[] = $weeks.' '.$CI->lang->line($weeks > 1 ? 'date_weeks' : 'date_week');
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200209 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000210
211 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200212
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000213 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
215 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200216 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300217 $str[] = $days.' '.$CI->lang->line($days > 1 ? 'date_days' : 'date_day');
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 $seconds -= $days * 86400;
221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200224
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000225 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
227 if ($hours > 0)
228 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300229 $str[] = $hours.' '.$CI->lang->line($hours > 1 ? 'date_hours' : 'date_hour');
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 $seconds -= $hours * 3600;
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200236
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000237 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
239 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200240 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300241 $str[] = $minutes.' '.$CI->lang->line($minutes > 1 ? 'date_minutes' : 'date_minute');
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 $seconds -= $minutes * 60;
245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Roger Herbert597eb212012-03-14 09:06:17 +0000247 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300249 $str[] = $seconds.' '.$CI->lang->line($seconds > 1 ? 'date_seconds' : 'date_second');
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Roger Herbert04c146d2012-03-11 15:31:01 +0000252 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
254}
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256// ------------------------------------------------------------------------
257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258if ( ! function_exists('days_in_month'))
259{
Timothy Warren01b129a2012-04-27 11:36:50 -0400260 /**
261 * Number of days in a month
262 *
263 * Takes a month/year as input and returns the number of days
264 * for the given month/year. Takes leap years into consideration.
265 *
266 * @param int a numeric month
267 * @param int a numeric year
268 * @return int
269 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 function days_in_month($month = 0, $year = '')
271 {
272 if ($month < 1 OR $month > 12)
273 {
274 return 0;
275 }
Andrey Andreeveef24062012-06-14 16:17:48 +0300276 elseif ( ! is_numeric($year) OR strlen($year) !== 4)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
278 $year = date('Y');
279 }
Barry Mienydd671972010-10-04 16:33:58 +0200280
Andrey Andreevbe368a82014-02-20 15:46:05 +0200281 if (defined('CAL_GREGORIAN'))
282 {
283 return cal_days_in_month(CAL_GREGORIAN, $month, $year);
284 }
285
Andrey Andreeveef24062012-06-14 16:17:48 +0300286 if ($year >= 1970)
287 {
288 return (int) date('t', mktime(12, 0, 0, $month, 1, $year));
289 }
290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 if ($month == 2)
292 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100293 if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
295 return 29;
296 }
297 }
298
299 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
300 return $days_in_month[$month - 1];
301 }
302}
Derek Jones36591092010-03-05 10:05:51 -0600303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304// ------------------------------------------------------------------------
305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306if ( ! function_exists('local_to_gmt'))
307{
Timothy Warren01b129a2012-04-27 11:36:50 -0400308 /**
309 * Converts a local Unix timestamp to GMT
310 *
311 * @param int Unix timestamp
312 * @return int
313 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 function local_to_gmt($time = '')
315 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100316 if ($time === '')
Greg Akerf9168392011-08-29 19:29:05 -0500317 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500319 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500320
Greg Akerf9168392011-08-29 19:29:05 -0500321 return mktime(
Andrey Andreevb089e152012-06-16 19:11:40 +0300322 gmdate('G', $time),
Andrey Andreevae31eb52012-05-17 14:54:15 +0300323 gmdate('i', $time),
324 gmdate('s', $time),
Andrey Andreevb089e152012-06-16 19:11:40 +0300325 gmdate('n', $time),
326 gmdate('j', $time),
Andrey Andreevae31eb52012-05-17 14:54:15 +0300327 gmdate('Y', $time)
Greg Akerf9168392011-08-29 19:29:05 -0500328 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
330}
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332// ------------------------------------------------------------------------
333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334if ( ! function_exists('gmt_to_local'))
335{
Timothy Warren01b129a2012-04-27 11:36:50 -0400336 /**
337 * Converts GMT time to a localized value
338 *
339 * Takes a Unix timestamp (in GMT) as input, and returns
340 * at the local value based on the timezone and DST setting
341 * submitted
342 *
343 * @param int Unix timestamp
344 * @param string timezone
345 * @param bool whether DST is active
346 * @return int
347 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200349 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100350 if ($time === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
352 return now();
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 $time += timezones($timezone) * 3600;
356
Andrey Andreeveef24062012-06-14 16:17:48 +0300357 return ($dst === TRUE) ? $time + 3600 : $time;
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
359}
Derek Jones36591092010-03-05 10:05:51 -0600360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361// ------------------------------------------------------------------------
362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363if ( ! function_exists('mysql_to_unix'))
364{
Timothy Warren01b129a2012-04-27 11:36:50 -0400365 /**
366 * Converts a MySQL Timestamp to Unix
367 *
webmasterar69779af2012-11-17 01:06:41 +0000368 * @param int MySQL timestamp YYYY-MM-DD HH:MM:SS
369 * @return int Unix timstamp
Timothy Warren01b129a2012-04-27 11:36:50 -0400370 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 function mysql_to_unix($time = '')
372 {
373 // We'll remove certain characters for backward compatibility
374 // since the formatting changed with MySQL 4.1
375 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200376
Andrey Andreev3bbbd262012-06-14 13:35:32 +0300377 $time = str_replace(array('-', ':', ' '), '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500380 return mktime(
381 substr($time, 8, 2),
382 substr($time, 10, 2),
383 substr($time, 12, 2),
384 substr($time, 4, 2),
385 substr($time, 6, 2),
386 substr($time, 0, 4)
387 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
389}
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391// ------------------------------------------------------------------------
392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393if ( ! function_exists('unix_to_human'))
394{
Timothy Warren01b129a2012-04-27 11:36:50 -0400395 /**
396 * Unix to "Human"
397 *
398 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
399 *
400 * @param int Unix timestamp
401 * @param bool whether to show seconds
402 * @param string format: us or euro
403 * @return string
404 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
406 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300407 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200408
Alex Bilbie773ccc32012-06-02 11:11:08 +0100409 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
411 $r .= date('h', $time).':'.date('i', $time);
412 }
413 else
414 {
415 $r .= date('H', $time).':'.date('i', $time);
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 if ($seconds)
419 {
420 $r .= ':'.date('s', $time);
421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Alex Bilbie773ccc32012-06-02 11:11:08 +0100423 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300425 return $r.' '.date('A', $time);
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 return $r;
429 }
430}
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432// ------------------------------------------------------------------------
433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434if ( ! function_exists('human_to_unix'))
435{
Timothy Warren01b129a2012-04-27 11:36:50 -0400436 /**
437 * Convert "human" date to GMT
438 *
439 * Reverses the above process
440 *
441 * @param string format: us or euro
442 * @return int
443 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 function human_to_unix($datestr = '')
445 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100446 if ($datestr === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
448 return FALSE;
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Andrey Andreevae31eb52012-05-17 14:54:15 +0300451 $datestr = preg_replace('/\040+/', ' ', trim($datestr));
Barry Mienydd671972010-10-04 16:33:58 +0200452
Andrey Andreevf11a1932012-06-14 16:35:09 +0300453 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 +0000454 {
455 return FALSE;
456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Andrey Andreeve24eed72012-11-02 23:33:45 +0200458 sscanf($datestr, '%d-%d-%d %s %s', $year, $month, $day, $time, $ampm);
459 sscanf($time, '%d:%d:%d', $hour, $min, $sec);
460 isset($sec) OR $sec = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000461
Andrey Andreeve24eed72012-11-02 23:33:45 +0200462 if (isset($ampm))
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
Andrey Andreeve24eed72012-11-02 23:33:45 +0200464 $ampm = strtolower($ampm);
Barry Mienydd671972010-10-04 16:33:58 +0200465
Andrey Andreevf11a1932012-06-14 16:35:09 +0300466 if ($ampm[0] === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500467 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300468 $hour += 12;
Greg Akerf9168392011-08-29 19:29:05 -0500469 }
Andrey Andreevf11a1932012-06-14 16:35:09 +0300470 elseif ($ampm[0] === 'a' && $hour === 12)
Greg Akerf9168392011-08-29 19:29:05 -0500471 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300472 $hour = 0;
Greg Akerf9168392011-08-29 19:29:05 -0500473 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 return mktime($hour, $min, $sec, $month, $day, $year);
477 }
478}
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480// ------------------------------------------------------------------------
481
Kyle Farris896d95a2011-08-21 23:03:54 -0300482if ( ! function_exists('nice_date'))
483{
Timothy Warren01b129a2012-04-27 11:36:50 -0400484 /**
485 * Turns many "reasonably-date-like" strings into something
486 * that is actually useful. This only works for dates after unix epoch.
487 *
488 * @param string The terribly formatted date-like string
489 * @param string Date format to return (same as php date function)
490 * @return string
491 */
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500492 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300493 {
494 if (empty($bad_date))
495 {
496 return 'Unknown';
497 }
Andrey Andreevd9b44be2012-06-15 16:07:08 +0300498 elseif (empty($format))
499 {
500 $format = 'U';
501 }
Greg Akerf9168392011-08-29 19:29:05 -0500502
Kyle Farris896d95a2011-08-21 23:03:54 -0300503 // Date like: YYYYMM
Andrey Andreevf11a1932012-06-14 16:35:09 +0300504 if (preg_match('/^\d{6}$/i', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300505 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300506 if (in_array(substr($bad_date, 0, 2), array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300507 {
508 $year = substr($bad_date, 0, 4);
509 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500510 }
511 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300512 {
513 $month = substr($bad_date, 0, 2);
514 $year = substr($bad_date, 2, 4);
515 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500516
Andrey Andreevae31eb52012-05-17 14:54:15 +0300517 return date($format, strtotime($year.'-'.$month.'-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300518 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500519
Kyle Farris896d95a2011-08-21 23:03:54 -0300520 // Date Like: YYYYMMDD
Andrey Andreevf11a1932012-06-14 16:35:09 +0300521 if (preg_match('/^(\d{2})\d{2}(\d{4})$/i', $bad_date, $matches))
Kyle Farris896d95a2011-08-21 23:03:54 -0300522 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300523 return date($format, strtotime($matches[1].'/01/'.$matches[2]));
Kyle Farris896d95a2011-08-21 23:03:54 -0300524 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500525
Kyle Farris896d95a2011-08-21 23:03:54 -0300526 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
Andrey Andreevf11a1932012-06-14 16:35:09 +0300527 if (preg_match('/^(\d{1,2})-(\d{1,2})-(\d{4})$/i', $bad_date, $matches))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500528 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300529 return date($format, strtotime($matches[3].'-'.$matches[1].'-'.$matches[2]));
Kyle Farris896d95a2011-08-21 23:03:54 -0300530 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500531
Kyle Farris896d95a2011-08-21 23:03:54 -0300532 // Any other kind of string, when converted into UNIX time,
533 // produces "0 seconds after epoc..." is probably bad...
534 // return "Invalid Date".
Alex Bilbie773ccc32012-06-02 11:11:08 +0100535 if (date('U', strtotime($bad_date)) === '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500536 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300537 return 'Invalid Date';
Kyle Farris896d95a2011-08-21 23:03:54 -0300538 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500539
Kyle Farris896d95a2011-08-21 23:03:54 -0300540 // It's probably a valid-ish date format already
541 return date($format, strtotime($bad_date));
542 }
543}
544
545// ------------------------------------------------------------------------
546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547if ( ! function_exists('timezone_menu'))
548{
Timothy Warren01b129a2012-04-27 11:36:50 -0400549 /**
550 * Timezone Menu
551 *
552 * Generates a drop-down menu of timezones.
553 *
554 * @param string timezone
555 * @param string classname
556 * @param string menu name
Mat Whitney7540ded2012-06-22 12:02:10 -0700557 * @param mixed attributes
Timothy Warren01b129a2012-04-27 11:36:50 -0400558 * @return string
559 */
Mat Whitney7540ded2012-06-22 12:02:10 -0700560 function timezone_menu($default = 'UTC', $class = '', $name = 'timezones', $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 {
562 $CI =& get_instance();
563 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200564
Alex Bilbie773ccc32012-06-02 11:11:08 +0100565 $default = ($default === 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000566
567 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200568
Alex Bilbie773ccc32012-06-02 11:11:08 +0100569 if ($class !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
571 $menu .= ' class="'.$class.'"';
572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573
Eric Barnesacedd2b2012-07-29 00:15:40 -0400574 $menu .= _stringify_attributes($attributes).">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 foreach (timezones() as $key => $val)
577 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100578 $selected = ($default === $key) ? ' selected="selected"' : '';
Andrey Andreevae31eb52012-05-17 14:54:15 +0300579 $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 }
581
Andrey Andreevae31eb52012-05-17 14:54:15 +0300582 return $menu.'</select>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 }
584}
Barry Mienydd671972010-10-04 16:33:58 +0200585
Derek Allard2067d1a2008-11-13 22:59:24 +0000586// ------------------------------------------------------------------------
587
Derek Allard2067d1a2008-11-13 22:59:24 +0000588if ( ! function_exists('timezones'))
589{
Timothy Warren01b129a2012-04-27 11:36:50 -0400590 /**
591 * Timezones
592 *
593 * Returns an array of timezones. This is a helper function
594 * for various other ones in this library
595 *
596 * @param string timezone
597 * @return string
598 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 function timezones($tz = '')
600 {
601 // Note: Don't change the order of these even though
602 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200603
604 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500605 'UM12' => -12,
606 'UM11' => -11,
607 'UM10' => -10,
608 'UM95' => -9.5,
609 'UM9' => -9,
610 'UM8' => -8,
611 'UM7' => -7,
612 'UM6' => -6,
613 'UM5' => -5,
614 'UM45' => -4.5,
615 'UM4' => -4,
616 'UM35' => -3.5,
617 'UM3' => -3,
618 'UM2' => -2,
619 'UM1' => -1,
620 'UTC' => 0,
621 'UP1' => +1,
622 'UP2' => +2,
623 'UP3' => +3,
624 'UP35' => +3.5,
625 'UP4' => +4,
626 'UP45' => +4.5,
627 'UP5' => +5,
628 'UP55' => +5.5,
629 'UP575' => +5.75,
630 'UP6' => +6,
631 'UP65' => +6.5,
632 'UP7' => +7,
633 'UP8' => +8,
634 'UP875' => +8.75,
635 'UP9' => +9,
636 'UP95' => +9.5,
637 'UP10' => +10,
638 'UP105' => +10.5,
639 'UP11' => +11,
640 'UP115' => +11.5,
641 'UP12' => +12,
642 'UP1275' => +12.75,
643 'UP13' => +13,
644 'UP14' => +14
645 );
Barry Mienydd671972010-10-04 16:33:58 +0200646
Alex Bilbie773ccc32012-06-02 11:11:08 +0100647 if ($tz === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
649 return $zones;
650 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500651
Andrey Andreeve92df332012-03-26 22:44:20 +0300652 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 }
654}
655
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200656// ------------------------------------------------------------------------
657
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200658if ( ! function_exists('date_range'))
659{
Andrey Andreev14aa3172012-05-02 13:27:30 +0300660 /**
661 * Date range
662 *
663 * Returns a list of dates within a specified period.
664 *
665 * @param int unix_start UNIX timestamp of period start date
666 * @param int unix_end|days UNIX timestamp of period end date
667 * or interval in days.
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300668 * @param mixed is_unix Specifies whether the second parameter
Andrey Andreev14aa3172012-05-02 13:27:30 +0300669 * is a UNIX timestamp or a day interval
670 * - TRUE or 'unix' for a timestamp
671 * - FALSE or 'days' for an interval
672 * @param string date_format Output date format, same as in date()
673 * @return array
674 */
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200675 function date_range($unix_start = '', $mixed = '', $is_unix = TRUE, $format = 'Y-m-d')
676 {
677 if ($unix_start == '' OR $mixed == '' OR $format == '')
678 {
679 return FALSE;
680 }
681
682 $is_unix = ! ( ! $is_unix OR $is_unix === 'days');
683
684 // Validate input and try strtotime() on invalid timestamps/intervals, just in case
Rasmus Lerdorf4ad89d82013-05-18 10:18:17 -0400685 if ( ( ! ctype_digit((string) $unix_start) && ($unix_start = @strtotime($unix_start)) === FALSE)
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200686 OR ( ! ctype_digit((string) $mixed) && ($is_unix === FALSE OR ($mixed = @strtotime($mixed)) === FALSE))
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200687 OR ($is_unix === TRUE && $mixed < $unix_start))
688 {
689 return FALSE;
690 }
691
692 if ($is_unix && ($unix_start == $mixed OR date($format, $unix_start) === date($format, $mixed)))
693 {
Rasmus Lerdorf4ad89d82013-05-18 10:18:17 -0400694 return array(date($format, $unix_start));
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200695 }
696
697 $range = array();
698
Andrey Andreev30da39b2012-03-10 15:49:17 +0200699 /* NOTE: Even though the DateTime object has many useful features, it appears that
700 * it doesn't always handle properly timezones, when timestamps are passed
701 * directly to its constructor. Neither of the following gave proper results:
702 *
703 * new DateTime('<timestamp>')
704 * new DateTime('<timestamp>', '<timezone>')
705 *
706 * --- available in PHP 5.3:
707 *
708 * DateTime::createFromFormat('<format>', '<timestamp>')
709 * DateTime::createFromFormat('<format>', '<timestamp>', '<timezone')
710 *
711 * ... so we'll have to set the timestamp after the object is instantiated.
712 * Furthermore, in PHP 5.3 we can use DateTime::setTimestamp() to do that and
713 * given that we have UNIX timestamps - we should use it.
714 */
715 $from = new DateTime();
716
717 if (is_php('5.3'))
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200718 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200719 $from->setTimestamp($unix_start);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200720 if ($is_unix)
721 {
722 $arg = new DateTime();
Andrey Andreev30da39b2012-03-10 15:49:17 +0200723 $arg->setTimestamp($mixed);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200724 }
725 else
726 {
727 $arg = (int) $mixed;
728 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200729
Andrey Andreev30da39b2012-03-10 15:49:17 +0200730 $period = new DatePeriod($from, new DateInterval('P1D'), $arg);
731 foreach ($period as $date)
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200732 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200733 $range[] = $date->format($format);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200734 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200735
Andrey Andreev30da39b2012-03-10 15:49:17 +0200736 /* If a period end date was passed to the DatePeriod constructor, it might not
737 * be in our results. Not sure if this is a bug or it's just possible because
738 * the end date might actually be less than 24 hours away from the previously
739 * generated DateTime object, but either way - we have to append it manually.
740 */
741 if ( ! is_int($arg) && $range[count($range) - 1] !== $arg->format($format))
742 {
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200743 $range[] = $arg->format($format);
744 }
745
746 return $range;
747 }
748
Andrey Andreev30da39b2012-03-10 15:49:17 +0200749 $from->setDate(date('Y', $unix_start), date('n', $unix_start), date('j', $unix_start));
750 $from->setTime(date('G', $unix_start), date('i', $unix_start), date('s', $unix_start));
751 if ($is_unix)
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200752 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200753 $arg = new DateTime();
754 $arg->setDate(date('Y', $mixed), date('n', $mixed), date('j', $mixed));
755 $arg->setTime(date('G', $mixed), date('i', $mixed), date('s', $mixed));
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200756 }
Andrey Andreev30da39b2012-03-10 15:49:17 +0200757 else
758 {
759 $arg = (int) $mixed;
760 }
761 $range[] = $from->format($format);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200762
Andrey Andreev30da39b2012-03-10 15:49:17 +0200763 if (is_int($arg)) // Day intervals
764 {
765 do
766 {
767 $from->modify('+1 day');
768 $range[] = $from->format($format);
769 }
770 while (--$arg > 0);
771 }
772 else // end date UNIX timestamp
773 {
774 for ($from->modify('+1 day'), $end_check = $arg->format('Ymd'); $from->format('Ymd') < $end_check; $from->modify('+1 day'))
775 {
776 $range[] = $from->format($format);
777 }
778
779 // Our loop only appended dates prior to our end date
780 $range[] = $arg->format($format);
781 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200782
783 return $range;
784 }
785}
786
Derek Allard2067d1a2008-11-13 22:59:24 +0000787/* End of file date_helper.php */
Andrey Andreevc5a7c5f2013-07-17 20:10:09 +0300788/* Location: ./system/helpers/date_helper.php */