blob: ae0b7a2b7a1522a6760b2843a95d080000753dea [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))
Iban Eguia16760bb2012-03-27 18:51:52 +020054 {
Iban Eguiafeb14da2012-06-12 16:09:36 +020055 $timezone = config_item('time_reference');
Iban Eguia16760bb2012-03-27 18:51:52 +020056 }
Barry Mienydd671972010-10-04 16:33:58 +020057
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 }
Barry Mienydd671972010-10-04 16:33:58 +020062
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 *
120 * @param string the chosen format
121 * @param int Unix timestamp
122 * @return string
123 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 function standard_date($fmt = 'DATE_RFC822', $time = '')
125 {
126 $formats = array(
Andrey Andreeveef24062012-06-14 16:17:48 +0300127 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O',
128 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
129 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
130 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
131 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC',
132 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
133 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
134 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
135 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
136 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O'
137 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000138
Andrey Andreeveef24062012-06-14 16:17:48 +0300139 return isset($formats[$fmt]) ? mdate($formats[$fmt], $time) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
141}
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143// ------------------------------------------------------------------------
144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145if ( ! function_exists('timespan'))
146{
Timothy Warren01b129a2012-04-27 11:36:50 -0400147 /**
148 * Timespan
149 *
150 * Returns a span of seconds in this format:
151 * 10 days 14 hours 36 minutes 47 seconds
152 *
153 * @param int a number of seconds
154 * @param int Unix timestamp
155 * @param int a number of display units
156 * @return string
157 */
Roger Herbert8d69aa12012-03-14 08:44:55 +0000158 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
160 $CI =& get_instance();
161 $CI->lang->load('date');
162
Andrey Andreeveef24062012-06-14 16:17:48 +0300163 is_numeric($seconds) OR $seconds = 1;
164 is_numeric($time) OR $time = time();
165 is_numeric($units) OR $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000166
Greg Akerf9168392011-08-29 19:29:05 -0500167 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200168
Roger Herbert04c146d2012-03-11 15:31:01 +0000169 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500170 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200173 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300174 $str[] = $years.' '.$CI->lang->line($years > 1 ? 'date_years' : 'date_year');
Barry Mienydd671972010-10-04 16:33:58 +0200175 }
176
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500177 $seconds -= $years * 31557600;
178 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200179
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000180 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
182 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200183 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300184 $str[] = $months.' '.$CI->lang->line($months > 1 ? 'date_months' : 'date_month');
Barry Mienydd671972010-10-04 16:33:58 +0200185 }
186
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500187 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
189
190 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200191
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000192 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
194 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200195 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300196 $str[] = $weeks.' '.$CI->lang->line($weeks > 1 ? 'date_weeks' : 'date_week');
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200200 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000201
202 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200203
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000204 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200207 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300208 $str[] = $days.' '.$CI->lang->line($days > 1 ? 'date_days' : 'date_day');
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 $seconds -= $days * 86400;
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200215
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000216 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
218 if ($hours > 0)
219 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300220 $str[] = $hours.' '.$CI->lang->line($hours > 1 ? 'date_hours' : 'date_hour');
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 $seconds -= $hours * 3600;
224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200227
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000228 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
230 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200231 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300232 $str[] = $minutes.' '.$CI->lang->line($minutes > 1 ? 'date_minutes' : 'date_minute');
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 $seconds -= $minutes * 60;
236 }
Barry Mienydd671972010-10-04 16:33:58 +0200237
Roger Herbert597eb212012-03-14 09:06:17 +0000238 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300240 $str[] = $seconds.' '.$CI->lang->line($seconds > 1 ? 'date_seconds' : 'date_second');
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Roger Herbert04c146d2012-03-11 15:31:01 +0000243 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
245}
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247// ------------------------------------------------------------------------
248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249if ( ! function_exists('days_in_month'))
250{
Timothy Warren01b129a2012-04-27 11:36:50 -0400251 /**
252 * Number of days in a month
253 *
254 * Takes a month/year as input and returns the number of days
255 * for the given month/year. Takes leap years into consideration.
256 *
257 * @param int a numeric month
258 * @param int a numeric year
259 * @return int
260 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 function days_in_month($month = 0, $year = '')
262 {
263 if ($month < 1 OR $month > 12)
264 {
265 return 0;
266 }
Andrey Andreeveef24062012-06-14 16:17:48 +0300267 elseif ( ! is_numeric($year) OR strlen($year) !== 4)
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
269 $year = date('Y');
270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Andrey Andreeveef24062012-06-14 16:17:48 +0300272 if ($year >= 1970)
273 {
274 return (int) date('t', mktime(12, 0, 0, $month, 1, $year));
275 }
276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 if ($month == 2)
278 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100279 if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 return 29;
282 }
283 }
284
285 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
286 return $days_in_month[$month - 1];
287 }
288}
Derek Jones36591092010-03-05 10:05:51 -0600289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290// ------------------------------------------------------------------------
291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292if ( ! function_exists('local_to_gmt'))
293{
Timothy Warren01b129a2012-04-27 11:36:50 -0400294 /**
295 * Converts a local Unix timestamp to GMT
296 *
297 * @param int Unix timestamp
298 * @return int
299 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 function local_to_gmt($time = '')
301 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100302 if ($time === '')
Greg Akerf9168392011-08-29 19:29:05 -0500303 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500305 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500306
Andrey Andreev3bbbd262012-06-14 13:35:32 +0300307 return gmmktime(
Andrey Andreeveef24062012-06-14 16:17:48 +0300308 date('G', $time),
Andrey Andreev3bbbd262012-06-14 13:35:32 +0300309 date('i', $time),
310 date('s', $time),
Andrey Andreeveef24062012-06-14 16:17:48 +0300311 date('n', $time),
312 date('j', $time),
Andrey Andreev3bbbd262012-06-14 13:35:32 +0300313 date('Y', $time)
Greg Akerf9168392011-08-29 19:29:05 -0500314 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
316}
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318// ------------------------------------------------------------------------
319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320if ( ! function_exists('gmt_to_local'))
321{
Timothy Warren01b129a2012-04-27 11:36:50 -0400322 /**
323 * Converts GMT time to a localized value
324 *
325 * Takes a Unix timestamp (in GMT) as input, and returns
326 * at the local value based on the timezone and DST setting
327 * submitted
328 *
329 * @param int Unix timestamp
330 * @param string timezone
331 * @param bool whether DST is active
332 * @return int
333 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200335 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100336 if ($time === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 return now();
339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 $time += timezones($timezone) * 3600;
342
Andrey Andreeveef24062012-06-14 16:17:48 +0300343 return ($dst === TRUE) ? $time + 3600 : $time;
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
345}
Derek Jones36591092010-03-05 10:05:51 -0600346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347// ------------------------------------------------------------------------
348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349if ( ! function_exists('mysql_to_unix'))
350{
Timothy Warren01b129a2012-04-27 11:36:50 -0400351 /**
352 * Converts a MySQL Timestamp to Unix
353 *
354 * @param int Unix timestamp
355 * @return int
356 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 function mysql_to_unix($time = '')
358 {
359 // We'll remove certain characters for backward compatibility
360 // since the formatting changed with MySQL 4.1
361 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200362
Andrey Andreev3bbbd262012-06-14 13:35:32 +0300363 $time = str_replace(array('-', ':', ' '), '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500366 return mktime(
367 substr($time, 8, 2),
368 substr($time, 10, 2),
369 substr($time, 12, 2),
370 substr($time, 4, 2),
371 substr($time, 6, 2),
372 substr($time, 0, 4)
373 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 }
375}
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377// ------------------------------------------------------------------------
378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379if ( ! function_exists('unix_to_human'))
380{
Timothy Warren01b129a2012-04-27 11:36:50 -0400381 /**
382 * Unix to "Human"
383 *
384 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
385 *
386 * @param int Unix timestamp
387 * @param bool whether to show seconds
388 * @param string format: us or euro
389 * @return string
390 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
392 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300393 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200394
Alex Bilbie773ccc32012-06-02 11:11:08 +0100395 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
397 $r .= date('h', $time).':'.date('i', $time);
398 }
399 else
400 {
401 $r .= date('H', $time).':'.date('i', $time);
402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 if ($seconds)
405 {
406 $r .= ':'.date('s', $time);
407 }
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 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300411 return $r.' '.date('A', $time);
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 }
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 return $r;
415 }
416}
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418// ------------------------------------------------------------------------
419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420if ( ! function_exists('human_to_unix'))
421{
Timothy Warren01b129a2012-04-27 11:36:50 -0400422 /**
423 * Convert "human" date to GMT
424 *
425 * Reverses the above process
426 *
427 * @param string format: us or euro
428 * @return int
429 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 function human_to_unix($datestr = '')
431 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100432 if ($datestr === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 return FALSE;
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Andrey Andreevae31eb52012-05-17 14:54:15 +0300437 $datestr = preg_replace('/\040+/', ' ', trim($datestr));
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 if ( ! preg_match('/^[0-9]{2,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))
440 {
441 return FALSE;
442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Jones36591092010-03-05 10:05:51 -0600444 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000445
Andrey Andreevae31eb52012-05-17 14:54:15 +0300446 $ex = explode('-', $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200447
Andrey Andreevae31eb52012-05-17 14:54:15 +0300448 $year = (strlen($ex[0]) === 2) ? '20'.$ex[0] : $ex[0];
449 $month = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
450 $day = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000451
Andrey Andreevae31eb52012-05-17 14:54:15 +0300452 $ex = explode(':', $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200453
Andrey Andreevae31eb52012-05-17 14:54:15 +0300454 $hour = (strlen($ex[0]) === 1) ? '0'.$ex[0] : $ex[0];
455 $min = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000456
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300457 if (isset($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300459 $sec = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
461 else
462 {
463 // Unless specified, seconds get set to zero.
464 $sec = '00';
465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300467 if (isset($split[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300469 $ampm = strtolower($split[2]);
Barry Mienydd671972010-10-04 16:33:58 +0200470
Andrey Andreeve92df332012-03-26 22:44:20 +0300471 if (substr($ampm, 0, 1) === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500472 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300473 $hour += 12;
Greg Akerf9168392011-08-29 19:29:05 -0500474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Andrey Andreeve92df332012-03-26 22:44:20 +0300476 if (substr($ampm, 0, 1) === 'a' && $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500477 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500478 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500479 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500480
Andrey Andreevae31eb52012-05-17 14:54:15 +0300481 if (strlen($hour) === 1)
Greg Akerf9168392011-08-29 19:29:05 -0500482 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500483 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500484 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 return mktime($hour, $min, $sec, $month, $day, $year);
488 }
489}
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491// ------------------------------------------------------------------------
492
Kyle Farris896d95a2011-08-21 23:03:54 -0300493if ( ! function_exists('nice_date'))
494{
Timothy Warren01b129a2012-04-27 11:36:50 -0400495 /**
496 * Turns many "reasonably-date-like" strings into something
497 * that is actually useful. This only works for dates after unix epoch.
498 *
499 * @param string The terribly formatted date-like string
500 * @param string Date format to return (same as php date function)
501 * @return string
502 */
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500503 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300504 {
505 if (empty($bad_date))
506 {
507 return 'Unknown';
508 }
Greg Akerf9168392011-08-29 19:29:05 -0500509
Kyle Farris896d95a2011-08-21 23:03:54 -0300510 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500511 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300512 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300513 if (in_array(substr($bad_date, 0, 2), array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300514 {
515 $year = substr($bad_date, 0, 4);
516 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500517 }
518 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300519 {
520 $month = substr($bad_date, 0, 2);
521 $year = substr($bad_date, 2, 4);
522 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500523
Andrey Andreevae31eb52012-05-17 14:54:15 +0300524 return date($format, strtotime($year.'-'.$month.'-01'));
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: YYYYMMDD
Andrey Andreevae31eb52012-05-17 14:54:15 +0300528 if (preg_match('/^\d{8}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300529 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300530 $month = substr($bad_date, 0, 2);
531 $day = substr($bad_date, 2, 2);
532 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500533
Andrey Andreevae31eb52012-05-17 14:54:15 +0300534 return date($format, strtotime($month.'/01/'.$year));
Kyle Farris896d95a2011-08-21 23:03:54 -0300535 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500536
Kyle Farris896d95a2011-08-21 23:03:54 -0300537 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
Andrey Andreeveef24062012-06-14 16:17:48 +0300538 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/', $bad_date, $matches))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500539 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300540 list($m, $d, $y) = explode('-', $bad_date);
Andrey Andreevae31eb52012-05-17 14:54:15 +0300541 return date($format, strtotime($y.'-'.$m.'-'.$d));
Kyle Farris896d95a2011-08-21 23:03:54 -0300542 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500543
Kyle Farris896d95a2011-08-21 23:03:54 -0300544 // Any other kind of string, when converted into UNIX time,
545 // produces "0 seconds after epoc..." is probably bad...
546 // return "Invalid Date".
Alex Bilbie773ccc32012-06-02 11:11:08 +0100547 if (date('U', strtotime($bad_date)) === '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500548 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300549 return 'Invalid Date';
Kyle Farris896d95a2011-08-21 23:03:54 -0300550 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500551
Kyle Farris896d95a2011-08-21 23:03:54 -0300552 // It's probably a valid-ish date format already
553 return date($format, strtotime($bad_date));
554 }
555}
556
557// ------------------------------------------------------------------------
558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559if ( ! function_exists('timezone_menu'))
560{
Timothy Warren01b129a2012-04-27 11:36:50 -0400561 /**
562 * Timezone Menu
563 *
564 * Generates a drop-down menu of timezones.
565 *
566 * @param string timezone
567 * @param string classname
568 * @param string menu name
569 * @return string
570 */
Andrey Andreevae31eb52012-05-17 14:54:15 +0300571 function timezone_menu($default = 'UTC', $class = '', $name = 'timezones')
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
573 $CI =& get_instance();
574 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200575
Alex Bilbie773ccc32012-06-02 11:11:08 +0100576 $default = ($default === 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000577
578 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200579
Alex Bilbie773ccc32012-06-02 11:11:08 +0100580 if ($class !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
582 $menu .= ' class="'.$class.'"';
583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 foreach (timezones() as $key => $val)
588 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100589 $selected = ($default === $key) ? ' selected="selected"' : '';
Andrey Andreevae31eb52012-05-17 14:54:15 +0300590 $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 }
592
Andrey Andreevae31eb52012-05-17 14:54:15 +0300593 return $menu.'</select>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 }
595}
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597// ------------------------------------------------------------------------
598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599if ( ! function_exists('timezones'))
600{
Timothy Warren01b129a2012-04-27 11:36:50 -0400601 /**
602 * Timezones
603 *
604 * Returns an array of timezones. This is a helper function
605 * for various other ones in this library
606 *
607 * @param string timezone
608 * @return string
609 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 function timezones($tz = '')
611 {
612 // Note: Don't change the order of these even though
613 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200614
615 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500616 'UM12' => -12,
617 'UM11' => -11,
618 'UM10' => -10,
619 'UM95' => -9.5,
620 'UM9' => -9,
621 'UM8' => -8,
622 'UM7' => -7,
623 'UM6' => -6,
624 'UM5' => -5,
625 'UM45' => -4.5,
626 'UM4' => -4,
627 'UM35' => -3.5,
628 'UM3' => -3,
629 'UM2' => -2,
630 'UM1' => -1,
631 'UTC' => 0,
632 'UP1' => +1,
633 'UP2' => +2,
634 'UP3' => +3,
635 'UP35' => +3.5,
636 'UP4' => +4,
637 'UP45' => +4.5,
638 'UP5' => +5,
639 'UP55' => +5.5,
640 'UP575' => +5.75,
641 'UP6' => +6,
642 'UP65' => +6.5,
643 'UP7' => +7,
644 'UP8' => +8,
645 'UP875' => +8.75,
646 'UP9' => +9,
647 'UP95' => +9.5,
648 'UP10' => +10,
649 'UP105' => +10.5,
650 'UP11' => +11,
651 'UP115' => +11.5,
652 'UP12' => +12,
653 'UP1275' => +12.75,
654 'UP13' => +13,
655 'UP14' => +14
656 );
Barry Mienydd671972010-10-04 16:33:58 +0200657
Alex Bilbie773ccc32012-06-02 11:11:08 +0100658 if ($tz === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 {
660 return $zones;
661 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500662
Andrey Andreeve92df332012-03-26 22:44:20 +0300663 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
665}
666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000668/* Location: ./system/helpers/date_helper.php */