blob: cafb6ba955fc752fb9e34a120a052068105ffb4d [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 Andreevb089e152012-06-16 19:11:40 +0300307 return mktime(
308 gmdate('G', $time),
309 gmdate('i', $time),
310 gmdate('s', $time),
311 gmdate('n', $time),
312 gmdate('j', $time),
313 gmdate('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
Andrey Andreevf11a1932012-06-14 16:35:09 +0300439 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 +0000440 {
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 Andreevf11a1932012-06-14 16:35:09 +0300446 list($year, $month, $day) = explode('-', $split[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000447
Andrey Andreevae31eb52012-05-17 14:54:15 +0300448 $ex = explode(':', $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200449
Andrey Andreevf11a1932012-06-14 16:35:09 +0300450 $hour = (int) $ex[0];
451 $min = (int) $ex[1];
452 $sec = ( ! empty($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2]))
453 ? (int) $ex[2] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200454
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300455 if (isset($split[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300457 $ampm = strtolower($split[2]);
Barry Mienydd671972010-10-04 16:33:58 +0200458
Andrey Andreevf11a1932012-06-14 16:35:09 +0300459 if ($ampm[0] === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500460 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300461 $hour += 12;
Greg Akerf9168392011-08-29 19:29:05 -0500462 }
Andrey Andreevf11a1932012-06-14 16:35:09 +0300463 elseif ($ampm[0] === 'a' && $hour === 12)
Greg Akerf9168392011-08-29 19:29:05 -0500464 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300465 $hour = 0;
Greg Akerf9168392011-08-29 19:29:05 -0500466 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 return mktime($hour, $min, $sec, $month, $day, $year);
470 }
471}
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473// ------------------------------------------------------------------------
474
Kyle Farris896d95a2011-08-21 23:03:54 -0300475if ( ! function_exists('nice_date'))
476{
Timothy Warren01b129a2012-04-27 11:36:50 -0400477 /**
478 * Turns many "reasonably-date-like" strings into something
479 * that is actually useful. This only works for dates after unix epoch.
480 *
481 * @param string The terribly formatted date-like string
482 * @param string Date format to return (same as php date function)
483 * @return string
484 */
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500485 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300486 {
487 if (empty($bad_date))
488 {
489 return 'Unknown';
490 }
Andrey Andreevd9b44be2012-06-15 16:07:08 +0300491 elseif (empty($format))
492 {
493 $format = 'U';
494 }
Greg Akerf9168392011-08-29 19:29:05 -0500495
Kyle Farris896d95a2011-08-21 23:03:54 -0300496 // Date like: YYYYMM
Andrey Andreevf11a1932012-06-14 16:35:09 +0300497 if (preg_match('/^\d{6}$/i', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300498 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300499 if (in_array(substr($bad_date, 0, 2), array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300500 {
501 $year = substr($bad_date, 0, 4);
502 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500503 }
504 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300505 {
506 $month = substr($bad_date, 0, 2);
507 $year = substr($bad_date, 2, 4);
508 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500509
Andrey Andreevae31eb52012-05-17 14:54:15 +0300510 return date($format, strtotime($year.'-'.$month.'-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300511 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500512
Kyle Farris896d95a2011-08-21 23:03:54 -0300513 // Date Like: YYYYMMDD
Andrey Andreevf11a1932012-06-14 16:35:09 +0300514 if (preg_match('/^(\d{2})\d{2}(\d{4})$/i', $bad_date, $matches))
Kyle Farris896d95a2011-08-21 23:03:54 -0300515 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300516 return date($format, strtotime($matches[1].'/01/'.$matches[2]));
Kyle Farris896d95a2011-08-21 23:03:54 -0300517 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500518
Kyle Farris896d95a2011-08-21 23:03:54 -0300519 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
Andrey Andreevf11a1932012-06-14 16:35:09 +0300520 if (preg_match('/^(\d{1,2})-(\d{1,2})-(\d{4})$/i', $bad_date, $matches))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500521 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300522 return date($format, strtotime($matches[3].'-'.$matches[1].'-'.$matches[2]));
Kyle Farris896d95a2011-08-21 23:03:54 -0300523 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500524
Kyle Farris896d95a2011-08-21 23:03:54 -0300525 // Any other kind of string, when converted into UNIX time,
526 // produces "0 seconds after epoc..." is probably bad...
527 // return "Invalid Date".
Alex Bilbie773ccc32012-06-02 11:11:08 +0100528 if (date('U', strtotime($bad_date)) === '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500529 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300530 return 'Invalid Date';
Kyle Farris896d95a2011-08-21 23:03:54 -0300531 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500532
Kyle Farris896d95a2011-08-21 23:03:54 -0300533 // It's probably a valid-ish date format already
534 return date($format, strtotime($bad_date));
535 }
536}
537
538// ------------------------------------------------------------------------
539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540if ( ! function_exists('timezone_menu'))
541{
Timothy Warren01b129a2012-04-27 11:36:50 -0400542 /**
543 * Timezone Menu
544 *
545 * Generates a drop-down menu of timezones.
546 *
547 * @param string timezone
548 * @param string classname
549 * @param string menu name
550 * @return string
551 */
Andrey Andreevae31eb52012-05-17 14:54:15 +0300552 function timezone_menu($default = 'UTC', $class = '', $name = 'timezones')
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
554 $CI =& get_instance();
555 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200556
Alex Bilbie773ccc32012-06-02 11:11:08 +0100557 $default = ($default === 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000558
559 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200560
Alex Bilbie773ccc32012-06-02 11:11:08 +0100561 if ($class !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
563 $menu .= ' class="'.$class.'"';
564 }
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 foreach (timezones() as $key => $val)
569 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100570 $selected = ($default === $key) ? ' selected="selected"' : '';
Andrey Andreevae31eb52012-05-17 14:54:15 +0300571 $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 }
573
Andrey Andreevae31eb52012-05-17 14:54:15 +0300574 return $menu.'</select>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
576}
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578// ------------------------------------------------------------------------
579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580if ( ! function_exists('timezones'))
581{
Timothy Warren01b129a2012-04-27 11:36:50 -0400582 /**
583 * Timezones
584 *
585 * Returns an array of timezones. This is a helper function
586 * for various other ones in this library
587 *
588 * @param string timezone
589 * @return string
590 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 function timezones($tz = '')
592 {
593 // Note: Don't change the order of these even though
594 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200595
596 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500597 'UM12' => -12,
598 'UM11' => -11,
599 'UM10' => -10,
600 'UM95' => -9.5,
601 'UM9' => -9,
602 'UM8' => -8,
603 'UM7' => -7,
604 'UM6' => -6,
605 'UM5' => -5,
606 'UM45' => -4.5,
607 'UM4' => -4,
608 'UM35' => -3.5,
609 'UM3' => -3,
610 'UM2' => -2,
611 'UM1' => -1,
612 'UTC' => 0,
613 'UP1' => +1,
614 'UP2' => +2,
615 'UP3' => +3,
616 'UP35' => +3.5,
617 'UP4' => +4,
618 'UP45' => +4.5,
619 'UP5' => +5,
620 'UP55' => +5.5,
621 'UP575' => +5.75,
622 'UP6' => +6,
623 'UP65' => +6.5,
624 'UP7' => +7,
625 'UP8' => +8,
626 'UP875' => +8.75,
627 'UP9' => +9,
628 'UP95' => +9.5,
629 'UP10' => +10,
630 'UP105' => +10.5,
631 'UP11' => +11,
632 'UP115' => +11.5,
633 'UP12' => +12,
634 'UP1275' => +12.75,
635 'UP13' => +13,
636 'UP14' => +14
637 );
Barry Mienydd671972010-10-04 16:33:58 +0200638
Alex Bilbie773ccc32012-06-02 11:11:08 +0100639 if ($tz === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
641 return $zones;
642 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500643
Andrey Andreeve92df332012-03-26 22:44:20 +0300644 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 }
646}
647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000649/* Location: ./system/helpers/date_helper.php */