blob: 6686089c6aa5f72e65140e50ce989a92dde4580e [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 */
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300124 function standard_date($fmt = 'DATE_RFC822', $time = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300126 if (empty($time))
127 {
128 $time = now();
129 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000130
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300131 // Procedural style pre-defined constants from the DateTime extension
132 if (strpos($fmt, 'DATE_') !== 0 OR defined($fmt) === FALSE)
133 {
134 return FALSE;
135 }
136
137 return date(constant($fmt), $time);
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
139}
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141// ------------------------------------------------------------------------
142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143if ( ! function_exists('timespan'))
144{
Timothy Warren01b129a2012-04-27 11:36:50 -0400145 /**
146 * Timespan
147 *
148 * Returns a span of seconds in this format:
149 * 10 days 14 hours 36 minutes 47 seconds
150 *
151 * @param int a number of seconds
152 * @param int Unix timestamp
153 * @param int a number of display units
154 * @return string
155 */
Roger Herbert8d69aa12012-03-14 08:44:55 +0000156 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
158 $CI =& get_instance();
159 $CI->lang->load('date');
160
Andrey Andreeveef24062012-06-14 16:17:48 +0300161 is_numeric($seconds) OR $seconds = 1;
162 is_numeric($time) OR $time = time();
163 is_numeric($units) OR $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000164
Greg Akerf9168392011-08-29 19:29:05 -0500165 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200166
Roger Herbert04c146d2012-03-11 15:31:01 +0000167 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500168 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200171 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300172 $str[] = $years.' '.$CI->lang->line($years > 1 ? 'date_years' : 'date_year');
Barry Mienydd671972010-10-04 16:33:58 +0200173 }
174
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500175 $seconds -= $years * 31557600;
176 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200177
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000178 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
180 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200181 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300182 $str[] = $months.' '.$CI->lang->line($months > 1 ? 'date_months' : 'date_month');
Barry Mienydd671972010-10-04 16:33:58 +0200183 }
184
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500185 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
187
188 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200189
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000190 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
192 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200193 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300194 $str[] = $weeks.' '.$CI->lang->line($weeks > 1 ? 'date_weeks' : 'date_week');
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200198 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000199
200 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200201
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000202 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
204 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200205 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300206 $str[] = $days.' '.$CI->lang->line($days > 1 ? 'date_days' : 'date_day');
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 $seconds -= $days * 86400;
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200213
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000214 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
216 if ($hours > 0)
217 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300218 $str[] = $hours.' '.$CI->lang->line($hours > 1 ? 'date_hours' : 'date_hour');
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 $seconds -= $hours * 3600;
222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200225
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000226 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
228 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200229 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300230 $str[] = $minutes.' '.$CI->lang->line($minutes > 1 ? 'date_minutes' : 'date_minute');
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 $seconds -= $minutes * 60;
234 }
Barry Mienydd671972010-10-04 16:33:58 +0200235
Roger Herbert597eb212012-03-14 09:06:17 +0000236 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300238 $str[] = $seconds.' '.$CI->lang->line($seconds > 1 ? 'date_seconds' : 'date_second');
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Roger Herbert04c146d2012-03-11 15:31:01 +0000241 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243}
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245// ------------------------------------------------------------------------
246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247if ( ! function_exists('days_in_month'))
248{
Timothy Warren01b129a2012-04-27 11:36:50 -0400249 /**
250 * Number of days in a month
251 *
252 * Takes a month/year as input and returns the number of days
253 * for the given month/year. Takes leap years into consideration.
254 *
255 * @param int a numeric month
256 * @param int a numeric year
257 * @return int
258 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 function days_in_month($month = 0, $year = '')
260 {
261 if ($month < 1 OR $month > 12)
262 {
263 return 0;
264 }
Andrey Andreeveef24062012-06-14 16:17:48 +0300265 elseif ( ! is_numeric($year) OR strlen($year) !== 4)
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
267 $year = date('Y');
268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Andrey Andreeveef24062012-06-14 16:17:48 +0300270 if ($year >= 1970)
271 {
272 return (int) date('t', mktime(12, 0, 0, $month, 1, $year));
273 }
274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 if ($month == 2)
276 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100277 if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
279 return 29;
280 }
281 }
282
283 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
284 return $days_in_month[$month - 1];
285 }
286}
Derek Jones36591092010-03-05 10:05:51 -0600287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288// ------------------------------------------------------------------------
289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290if ( ! function_exists('local_to_gmt'))
291{
Timothy Warren01b129a2012-04-27 11:36:50 -0400292 /**
293 * Converts a local Unix timestamp to GMT
294 *
295 * @param int Unix timestamp
296 * @return int
297 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 function local_to_gmt($time = '')
299 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100300 if ($time === '')
Greg Akerf9168392011-08-29 19:29:05 -0500301 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500303 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500304
Andrey Andreevb089e152012-06-16 19:11:40 +0300305 return mktime(
306 gmdate('G', $time),
307 gmdate('i', $time),
308 gmdate('s', $time),
309 gmdate('n', $time),
310 gmdate('j', $time),
311 gmdate('Y', $time)
Greg Akerf9168392011-08-29 19:29:05 -0500312 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
314}
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316// ------------------------------------------------------------------------
317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318if ( ! function_exists('gmt_to_local'))
319{
Timothy Warren01b129a2012-04-27 11:36:50 -0400320 /**
321 * Converts GMT time to a localized value
322 *
323 * Takes a Unix timestamp (in GMT) as input, and returns
324 * at the local value based on the timezone and DST setting
325 * submitted
326 *
327 * @param int Unix timestamp
328 * @param string timezone
329 * @param bool whether DST is active
330 * @return int
331 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200333 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100334 if ($time === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 return now();
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 $time += timezones($timezone) * 3600;
340
Andrey Andreeveef24062012-06-14 16:17:48 +0300341 return ($dst === TRUE) ? $time + 3600 : $time;
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 }
343}
Derek Jones36591092010-03-05 10:05:51 -0600344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345// ------------------------------------------------------------------------
346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347if ( ! function_exists('mysql_to_unix'))
348{
Timothy Warren01b129a2012-04-27 11:36:50 -0400349 /**
350 * Converts a MySQL Timestamp to Unix
351 *
352 * @param int Unix timestamp
353 * @return int
354 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 function mysql_to_unix($time = '')
356 {
357 // We'll remove certain characters for backward compatibility
358 // since the formatting changed with MySQL 4.1
359 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200360
Andrey Andreev3bbbd262012-06-14 13:35:32 +0300361 $time = str_replace(array('-', ':', ' '), '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500364 return mktime(
365 substr($time, 8, 2),
366 substr($time, 10, 2),
367 substr($time, 12, 2),
368 substr($time, 4, 2),
369 substr($time, 6, 2),
370 substr($time, 0, 4)
371 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
373}
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375// ------------------------------------------------------------------------
376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377if ( ! function_exists('unix_to_human'))
378{
Timothy Warren01b129a2012-04-27 11:36:50 -0400379 /**
380 * Unix to "Human"
381 *
382 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
383 *
384 * @param int Unix timestamp
385 * @param bool whether to show seconds
386 * @param string format: us or euro
387 * @return string
388 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
390 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300391 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200392
Alex Bilbie773ccc32012-06-02 11:11:08 +0100393 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
395 $r .= date('h', $time).':'.date('i', $time);
396 }
397 else
398 {
399 $r .= date('H', $time).':'.date('i', $time);
400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 if ($seconds)
403 {
404 $r .= ':'.date('s', $time);
405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Alex Bilbie773ccc32012-06-02 11:11:08 +0100407 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Andrey Andreeveef24062012-06-14 16:17:48 +0300409 return $r.' '.date('A', $time);
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 return $r;
413 }
414}
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416// ------------------------------------------------------------------------
417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418if ( ! function_exists('human_to_unix'))
419{
Timothy Warren01b129a2012-04-27 11:36:50 -0400420 /**
421 * Convert "human" date to GMT
422 *
423 * Reverses the above process
424 *
425 * @param string format: us or euro
426 * @return int
427 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 function human_to_unix($datestr = '')
429 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100430 if ($datestr === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
432 return FALSE;
433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Andrey Andreevae31eb52012-05-17 14:54:15 +0300435 $datestr = preg_replace('/\040+/', ' ', trim($datestr));
Barry Mienydd671972010-10-04 16:33:58 +0200436
Andrey Andreevf11a1932012-06-14 16:35:09 +0300437 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 +0000438 {
439 return FALSE;
440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Jones36591092010-03-05 10:05:51 -0600442 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000443
Andrey Andreevf11a1932012-06-14 16:35:09 +0300444 list($year, $month, $day) = explode('-', $split[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000445
Andrey Andreevae31eb52012-05-17 14:54:15 +0300446 $ex = explode(':', $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200447
Andrey Andreevf11a1932012-06-14 16:35:09 +0300448 $hour = (int) $ex[0];
449 $min = (int) $ex[1];
450 $sec = ( ! empty($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2]))
451 ? (int) $ex[2] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200452
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300453 if (isset($split[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300455 $ampm = strtolower($split[2]);
Barry Mienydd671972010-10-04 16:33:58 +0200456
Andrey Andreevf11a1932012-06-14 16:35:09 +0300457 if ($ampm[0] === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500458 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300459 $hour += 12;
Greg Akerf9168392011-08-29 19:29:05 -0500460 }
Andrey Andreevf11a1932012-06-14 16:35:09 +0300461 elseif ($ampm[0] === 'a' && $hour === 12)
Greg Akerf9168392011-08-29 19:29:05 -0500462 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300463 $hour = 0;
Greg Akerf9168392011-08-29 19:29:05 -0500464 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 return mktime($hour, $min, $sec, $month, $day, $year);
468 }
469}
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471// ------------------------------------------------------------------------
472
Kyle Farris896d95a2011-08-21 23:03:54 -0300473if ( ! function_exists('nice_date'))
474{
Timothy Warren01b129a2012-04-27 11:36:50 -0400475 /**
476 * Turns many "reasonably-date-like" strings into something
477 * that is actually useful. This only works for dates after unix epoch.
478 *
479 * @param string The terribly formatted date-like string
480 * @param string Date format to return (same as php date function)
481 * @return string
482 */
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500483 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300484 {
485 if (empty($bad_date))
486 {
487 return 'Unknown';
488 }
Andrey Andreevd9b44be2012-06-15 16:07:08 +0300489 elseif (empty($format))
490 {
491 $format = 'U';
492 }
Greg Akerf9168392011-08-29 19:29:05 -0500493
Kyle Farris896d95a2011-08-21 23:03:54 -0300494 // Date like: YYYYMM
Andrey Andreevf11a1932012-06-14 16:35:09 +0300495 if (preg_match('/^\d{6}$/i', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300496 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300497 if (in_array(substr($bad_date, 0, 2), array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300498 {
499 $year = substr($bad_date, 0, 4);
500 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500501 }
502 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300503 {
504 $month = substr($bad_date, 0, 2);
505 $year = substr($bad_date, 2, 4);
506 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500507
Andrey Andreevae31eb52012-05-17 14:54:15 +0300508 return date($format, strtotime($year.'-'.$month.'-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300509 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500510
Kyle Farris896d95a2011-08-21 23:03:54 -0300511 // Date Like: YYYYMMDD
Andrey Andreevf11a1932012-06-14 16:35:09 +0300512 if (preg_match('/^(\d{2})\d{2}(\d{4})$/i', $bad_date, $matches))
Kyle Farris896d95a2011-08-21 23:03:54 -0300513 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300514 return date($format, strtotime($matches[1].'/01/'.$matches[2]));
Kyle Farris896d95a2011-08-21 23:03:54 -0300515 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500516
Kyle Farris896d95a2011-08-21 23:03:54 -0300517 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
Andrey Andreevf11a1932012-06-14 16:35:09 +0300518 if (preg_match('/^(\d{1,2})-(\d{1,2})-(\d{4})$/i', $bad_date, $matches))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500519 {
Andrey Andreevf11a1932012-06-14 16:35:09 +0300520 return date($format, strtotime($matches[3].'-'.$matches[1].'-'.$matches[2]));
Kyle Farris896d95a2011-08-21 23:03:54 -0300521 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500522
Kyle Farris896d95a2011-08-21 23:03:54 -0300523 // Any other kind of string, when converted into UNIX time,
524 // produces "0 seconds after epoc..." is probably bad...
525 // return "Invalid Date".
Alex Bilbie773ccc32012-06-02 11:11:08 +0100526 if (date('U', strtotime($bad_date)) === '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500527 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300528 return 'Invalid Date';
Kyle Farris896d95a2011-08-21 23:03:54 -0300529 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500530
Kyle Farris896d95a2011-08-21 23:03:54 -0300531 // It's probably a valid-ish date format already
532 return date($format, strtotime($bad_date));
533 }
534}
535
536// ------------------------------------------------------------------------
537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538if ( ! function_exists('timezone_menu'))
539{
Timothy Warren01b129a2012-04-27 11:36:50 -0400540 /**
541 * Timezone Menu
542 *
543 * Generates a drop-down menu of timezones.
544 *
545 * @param string timezone
546 * @param string classname
547 * @param string menu name
Mat Whitney7540ded2012-06-22 12:02:10 -0700548 * @param mixed attributes
Timothy Warren01b129a2012-04-27 11:36:50 -0400549 * @return string
550 */
Mat Whitney7540ded2012-06-22 12:02:10 -0700551 function timezone_menu($default = 'UTC', $class = '', $name = 'timezones', $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
553 $CI =& get_instance();
554 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200555
Alex Bilbie773ccc32012-06-02 11:11:08 +0100556 $default = ($default === 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000557
558 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200559
Alex Bilbie773ccc32012-06-02 11:11:08 +0100560 if ($class !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 {
562 $menu .= ' class="'.$class.'"';
563 }
Barry Mienydd671972010-10-04 16:33:58 +0200564
Mat Whitney7540ded2012-06-22 12:02:10 -0700565 // Generate a string from the attributes submitted, if any
566 if (is_array($attributes))
567 {
568 $atts = '';
569 foreach ($attributes as $key => $val)
570 {
571 $atts .= ' '.$key.'="'.$val.'"';
572 }
573 $attributes = $atts;
574 }
575 elseif (is_string($attributes) && strlen($attributes) > 0)
576 {
577 $attributes = ' '.$attributes;
578 }
579
580 $menu .= $attributes.">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 foreach (timezones() as $key => $val)
583 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100584 $selected = ($default === $key) ? ' selected="selected"' : '';
Andrey Andreevae31eb52012-05-17 14:54:15 +0300585 $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 }
587
Andrey Andreevae31eb52012-05-17 14:54:15 +0300588 return $menu.'</select>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 }
590}
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592// ------------------------------------------------------------------------
593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594if ( ! function_exists('timezones'))
595{
Timothy Warren01b129a2012-04-27 11:36:50 -0400596 /**
597 * Timezones
598 *
599 * Returns an array of timezones. This is a helper function
600 * for various other ones in this library
601 *
602 * @param string timezone
603 * @return string
604 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 function timezones($tz = '')
606 {
607 // Note: Don't change the order of these even though
608 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200609
610 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500611 'UM12' => -12,
612 'UM11' => -11,
613 'UM10' => -10,
614 'UM95' => -9.5,
615 'UM9' => -9,
616 'UM8' => -8,
617 'UM7' => -7,
618 'UM6' => -6,
619 'UM5' => -5,
620 'UM45' => -4.5,
621 'UM4' => -4,
622 'UM35' => -3.5,
623 'UM3' => -3,
624 'UM2' => -2,
625 'UM1' => -1,
626 'UTC' => 0,
627 'UP1' => +1,
628 'UP2' => +2,
629 'UP3' => +3,
630 'UP35' => +3.5,
631 'UP4' => +4,
632 'UP45' => +4.5,
633 'UP5' => +5,
634 'UP55' => +5.5,
635 'UP575' => +5.75,
636 'UP6' => +6,
637 'UP65' => +6.5,
638 'UP7' => +7,
639 'UP8' => +8,
640 'UP875' => +8.75,
641 'UP9' => +9,
642 'UP95' => +9.5,
643 'UP10' => +10,
644 'UP105' => +10.5,
645 'UP11' => +11,
646 'UP115' => +11.5,
647 'UP12' => +12,
648 'UP1275' => +12.75,
649 'UP13' => +13,
650 'UP14' => +14
651 );
Barry Mienydd671972010-10-04 16:33:58 +0200652
Alex Bilbie773ccc32012-06-02 11:11:08 +0100653 if ($tz === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 {
655 return $zones;
656 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500657
Andrey Andreeve92df332012-03-26 22:44:20 +0300658 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
660}
661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000663/* Location: ./system/helpers/date_helper.php */