blob: 51b2b76db3e870e7e54885bf10c6d704a3745994 [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().
122 * Due to that, this function is DEPRECATED and should be removed
123 * in CodeIgniter 3.1+.
124 *
125 * Here are two examples of how you should replace it:
126 *
127 * date(DATE_RFC822, now()); // default
128 * date(DATE_W3C, $time); // a different format and time
129 *
130 * Reference: http://www.php.net/manual/en/class.datetime.php#datetime.constants.types
131 *
132 * @deprecated
Timothy Warren01b129a2012-04-27 11:36:50 -0400133 * @param string the chosen format
134 * @param int Unix timestamp
135 * @return string
136 */
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300137 function standard_date($fmt = 'DATE_RFC822', $time = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300139 if (empty($time))
140 {
141 $time = now();
142 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000143
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300144 // Procedural style pre-defined constants from the DateTime extension
145 if (strpos($fmt, 'DATE_') !== 0 OR defined($fmt) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 return FALSE;
148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300150 return date(constant($fmt), $time);
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152}
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154// ------------------------------------------------------------------------
155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156if ( ! function_exists('timespan'))
157{
Timothy Warren01b129a2012-04-27 11:36:50 -0400158 /**
159 * Timespan
160 *
161 * Returns a span of seconds in this format:
162 * 10 days 14 hours 36 minutes 47 seconds
163 *
164 * @param int a number of seconds
165 * @param int Unix timestamp
166 * @param int a number of display units
167 * @return string
168 */
Roger Herbert8d69aa12012-03-14 08:44:55 +0000169 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 $CI =& get_instance();
172 $CI->lang->load('date');
173
Andrey Andreeveef24062012-06-14 16:17:48 +0300174 is_numeric($seconds) OR $seconds = 1;
175 is_numeric($time) OR $time = time();
176 is_numeric($units) OR $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000177
Greg Akerf9168392011-08-29 19:29:05 -0500178 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200179
Roger Herbert04c146d2012-03-11 15:31:01 +0000180 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500181 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200184 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300185 $str[] = $years.' '.$CI->lang->line($years > 1 ? 'date_years' : 'date_year');
Barry Mienydd671972010-10-04 16:33:58 +0200186 }
187
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500188 $seconds -= $years * 31557600;
189 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200190
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000191 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
193 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200194 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300195 $str[] = $months.' '.$CI->lang->line($months > 1 ? 'date_months' : 'date_month');
Barry Mienydd671972010-10-04 16:33:58 +0200196 }
197
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500198 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200
201 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200202
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000203 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
205 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200206 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300207 $str[] = $weeks.' '.$CI->lang->line($weeks > 1 ? 'date_weeks' : 'date_week');
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200211 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000212
213 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200214
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000215 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
217 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200218 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300219 $str[] = $days.' '.$CI->lang->line($days > 1 ? 'date_days' : 'date_day');
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 $seconds -= $days * 86400;
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200226
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000227 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
229 if ($hours > 0)
230 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300231 $str[] = $hours.' '.$CI->lang->line($hours > 1 ? 'date_hours' : 'date_hour');
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 $seconds -= $hours * 3600;
235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200238
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000239 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200242 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300243 $str[] = $minutes.' '.$CI->lang->line($minutes > 1 ? 'date_minutes' : 'date_minute');
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 $seconds -= $minutes * 60;
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Roger Herbert597eb212012-03-14 09:06:17 +0000249 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300251 $str[] = $seconds.' '.$CI->lang->line($seconds > 1 ? 'date_seconds' : 'date_second');
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Roger Herbert04c146d2012-03-11 15:31:01 +0000254 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
256}
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258// ------------------------------------------------------------------------
259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260if ( ! function_exists('days_in_month'))
261{
Timothy Warren01b129a2012-04-27 11:36:50 -0400262 /**
263 * Number of days in a month
264 *
265 * Takes a month/year as input and returns the number of days
266 * for the given month/year. Takes leap years into consideration.
267 *
268 * @param int a numeric month
269 * @param int a numeric year
270 * @return int
271 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 function days_in_month($month = 0, $year = '')
273 {
274 if ($month < 1 OR $month > 12)
275 {
276 return 0;
277 }
Andrey Andreeveef24062012-06-14 16:17:48 +0300278 elseif ( ! is_numeric($year) OR strlen($year) !== 4)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 $year = date('Y');
281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Andrey Andreeveef24062012-06-14 16:17:48 +0300283 if ($year >= 1970)
284 {
285 return (int) date('t', mktime(12, 0, 0, $month, 1, $year));
286 }
287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 if ($month == 2)
289 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100290 if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 return 29;
293 }
294 }
295
296 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
297 return $days_in_month[$month - 1];
298 }
299}
Derek Jones36591092010-03-05 10:05:51 -0600300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301// ------------------------------------------------------------------------
302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303if ( ! function_exists('local_to_gmt'))
304{
Timothy Warren01b129a2012-04-27 11:36:50 -0400305 /**
306 * Converts a local Unix timestamp to GMT
307 *
308 * @param int Unix timestamp
309 * @return int
310 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 function local_to_gmt($time = '')
312 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100313 if ($time === '')
Greg Akerf9168392011-08-29 19:29:05 -0500314 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500316 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500317
Greg Akerf9168392011-08-29 19:29:05 -0500318 return mktime(
Andrey Andreevb089e152012-06-16 19:11:40 +0300319 gmdate('G', $time),
Andrey Andreevae31eb52012-05-17 14:54:15 +0300320 gmdate('i', $time),
321 gmdate('s', $time),
Andrey Andreevb089e152012-06-16 19:11:40 +0300322 gmdate('n', $time),
323 gmdate('j', $time),
Andrey Andreevae31eb52012-05-17 14:54:15 +0300324 gmdate('Y', $time)
Greg Akerf9168392011-08-29 19:29:05 -0500325 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327}
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329// ------------------------------------------------------------------------
330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331if ( ! function_exists('gmt_to_local'))
332{
Timothy Warren01b129a2012-04-27 11:36:50 -0400333 /**
334 * Converts GMT time to a localized value
335 *
336 * Takes a Unix timestamp (in GMT) as input, and returns
337 * at the local value based on the timezone and DST setting
338 * submitted
339 *
340 * @param int Unix timestamp
341 * @param string timezone
342 * @param bool whether DST is active
343 * @return int
344 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200346 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100347 if ($time === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
349 return now();
350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 $time += timezones($timezone) * 3600;
353
Andrey Andreeveef24062012-06-14 16:17:48 +0300354 return ($dst === TRUE) ? $time + 3600 : $time;
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
356}
Derek Jones36591092010-03-05 10:05:51 -0600357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358// ------------------------------------------------------------------------
359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360if ( ! function_exists('mysql_to_unix'))
361{
Timothy Warren01b129a2012-04-27 11:36:50 -0400362 /**
363 * Converts a MySQL Timestamp to Unix
364 *
365 * @param int Unix timestamp
366 * @return int
367 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 function mysql_to_unix($time = '')
369 {
370 // We'll remove certain characters for backward compatibility
371 // since the formatting changed with MySQL 4.1
372 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200373
Andrey Andreev3bbbd262012-06-14 13:35:32 +0300374 $time = str_replace(array('-', ':', ' '), '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500377 return mktime(
378 substr($time, 8, 2),
379 substr($time, 10, 2),
380 substr($time, 12, 2),
381 substr($time, 4, 2),
382 substr($time, 6, 2),
383 substr($time, 0, 4)
384 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
386}
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388// ------------------------------------------------------------------------
389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390if ( ! function_exists('unix_to_human'))
391{
Timothy Warren01b129a2012-04-27 11:36:50 -0400392 /**
393 * Unix to "Human"
394 *
395 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
396 *
397 * @param int Unix timestamp
398 * @param bool whether to show seconds
399 * @param string format: us or euro
400 * @return string
401 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
403 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300404 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200405
Alex Bilbie773ccc32012-06-02 11:11:08 +0100406 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
408 $r .= date('h', $time).':'.date('i', $time);
409 }
410 else
411 {
412 $r .= date('H', $time).':'.date('i', $time);
413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 if ($seconds)
416 {
417 $r .= ':'.date('s', $time);
418 }
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 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300422 return $r.' '.date('A', $time);
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 return $r;
426 }
427}
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429// ------------------------------------------------------------------------
430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431if ( ! function_exists('human_to_unix'))
432{
Timothy Warren01b129a2012-04-27 11:36:50 -0400433 /**
434 * Convert "human" date to GMT
435 *
436 * Reverses the above process
437 *
438 * @param string format: us or euro
439 * @return int
440 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 function human_to_unix($datestr = '')
442 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100443 if ($datestr === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
445 return FALSE;
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Andrey Andreevae31eb52012-05-17 14:54:15 +0300448 $datestr = preg_replace('/\040+/', ' ', trim($datestr));
Barry Mienydd671972010-10-04 16:33:58 +0200449
Andrey Andreevf11a1932012-06-14 16:35:09 +0300450 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 +0000451 {
452 return FALSE;
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Jones36591092010-03-05 10:05:51 -0600455 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000456
Andrey Andreevf11a1932012-06-14 16:35:09 +0300457 list($year, $month, $day) = explode('-', $split[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000458
Andrey Andreevae31eb52012-05-17 14:54:15 +0300459 $ex = explode(':', $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200460
Andrey Andreevf11a1932012-06-14 16:35:09 +0300461 $hour = (int) $ex[0];
462 $min = (int) $ex[1];
463 $sec = ( ! empty($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2]))
464 ? (int) $ex[2] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000465
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300466 if (isset($split[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300468 $ampm = strtolower($split[2]);
Barry Mienydd671972010-10-04 16:33:58 +0200469
Andrey Andreevf11a1932012-06-14 16:35:09 +0300470 if ($ampm[0] === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500471 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300472 $hour += 12;
Greg Akerf9168392011-08-29 19:29:05 -0500473 }
Andrey Andreevf11a1932012-06-14 16:35:09 +0300474 elseif ($ampm[0] === 'a' && $hour === 12)
Greg Akerf9168392011-08-29 19:29:05 -0500475 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300476 $hour = 0;
Greg Akerf9168392011-08-29 19:29:05 -0500477 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 return mktime($hour, $min, $sec, $month, $day, $year);
481 }
482}
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484// ------------------------------------------------------------------------
485
Kyle Farris896d95a2011-08-21 23:03:54 -0300486if ( ! function_exists('nice_date'))
487{
Timothy Warren01b129a2012-04-27 11:36:50 -0400488 /**
489 * Turns many "reasonably-date-like" strings into something
490 * that is actually useful. This only works for dates after unix epoch.
491 *
492 * @param string The terribly formatted date-like string
493 * @param string Date format to return (same as php date function)
494 * @return string
495 */
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500496 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300497 {
498 if (empty($bad_date))
499 {
500 return 'Unknown';
501 }
Andrey Andreevd9b44be2012-06-15 16:07:08 +0300502 elseif (empty($format))
503 {
504 $format = 'U';
505 }
Greg Akerf9168392011-08-29 19:29:05 -0500506
Kyle Farris896d95a2011-08-21 23:03:54 -0300507 // Date like: YYYYMM
Andrey Andreevf11a1932012-06-14 16:35:09 +0300508 if (preg_match('/^\d{6}$/i', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300509 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300510 if (in_array(substr($bad_date, 0, 2), array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300511 {
512 $year = substr($bad_date, 0, 4);
513 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500514 }
515 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300516 {
517 $month = substr($bad_date, 0, 2);
518 $year = substr($bad_date, 2, 4);
519 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500520
Andrey Andreevae31eb52012-05-17 14:54:15 +0300521 return date($format, strtotime($year.'-'.$month.'-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300522 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500523
Kyle Farris896d95a2011-08-21 23:03:54 -0300524 // Date Like: YYYYMMDD
Andrey Andreevf11a1932012-06-14 16:35:09 +0300525 if (preg_match('/^(\d{2})\d{2}(\d{4})$/i', $bad_date, $matches))
Kyle Farris896d95a2011-08-21 23:03:54 -0300526 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300527 return date($format, strtotime($matches[1].'/01/'.$matches[2]));
Kyle Farris896d95a2011-08-21 23:03:54 -0300528 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500529
Kyle Farris896d95a2011-08-21 23:03:54 -0300530 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
Andrey Andreevf11a1932012-06-14 16:35:09 +0300531 if (preg_match('/^(\d{1,2})-(\d{1,2})-(\d{4})$/i', $bad_date, $matches))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500532 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300533 return date($format, strtotime($matches[3].'-'.$matches[1].'-'.$matches[2]));
Kyle Farris896d95a2011-08-21 23:03:54 -0300534 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500535
Kyle Farris896d95a2011-08-21 23:03:54 -0300536 // Any other kind of string, when converted into UNIX time,
537 // produces "0 seconds after epoc..." is probably bad...
538 // return "Invalid Date".
Alex Bilbie773ccc32012-06-02 11:11:08 +0100539 if (date('U', strtotime($bad_date)) === '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500540 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300541 return 'Invalid Date';
Kyle Farris896d95a2011-08-21 23:03:54 -0300542 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500543
Kyle Farris896d95a2011-08-21 23:03:54 -0300544 // It's probably a valid-ish date format already
545 return date($format, strtotime($bad_date));
546 }
547}
548
549// ------------------------------------------------------------------------
550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551if ( ! function_exists('timezone_menu'))
552{
Timothy Warren01b129a2012-04-27 11:36:50 -0400553 /**
554 * Timezone Menu
555 *
556 * Generates a drop-down menu of timezones.
557 *
558 * @param string timezone
559 * @param string classname
560 * @param string menu name
Mat Whitney7540ded2012-06-22 12:02:10 -0700561 * @param mixed attributes
Timothy Warren01b129a2012-04-27 11:36:50 -0400562 * @return string
563 */
Mat Whitney7540ded2012-06-22 12:02:10 -0700564 function timezone_menu($default = 'UTC', $class = '', $name = 'timezones', $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 {
566 $CI =& get_instance();
567 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200568
Alex Bilbie773ccc32012-06-02 11:11:08 +0100569 $default = ($default === 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000570
571 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200572
Alex Bilbie773ccc32012-06-02 11:11:08 +0100573 if ($class !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
575 $menu .= ' class="'.$class.'"';
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Eric Barnesacedd2b2012-07-29 00:15:40 -0400578 $menu .= _stringify_attributes($attributes).">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 foreach (timezones() as $key => $val)
581 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100582 $selected = ($default === $key) ? ' selected="selected"' : '';
Andrey Andreevae31eb52012-05-17 14:54:15 +0300583 $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 }
585
Andrey Andreevae31eb52012-05-17 14:54:15 +0300586 return $menu.'</select>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 }
588}
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590// ------------------------------------------------------------------------
591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592if ( ! function_exists('timezones'))
593{
Timothy Warren01b129a2012-04-27 11:36:50 -0400594 /**
595 * Timezones
596 *
597 * Returns an array of timezones. This is a helper function
598 * for various other ones in this library
599 *
600 * @param string timezone
601 * @return string
602 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 function timezones($tz = '')
604 {
605 // Note: Don't change the order of these even though
606 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200607
608 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500609 'UM12' => -12,
610 'UM11' => -11,
611 'UM10' => -10,
612 'UM95' => -9.5,
613 'UM9' => -9,
614 'UM8' => -8,
615 'UM7' => -7,
616 'UM6' => -6,
617 'UM5' => -5,
618 'UM45' => -4.5,
619 'UM4' => -4,
620 'UM35' => -3.5,
621 'UM3' => -3,
622 'UM2' => -2,
623 'UM1' => -1,
624 'UTC' => 0,
625 'UP1' => +1,
626 'UP2' => +2,
627 'UP3' => +3,
628 'UP35' => +3.5,
629 'UP4' => +4,
630 'UP45' => +4.5,
631 'UP5' => +5,
632 'UP55' => +5.5,
633 'UP575' => +5.75,
634 'UP6' => +6,
635 'UP65' => +6.5,
636 'UP7' => +7,
637 'UP8' => +8,
638 'UP875' => +8.75,
639 'UP9' => +9,
640 'UP95' => +9.5,
641 'UP10' => +10,
642 'UP105' => +10.5,
643 'UP11' => +11,
644 'UP115' => +11.5,
645 'UP12' => +12,
646 'UP1275' => +12.75,
647 'UP13' => +13,
648 'UP14' => +14
649 );
Barry Mienydd671972010-10-04 16:33:58 +0200650
Alex Bilbie773ccc32012-06-02 11:11:08 +0100651 if ($tz === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 {
653 return $zones;
654 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500655
Andrey Andreeve92df332012-03-26 22:44:20 +0300656 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 }
658}
659
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200660// ------------------------------------------------------------------------
661
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200662if ( ! function_exists('date_range'))
663{
Andrey Andreev14aa3172012-05-02 13:27:30 +0300664 /**
665 * Date range
666 *
667 * Returns a list of dates within a specified period.
668 *
669 * @param int unix_start UNIX timestamp of period start date
670 * @param int unix_end|days UNIX timestamp of period end date
671 * or interval in days.
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300672 * @param mixed is_unix Specifies whether the second parameter
Andrey Andreev14aa3172012-05-02 13:27:30 +0300673 * is a UNIX timestamp or a day interval
674 * - TRUE or 'unix' for a timestamp
675 * - FALSE or 'days' for an interval
676 * @param string date_format Output date format, same as in date()
677 * @return array
678 */
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200679 function date_range($unix_start = '', $mixed = '', $is_unix = TRUE, $format = 'Y-m-d')
680 {
681 if ($unix_start == '' OR $mixed == '' OR $format == '')
682 {
683 return FALSE;
684 }
685
686 $is_unix = ! ( ! $is_unix OR $is_unix === 'days');
687
688 // Validate input and try strtotime() on invalid timestamps/intervals, just in case
689 if ( ( ! preg_match('/^[0-9]+$/', $unix_start) && ($unix_start = @strtotime($unix_time)) === FALSE)
690 OR ( ! preg_match('/^[0-9]+$/', $mixed) && ($is_unix === FALSE OR ($mixed = @strtotime($mixed)) === FALSE))
691 OR ($is_unix === TRUE && $mixed < $unix_start))
692 {
693 return FALSE;
694 }
695
696 if ($is_unix && ($unix_start == $mixed OR date($format, $unix_start) === date($format, $mixed)))
697 {
698 return array($start_date);
699 }
700
701 $range = array();
702
Andrey Andreev30da39b2012-03-10 15:49:17 +0200703 /* NOTE: Even though the DateTime object has many useful features, it appears that
704 * it doesn't always handle properly timezones, when timestamps are passed
705 * directly to its constructor. Neither of the following gave proper results:
706 *
707 * new DateTime('<timestamp>')
708 * new DateTime('<timestamp>', '<timezone>')
709 *
710 * --- available in PHP 5.3:
711 *
712 * DateTime::createFromFormat('<format>', '<timestamp>')
713 * DateTime::createFromFormat('<format>', '<timestamp>', '<timezone')
714 *
715 * ... so we'll have to set the timestamp after the object is instantiated.
716 * Furthermore, in PHP 5.3 we can use DateTime::setTimestamp() to do that and
717 * given that we have UNIX timestamps - we should use it.
718 */
719 $from = new DateTime();
720
721 if (is_php('5.3'))
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200722 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200723 $from->setTimestamp($unix_start);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200724 if ($is_unix)
725 {
726 $arg = new DateTime();
Andrey Andreev30da39b2012-03-10 15:49:17 +0200727 $arg->setTimestamp($mixed);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200728 }
729 else
730 {
731 $arg = (int) $mixed;
732 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200733
Andrey Andreev30da39b2012-03-10 15:49:17 +0200734 $period = new DatePeriod($from, new DateInterval('P1D'), $arg);
735 foreach ($period as $date)
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200736 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200737 $range[] = $date->format($format);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200738 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200739
Andrey Andreev30da39b2012-03-10 15:49:17 +0200740 /* If a period end date was passed to the DatePeriod constructor, it might not
741 * be in our results. Not sure if this is a bug or it's just possible because
742 * the end date might actually be less than 24 hours away from the previously
743 * generated DateTime object, but either way - we have to append it manually.
744 */
745 if ( ! is_int($arg) && $range[count($range) - 1] !== $arg->format($format))
746 {
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200747 $range[] = $arg->format($format);
748 }
749
750 return $range;
751 }
752
Andrey Andreev30da39b2012-03-10 15:49:17 +0200753 $from->setDate(date('Y', $unix_start), date('n', $unix_start), date('j', $unix_start));
754 $from->setTime(date('G', $unix_start), date('i', $unix_start), date('s', $unix_start));
755 if ($is_unix)
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200756 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200757 $arg = new DateTime();
758 $arg->setDate(date('Y', $mixed), date('n', $mixed), date('j', $mixed));
759 $arg->setTime(date('G', $mixed), date('i', $mixed), date('s', $mixed));
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200760 }
Andrey Andreev30da39b2012-03-10 15:49:17 +0200761 else
762 {
763 $arg = (int) $mixed;
764 }
765 $range[] = $from->format($format);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200766
Andrey Andreev30da39b2012-03-10 15:49:17 +0200767 if (is_int($arg)) // Day intervals
768 {
769 do
770 {
771 $from->modify('+1 day');
772 $range[] = $from->format($format);
773 }
774 while (--$arg > 0);
775 }
776 else // end date UNIX timestamp
777 {
778 for ($from->modify('+1 day'), $end_check = $arg->format('Ymd'); $from->format('Ymd') < $end_check; $from->modify('+1 day'))
779 {
780 $range[] = $from->format($format);
781 }
782
783 // Our loop only appended dates prior to our end date
784 $range[] = $arg->format($format);
785 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200786
787 return $range;
788 }
789}
790
Derek Allard2067d1a2008-11-13 22:59:24 +0000791/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000792/* Location: ./system/helpers/date_helper.php */