blob: 5d9251526a570d805e71fb08a458427cc56eac85 [file] [log] [blame]
Andrey Andreeve684bda2012-03-26 13:47:29 +03001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Date Helpers
30 *
31 * @package CodeIgniter
32 * @subpackage Helpers
33 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/helpers/date_helper.html
36 */
37
38// ------------------------------------------------------------------------
39
Derek Allard2067d1a2008-11-13 22:59:24 +000040if ( ! function_exists('now'))
41{
Timothy Warren01b129a2012-04-27 11:36:50 -040042 /**
43 * Get "now" time
44 *
Iban Eguia74009652012-06-13 22:57:50 +020045 * Returns time() based on the timezone parameter or on the
46 * "time_reference" setting
Timothy Warren01b129a2012-04-27 11:36:50 -040047 *
Iban Eguia895e98c2012-06-08 23:01:31 +020048 * @param string
Timothy Warren01b129a2012-04-27 11:36:50 -040049 * @return int
50 */
Iban Eguia83105952012-03-27 18:18:15 +020051 function now($timezone = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000052 {
Iban Eguiac88daba2012-06-11 13:58:30 +020053 if (empty($timezone))
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
Iban Eguiafeb14da2012-06-12 16:09:36 +020055 $timezone = config_item('time_reference');
Derek Allard2067d1a2008-11-13 22:59:24 +000056 }
Greg Akerc964e722011-08-29 19:31:29 -050057
Iban Eguiac88daba2012-06-11 13:58:30 +020058 if ($timezone === 'local' OR $timezone === date_default_timezone_get())
Iban Eguiae15e3dd2012-06-09 23:52:27 +020059 {
Iban Eguiac88daba2012-06-11 13:58:30 +020060 return time();
Iban Eguiae15e3dd2012-06-09 23:52:27 +020061 }
Derek Allard2067d1a2008-11-13 22:59:24 +000062
Iban Eguiac88daba2012-06-11 13:58:30 +020063 $datetime = new DateTime('now', new DateTimeZone($timezone));
64 sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
65
66 return mktime($hour, $minute, $second, $month, $day, $year);
Derek Allard2067d1a2008-11-13 22:59:24 +000067 }
68}
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070// ------------------------------------------------------------------------
71
Derek Allard2067d1a2008-11-13 22:59:24 +000072if ( ! function_exists('mdate'))
73{
Timothy Warren01b129a2012-04-27 11:36:50 -040074 /**
75 * Convert MySQL Style Datecodes
76 *
77 * This function is identical to PHPs date() function,
78 * except that it allows date codes to be formatted using
79 * the MySQL style, where each code letter is preceded
80 * with a percent sign: %Y %m %d etc...
81 *
82 * The benefit of doing dates this way is that you don't
83 * have to worry about escaping your text letters that
84 * match the date codes.
85 *
86 * @param string
87 * @param int
88 * @return int
89 */
Derek Allard2067d1a2008-11-13 22:59:24 +000090 function mdate($datestr = '', $time = '')
91 {
Alex Bilbie773ccc32012-06-02 11:11:08 +010092 if ($datestr === '')
Greg Akerf9168392011-08-29 19:29:05 -050093 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -050094 return '';
Greg Akerf9168392011-08-29 19:29:05 -050095 }
Andrey Andreeveef24062012-06-14 16:17:48 +030096 elseif (empty($time))
97 {
98 $time = now();
99 }
Barry Mienydd671972010-10-04 16:33:58 +0200100
Greg Akerf9168392011-08-29 19:29:05 -0500101 $datestr = str_replace(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500102 '%\\',
103 '',
Andrey Andreevae31eb52012-05-17 14:54:15 +0300104 preg_replace('/([a-z]+?){1}/i', '\\\\\\1', $datestr)
Greg Akerf9168392011-08-29 19:29:05 -0500105 );
Greg Akerc964e722011-08-29 19:31:29 -0500106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 return date($datestr, $time);
108 }
109}
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111// ------------------------------------------------------------------------
112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113if ( ! function_exists('standard_date'))
114{
Timothy Warren01b129a2012-04-27 11:36:50 -0400115 /**
116 * Standard Date
117 *
118 * Returns a date formatted according to the submitted standard.
119 *
Andrey Andreevac570332012-07-04 13:04:10 +0300120 * As of PHP 5.2, the DateTime extension provides constants that
121 * serve for the exact same purpose and are used with date().
Andrey Andreevac570332012-07-04 13:04:10 +0300122 *
Andrey Andreev29d909d2012-10-27 01:05:09 +0300123 * @todo Remove in version 3.1+.
124 * @deprecated 3.0.0 Use PHP's native date() instead.
125 * @link http://www.php.net/manual/en/class.datetime.php#datetime.constants.types
Andrey Andreevac570332012-07-04 13:04:10 +0300126 *
Andrey Andreev29d909d2012-10-27 01:05:09 +0300127 * @example date(DATE_RFC822, now()); // default
128 * @example date(DATE_W3C, $time); // a different format and time
Andrey Andreevac570332012-07-04 13:04:10 +0300129 *
Andrey Andreev29d909d2012-10-27 01:05:09 +0300130 * @param string $fmt = 'DATE_RFC822' the chosen format
131 * @param int $time = NULL Unix timestamp
Timothy Warren01b129a2012-04-27 11:36:50 -0400132 * @return string
133 */
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300134 function standard_date($fmt = 'DATE_RFC822', $time = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300136 if (empty($time))
137 {
138 $time = now();
139 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000140
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300141 // Procedural style pre-defined constants from the DateTime extension
142 if (strpos($fmt, 'DATE_') !== 0 OR defined($fmt) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
144 return FALSE;
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300147 return date(constant($fmt), $time);
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
149}
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151// ------------------------------------------------------------------------
152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153if ( ! function_exists('timespan'))
154{
Timothy Warren01b129a2012-04-27 11:36:50 -0400155 /**
156 * Timespan
157 *
158 * Returns a span of seconds in this format:
159 * 10 days 14 hours 36 minutes 47 seconds
160 *
161 * @param int a number of seconds
162 * @param int Unix timestamp
163 * @param int a number of display units
164 * @return string
165 */
Roger Herbert8d69aa12012-03-14 08:44:55 +0000166 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
168 $CI =& get_instance();
169 $CI->lang->load('date');
170
Andrey Andreeveef24062012-06-14 16:17:48 +0300171 is_numeric($seconds) OR $seconds = 1;
172 is_numeric($time) OR $time = time();
173 is_numeric($units) OR $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000174
Greg Akerf9168392011-08-29 19:29:05 -0500175 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200176
Roger Herbert04c146d2012-03-11 15:31:01 +0000177 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500178 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200181 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300182 $str[] = $years.' '.$CI->lang->line($years > 1 ? 'date_years' : 'date_year');
Barry Mienydd671972010-10-04 16:33:58 +0200183 }
184
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500185 $seconds -= $years * 31557600;
186 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200187
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000188 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
190 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200191 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300192 $str[] = $months.' '.$CI->lang->line($months > 1 ? 'date_months' : 'date_month');
Barry Mienydd671972010-10-04 16:33:58 +0200193 }
194
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500195 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
197
198 $weeks = floor($seconds / 604800);
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 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
202 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200203 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300204 $str[] = $weeks.' '.$CI->lang->line($weeks > 1 ? 'date_weeks' : 'date_week');
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200208 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000209
210 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200211
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000212 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200215 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300216 $str[] = $days.' '.$CI->lang->line($days > 1 ? 'date_days' : 'date_day');
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 -= $days * 86400;
220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200223
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000224 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
226 if ($hours > 0)
227 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300228 $str[] = $hours.' '.$CI->lang->line($hours > 1 ? 'date_hours' : 'date_hour');
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 -= $hours * 3600;
232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 $minutes = floor($seconds / 60);
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 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
238 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200239 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300240 $str[] = $minutes.' '.$CI->lang->line($minutes > 1 ? 'date_minutes' : 'date_minute');
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 -= $minutes * 60;
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Roger Herbert597eb212012-03-14 09:06:17 +0000246 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300248 $str[] = $seconds.' '.$CI->lang->line($seconds > 1 ? 'date_seconds' : 'date_second');
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Roger Herbert04c146d2012-03-11 15:31:01 +0000251 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
253}
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255// ------------------------------------------------------------------------
256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257if ( ! function_exists('days_in_month'))
258{
Timothy Warren01b129a2012-04-27 11:36:50 -0400259 /**
260 * Number of days in a month
261 *
262 * Takes a month/year as input and returns the number of days
263 * for the given month/year. Takes leap years into consideration.
264 *
265 * @param int a numeric month
266 * @param int a numeric year
267 * @return int
268 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 function days_in_month($month = 0, $year = '')
270 {
271 if ($month < 1 OR $month > 12)
272 {
273 return 0;
274 }
Andrey Andreeveef24062012-06-14 16:17:48 +0300275 elseif ( ! is_numeric($year) OR strlen($year) !== 4)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
277 $year = date('Y');
278 }
Barry Mienydd671972010-10-04 16:33:58 +0200279
Andrey Andreeveef24062012-06-14 16:17:48 +0300280 if ($year >= 1970)
281 {
282 return (int) date('t', mktime(12, 0, 0, $month, 1, $year));
283 }
284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 if ($month == 2)
286 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100287 if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 return 29;
290 }
291 }
292
293 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
294 return $days_in_month[$month - 1];
295 }
296}
Derek Jones36591092010-03-05 10:05:51 -0600297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298// ------------------------------------------------------------------------
299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300if ( ! function_exists('local_to_gmt'))
301{
Timothy Warren01b129a2012-04-27 11:36:50 -0400302 /**
303 * Converts a local Unix timestamp to GMT
304 *
305 * @param int Unix timestamp
306 * @return int
307 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 function local_to_gmt($time = '')
309 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100310 if ($time === '')
Greg Akerf9168392011-08-29 19:29:05 -0500311 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500313 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500314
Greg Akerf9168392011-08-29 19:29:05 -0500315 return mktime(
Andrey Andreevb089e152012-06-16 19:11:40 +0300316 gmdate('G', $time),
Andrey Andreevae31eb52012-05-17 14:54:15 +0300317 gmdate('i', $time),
318 gmdate('s', $time),
Andrey Andreevb089e152012-06-16 19:11:40 +0300319 gmdate('n', $time),
320 gmdate('j', $time),
Andrey Andreevae31eb52012-05-17 14:54:15 +0300321 gmdate('Y', $time)
Greg Akerf9168392011-08-29 19:29:05 -0500322 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
324}
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326// ------------------------------------------------------------------------
327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328if ( ! function_exists('gmt_to_local'))
329{
Timothy Warren01b129a2012-04-27 11:36:50 -0400330 /**
331 * Converts GMT time to a localized value
332 *
333 * Takes a Unix timestamp (in GMT) as input, and returns
334 * at the local value based on the timezone and DST setting
335 * submitted
336 *
337 * @param int Unix timestamp
338 * @param string timezone
339 * @param bool whether DST is active
340 * @return int
341 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200343 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100344 if ($time === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
346 return now();
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 $time += timezones($timezone) * 3600;
350
Andrey Andreeveef24062012-06-14 16:17:48 +0300351 return ($dst === TRUE) ? $time + 3600 : $time;
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
353}
Derek Jones36591092010-03-05 10:05:51 -0600354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355// ------------------------------------------------------------------------
356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357if ( ! function_exists('mysql_to_unix'))
358{
Timothy Warren01b129a2012-04-27 11:36:50 -0400359 /**
360 * Converts a MySQL Timestamp to Unix
361 *
362 * @param int Unix timestamp
363 * @return int
364 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 function mysql_to_unix($time = '')
366 {
367 // We'll remove certain characters for backward compatibility
368 // since the formatting changed with MySQL 4.1
369 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200370
Andrey Andreev3bbbd262012-06-14 13:35:32 +0300371 $time = str_replace(array('-', ':', ' '), '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500374 return mktime(
375 substr($time, 8, 2),
376 substr($time, 10, 2),
377 substr($time, 12, 2),
378 substr($time, 4, 2),
379 substr($time, 6, 2),
380 substr($time, 0, 4)
381 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
383}
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385// ------------------------------------------------------------------------
386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387if ( ! function_exists('unix_to_human'))
388{
Timothy Warren01b129a2012-04-27 11:36:50 -0400389 /**
390 * Unix to "Human"
391 *
392 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
393 *
394 * @param int Unix timestamp
395 * @param bool whether to show seconds
396 * @param string format: us or euro
397 * @return string
398 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
400 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300401 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200402
Alex Bilbie773ccc32012-06-02 11:11:08 +0100403 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
405 $r .= date('h', $time).':'.date('i', $time);
406 }
407 else
408 {
409 $r .= date('H', $time).':'.date('i', $time);
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 if ($seconds)
413 {
414 $r .= ':'.date('s', $time);
415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Alex Bilbie773ccc32012-06-02 11:11:08 +0100417 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300419 return $r.' '.date('A', $time);
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 return $r;
423 }
424}
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426// ------------------------------------------------------------------------
427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428if ( ! function_exists('human_to_unix'))
429{
Timothy Warren01b129a2012-04-27 11:36:50 -0400430 /**
431 * Convert "human" date to GMT
432 *
433 * Reverses the above process
434 *
435 * @param string format: us or euro
436 * @return int
437 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 function human_to_unix($datestr = '')
439 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100440 if ($datestr === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
442 return FALSE;
443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Andrey Andreevae31eb52012-05-17 14:54:15 +0300445 $datestr = preg_replace('/\040+/', ' ', trim($datestr));
Barry Mienydd671972010-10-04 16:33:58 +0200446
Andrey Andreevf11a1932012-06-14 16:35:09 +0300447 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 +0000448 {
449 return FALSE;
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Jones36591092010-03-05 10:05:51 -0600452 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000453
Andrey Andreevf11a1932012-06-14 16:35:09 +0300454 list($year, $month, $day) = explode('-', $split[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000455
Andrey Andreevae31eb52012-05-17 14:54:15 +0300456 $ex = explode(':', $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200457
Andrey Andreevf11a1932012-06-14 16:35:09 +0300458 $hour = (int) $ex[0];
459 $min = (int) $ex[1];
460 $sec = ( ! empty($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2]))
461 ? (int) $ex[2] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000462
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300463 if (isset($split[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300465 $ampm = strtolower($split[2]);
Barry Mienydd671972010-10-04 16:33:58 +0200466
Andrey Andreevf11a1932012-06-14 16:35:09 +0300467 if ($ampm[0] === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500468 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300469 $hour += 12;
Greg Akerf9168392011-08-29 19:29:05 -0500470 }
Andrey Andreevf11a1932012-06-14 16:35:09 +0300471 elseif ($ampm[0] === 'a' && $hour === 12)
Greg Akerf9168392011-08-29 19:29:05 -0500472 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300473 $hour = 0;
Greg Akerf9168392011-08-29 19:29:05 -0500474 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 return mktime($hour, $min, $sec, $month, $day, $year);
478 }
479}
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481// ------------------------------------------------------------------------
482
Kyle Farris896d95a2011-08-21 23:03:54 -0300483if ( ! function_exists('nice_date'))
484{
Timothy Warren01b129a2012-04-27 11:36:50 -0400485 /**
486 * Turns many "reasonably-date-like" strings into something
487 * that is actually useful. This only works for dates after unix epoch.
488 *
489 * @param string The terribly formatted date-like string
490 * @param string Date format to return (same as php date function)
491 * @return string
492 */
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500493 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300494 {
495 if (empty($bad_date))
496 {
497 return 'Unknown';
498 }
Andrey Andreevd9b44be2012-06-15 16:07:08 +0300499 elseif (empty($format))
500 {
501 $format = 'U';
502 }
Greg Akerf9168392011-08-29 19:29:05 -0500503
Kyle Farris896d95a2011-08-21 23:03:54 -0300504 // Date like: YYYYMM
Andrey Andreevf11a1932012-06-14 16:35:09 +0300505 if (preg_match('/^\d{6}$/i', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300506 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300507 if (in_array(substr($bad_date, 0, 2), array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300508 {
509 $year = substr($bad_date, 0, 4);
510 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500511 }
512 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300513 {
514 $month = substr($bad_date, 0, 2);
515 $year = substr($bad_date, 2, 4);
516 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500517
Andrey Andreevae31eb52012-05-17 14:54:15 +0300518 return date($format, strtotime($year.'-'.$month.'-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300519 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500520
Kyle Farris896d95a2011-08-21 23:03:54 -0300521 // Date Like: YYYYMMDD
Andrey Andreevf11a1932012-06-14 16:35:09 +0300522 if (preg_match('/^(\d{2})\d{2}(\d{4})$/i', $bad_date, $matches))
Kyle Farris896d95a2011-08-21 23:03:54 -0300523 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300524 return date($format, strtotime($matches[1].'/01/'.$matches[2]));
Kyle Farris896d95a2011-08-21 23:03:54 -0300525 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500526
Kyle Farris896d95a2011-08-21 23:03:54 -0300527 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
Andrey Andreevf11a1932012-06-14 16:35:09 +0300528 if (preg_match('/^(\d{1,2})-(\d{1,2})-(\d{4})$/i', $bad_date, $matches))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500529 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300530 return date($format, strtotime($matches[3].'-'.$matches[1].'-'.$matches[2]));
Kyle Farris896d95a2011-08-21 23:03:54 -0300531 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500532
Kyle Farris896d95a2011-08-21 23:03:54 -0300533 // Any other kind of string, when converted into UNIX time,
534 // produces "0 seconds after epoc..." is probably bad...
535 // return "Invalid Date".
Alex Bilbie773ccc32012-06-02 11:11:08 +0100536 if (date('U', strtotime($bad_date)) === '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500537 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300538 return 'Invalid Date';
Kyle Farris896d95a2011-08-21 23:03:54 -0300539 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500540
Kyle Farris896d95a2011-08-21 23:03:54 -0300541 // It's probably a valid-ish date format already
542 return date($format, strtotime($bad_date));
543 }
544}
545
546// ------------------------------------------------------------------------
547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548if ( ! function_exists('timezone_menu'))
549{
Timothy Warren01b129a2012-04-27 11:36:50 -0400550 /**
551 * Timezone Menu
552 *
553 * Generates a drop-down menu of timezones.
554 *
555 * @param string timezone
556 * @param string classname
557 * @param string menu name
Mat Whitney7540ded2012-06-22 12:02:10 -0700558 * @param mixed attributes
Timothy Warren01b129a2012-04-27 11:36:50 -0400559 * @return string
560 */
Mat Whitney7540ded2012-06-22 12:02:10 -0700561 function timezone_menu($default = 'UTC', $class = '', $name = 'timezones', $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
563 $CI =& get_instance();
564 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200565
Alex Bilbie773ccc32012-06-02 11:11:08 +0100566 $default = ($default === 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000567
568 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200569
Alex Bilbie773ccc32012-06-02 11:11:08 +0100570 if ($class !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
572 $menu .= ' class="'.$class.'"';
573 }
Barry Mienydd671972010-10-04 16:33:58 +0200574
Eric Barnesacedd2b2012-07-29 00:15:40 -0400575 $menu .= _stringify_attributes($attributes).">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 foreach (timezones() as $key => $val)
578 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100579 $selected = ($default === $key) ? ' selected="selected"' : '';
Andrey Andreevae31eb52012-05-17 14:54:15 +0300580 $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 }
582
Andrey Andreevae31eb52012-05-17 14:54:15 +0300583 return $menu.'</select>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 }
585}
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587// ------------------------------------------------------------------------
588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589if ( ! function_exists('timezones'))
590{
Timothy Warren01b129a2012-04-27 11:36:50 -0400591 /**
592 * Timezones
593 *
594 * Returns an array of timezones. This is a helper function
595 * for various other ones in this library
596 *
597 * @param string timezone
598 * @return string
599 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 function timezones($tz = '')
601 {
602 // Note: Don't change the order of these even though
603 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200604
605 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500606 'UM12' => -12,
607 'UM11' => -11,
608 'UM10' => -10,
609 'UM95' => -9.5,
610 'UM9' => -9,
611 'UM8' => -8,
612 'UM7' => -7,
613 'UM6' => -6,
614 'UM5' => -5,
615 'UM45' => -4.5,
616 'UM4' => -4,
617 'UM35' => -3.5,
618 'UM3' => -3,
619 'UM2' => -2,
620 'UM1' => -1,
621 'UTC' => 0,
622 'UP1' => +1,
623 'UP2' => +2,
624 'UP3' => +3,
625 'UP35' => +3.5,
626 'UP4' => +4,
627 'UP45' => +4.5,
628 'UP5' => +5,
629 'UP55' => +5.5,
630 'UP575' => +5.75,
631 'UP6' => +6,
632 'UP65' => +6.5,
633 'UP7' => +7,
634 'UP8' => +8,
635 'UP875' => +8.75,
636 'UP9' => +9,
637 'UP95' => +9.5,
638 'UP10' => +10,
639 'UP105' => +10.5,
640 'UP11' => +11,
641 'UP115' => +11.5,
642 'UP12' => +12,
643 'UP1275' => +12.75,
644 'UP13' => +13,
645 'UP14' => +14
646 );
Barry Mienydd671972010-10-04 16:33:58 +0200647
Alex Bilbie773ccc32012-06-02 11:11:08 +0100648 if ($tz === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
650 return $zones;
651 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500652
Andrey Andreeve92df332012-03-26 22:44:20 +0300653 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 }
655}
656
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200657// ------------------------------------------------------------------------
658
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200659if ( ! function_exists('date_range'))
660{
Andrey Andreev14aa3172012-05-02 13:27:30 +0300661 /**
662 * Date range
663 *
664 * Returns a list of dates within a specified period.
665 *
666 * @param int unix_start UNIX timestamp of period start date
667 * @param int unix_end|days UNIX timestamp of period end date
668 * or interval in days.
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300669 * @param mixed is_unix Specifies whether the second parameter
Andrey Andreev14aa3172012-05-02 13:27:30 +0300670 * is a UNIX timestamp or a day interval
671 * - TRUE or 'unix' for a timestamp
672 * - FALSE or 'days' for an interval
673 * @param string date_format Output date format, same as in date()
674 * @return array
675 */
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200676 function date_range($unix_start = '', $mixed = '', $is_unix = TRUE, $format = 'Y-m-d')
677 {
678 if ($unix_start == '' OR $mixed == '' OR $format == '')
679 {
680 return FALSE;
681 }
682
683 $is_unix = ! ( ! $is_unix OR $is_unix === 'days');
684
685 // Validate input and try strtotime() on invalid timestamps/intervals, just in case
686 if ( ( ! preg_match('/^[0-9]+$/', $unix_start) && ($unix_start = @strtotime($unix_time)) === FALSE)
687 OR ( ! preg_match('/^[0-9]+$/', $mixed) && ($is_unix === FALSE OR ($mixed = @strtotime($mixed)) === FALSE))
688 OR ($is_unix === TRUE && $mixed < $unix_start))
689 {
690 return FALSE;
691 }
692
693 if ($is_unix && ($unix_start == $mixed OR date($format, $unix_start) === date($format, $mixed)))
694 {
695 return array($start_date);
696 }
697
698 $range = array();
699
Andrey Andreev30da39b2012-03-10 15:49:17 +0200700 /* NOTE: Even though the DateTime object has many useful features, it appears that
701 * it doesn't always handle properly timezones, when timestamps are passed
702 * directly to its constructor. Neither of the following gave proper results:
703 *
704 * new DateTime('<timestamp>')
705 * new DateTime('<timestamp>', '<timezone>')
706 *
707 * --- available in PHP 5.3:
708 *
709 * DateTime::createFromFormat('<format>', '<timestamp>')
710 * DateTime::createFromFormat('<format>', '<timestamp>', '<timezone')
711 *
712 * ... so we'll have to set the timestamp after the object is instantiated.
713 * Furthermore, in PHP 5.3 we can use DateTime::setTimestamp() to do that and
714 * given that we have UNIX timestamps - we should use it.
715 */
716 $from = new DateTime();
717
718 if (is_php('5.3'))
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200719 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200720 $from->setTimestamp($unix_start);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200721 if ($is_unix)
722 {
723 $arg = new DateTime();
Andrey Andreev30da39b2012-03-10 15:49:17 +0200724 $arg->setTimestamp($mixed);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200725 }
726 else
727 {
728 $arg = (int) $mixed;
729 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200730
Andrey Andreev30da39b2012-03-10 15:49:17 +0200731 $period = new DatePeriod($from, new DateInterval('P1D'), $arg);
732 foreach ($period as $date)
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200733 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200734 $range[] = $date->format($format);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200735 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200736
Andrey Andreev30da39b2012-03-10 15:49:17 +0200737 /* If a period end date was passed to the DatePeriod constructor, it might not
738 * be in our results. Not sure if this is a bug or it's just possible because
739 * the end date might actually be less than 24 hours away from the previously
740 * generated DateTime object, but either way - we have to append it manually.
741 */
742 if ( ! is_int($arg) && $range[count($range) - 1] !== $arg->format($format))
743 {
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200744 $range[] = $arg->format($format);
745 }
746
747 return $range;
748 }
749
Andrey Andreev30da39b2012-03-10 15:49:17 +0200750 $from->setDate(date('Y', $unix_start), date('n', $unix_start), date('j', $unix_start));
751 $from->setTime(date('G', $unix_start), date('i', $unix_start), date('s', $unix_start));
752 if ($is_unix)
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200753 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200754 $arg = new DateTime();
755 $arg->setDate(date('Y', $mixed), date('n', $mixed), date('j', $mixed));
756 $arg->setTime(date('G', $mixed), date('i', $mixed), date('s', $mixed));
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200757 }
Andrey Andreev30da39b2012-03-10 15:49:17 +0200758 else
759 {
760 $arg = (int) $mixed;
761 }
762 $range[] = $from->format($format);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200763
Andrey Andreev30da39b2012-03-10 15:49:17 +0200764 if (is_int($arg)) // Day intervals
765 {
766 do
767 {
768 $from->modify('+1 day');
769 $range[] = $from->format($format);
770 }
771 while (--$arg > 0);
772 }
773 else // end date UNIX timestamp
774 {
775 for ($from->modify('+1 day'), $end_check = $arg->format('Ymd'); $from->format('Ymd') < $end_check; $from->modify('+1 day'))
776 {
777 $range[] = $from->format($format);
778 }
779
780 // Our loop only appended dates prior to our end date
781 $range[] = $arg->format($format);
782 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200783
784 return $range;
785 }
786}
787
Derek Allard2067d1a2008-11-13 22:59:24 +0000788/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000789/* Location: ./system/helpers/date_helper.php */