blob: 5f0427f7dd3cb5e4b3c971e64a0325585f5603aa [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 *
45 * Returns time() or its GMT equivalent based on the config file preference
46 *
47 * @return int
48 */
Derek Allard2067d1a2008-11-13 22:59:24 +000049 function now()
50 {
51 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020052
Derek Allard2067d1a2008-11-13 22:59:24 +000053 if (strtolower($CI->config->item('time_reference')) == 'gmt')
54 {
55 $now = time();
56 $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 if (strlen($system_time) < 10)
59 {
60 $system_time = time();
61 log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
62 }
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 return $system_time;
65 }
Greg Akerc964e722011-08-29 19:31:29 -050066
67 return time();
Derek Allard2067d1a2008-11-13 22:59:24 +000068 }
69}
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071// ------------------------------------------------------------------------
72
Derek Allard2067d1a2008-11-13 22:59:24 +000073if ( ! function_exists('mdate'))
74{
Timothy Warren01b129a2012-04-27 11:36:50 -040075 /**
76 * Convert MySQL Style Datecodes
77 *
78 * This function is identical to PHPs date() function,
79 * except that it allows date codes to be formatted using
80 * the MySQL style, where each code letter is preceded
81 * with a percent sign: %Y %m %d etc...
82 *
83 * The benefit of doing dates this way is that you don't
84 * have to worry about escaping your text letters that
85 * match the date codes.
86 *
87 * @param string
88 * @param int
89 * @return int
90 */
Derek Allard2067d1a2008-11-13 22:59:24 +000091 function mdate($datestr = '', $time = '')
92 {
93 if ($datestr == '')
Greg Akerf9168392011-08-29 19:29:05 -050094 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -050095 return '';
Greg Akerf9168392011-08-29 19:29:05 -050096 }
Barry Mienydd671972010-10-04 16:33:58 +020097
Greg Akerc964e722011-08-29 19:31:29 -050098 $time = ($time == '') ? now() : $time;
Barry Mienydd671972010-10-04 16:33:58 +020099
Greg Akerf9168392011-08-29 19:29:05 -0500100 $datestr = str_replace(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500101 '%\\',
102 '',
Andrey Andreevae31eb52012-05-17 14:54:15 +0300103 preg_replace('/([a-z]+?){1}/i', '\\\\\\1', $datestr)
Greg Akerf9168392011-08-29 19:29:05 -0500104 );
Greg Akerc964e722011-08-29 19:31:29 -0500105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 return date($datestr, $time);
107 }
108}
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110// ------------------------------------------------------------------------
111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112if ( ! function_exists('standard_date'))
113{
Timothy Warren01b129a2012-04-27 11:36:50 -0400114 /**
115 * Standard Date
116 *
117 * Returns a date formatted according to the submitted standard.
118 *
119 * @param string the chosen format
120 * @param int Unix timestamp
121 * @return string
122 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 function standard_date($fmt = 'DATE_RFC822', $time = '')
124 {
125 $formats = array(
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400126 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400128 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
Syahril Zulkefli90658ad2011-11-13 23:43:37 +0800130 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC',
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
132 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400133 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400135 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O'
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 );
137
138 if ( ! isset($formats[$fmt]))
139 {
140 return FALSE;
141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 return mdate($formats[$fmt], $time);
144 }
145}
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147// ------------------------------------------------------------------------
148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149if ( ! function_exists('timespan'))
150{
Timothy Warren01b129a2012-04-27 11:36:50 -0400151 /**
152 * Timespan
153 *
154 * Returns a span of seconds in this format:
155 * 10 days 14 hours 36 minutes 47 seconds
156 *
157 * @param int a number of seconds
158 * @param int Unix timestamp
159 * @param int a number of display units
160 * @return string
161 */
Roger Herbert8d69aa12012-03-14 08:44:55 +0000162 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
164 $CI =& get_instance();
165 $CI->lang->load('date');
166
167 if ( ! is_numeric($seconds))
168 {
169 $seconds = 1;
170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 if ( ! is_numeric($time))
173 {
174 $time = time();
175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Roger Herbert04c146d2012-03-11 15:31:01 +0000177 if ( ! is_numeric($units))
178 {
Roger Herbert8d69aa12012-03-14 08:44:55 +0000179 $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000180 }
181
Greg Akerf9168392011-08-29 19:29:05 -0500182 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200183
Roger Herbert04c146d2012-03-11 15:31:01 +0000184 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500185 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200188 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000189 $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
Barry Mienydd671972010-10-04 16:33:58 +0200190 }
191
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500192 $seconds -= $years * 31557600;
193 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200194
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000195 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
197 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200198 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000199 $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month'));
Barry Mienydd671972010-10-04 16:33:58 +0200200 }
201
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500202 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
204
205 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200206
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000207 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
209 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200210 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000211 $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200215 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000216
217 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200218
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000219 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
221 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200222 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000223 $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 $seconds -= $days * 86400;
227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200230
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000231 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
233 if ($hours > 0)
234 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000235 $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 $seconds -= $hours * 3600;
239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200242
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000243 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
245 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200246 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000247 $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 $seconds -= $minutes * 60;
251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Roger Herbert597eb212012-03-14 09:06:17 +0000253 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000255 $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Roger Herbert04c146d2012-03-11 15:31:01 +0000258 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
260}
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262// ------------------------------------------------------------------------
263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264if ( ! function_exists('days_in_month'))
265{
Timothy Warren01b129a2012-04-27 11:36:50 -0400266 /**
267 * Number of days in a month
268 *
269 * Takes a month/year as input and returns the number of days
270 * for the given month/year. Takes leap years into consideration.
271 *
272 * @param int a numeric month
273 * @param int a numeric year
274 * @return int
275 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 function days_in_month($month = 0, $year = '')
277 {
278 if ($month < 1 OR $month > 12)
279 {
280 return 0;
281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 if ( ! is_numeric($year) OR strlen($year) != 4)
284 {
285 $year = date('Y');
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 if ($month == 2)
289 {
Andrey Andreeve92df332012-03-26 22:44:20 +0300290 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 {
313 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 Andreevae31eb52012-05-17 14:54:15 +0300319 gmdate('H', $time),
320 gmdate('i', $time),
321 gmdate('s', $time),
322 gmdate('m', $time),
323 gmdate('d', $time),
324 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 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 if ($time == '')
348 {
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
354 if ($dst == TRUE)
355 {
356 $time += 3600;
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 return $time;
360 }
361}
Derek Jones36591092010-03-05 10:05:51 -0600362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363// ------------------------------------------------------------------------
364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365if ( ! function_exists('mysql_to_unix'))
366{
Timothy Warren01b129a2012-04-27 11:36:50 -0400367 /**
368 * Converts a MySQL Timestamp to Unix
369 *
370 * @param int Unix timestamp
371 * @return int
372 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 function mysql_to_unix($time = '')
374 {
375 // We'll remove certain characters for backward compatibility
376 // since the formatting changed with MySQL 4.1
377 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 $time = str_replace('-', '', $time);
380 $time = str_replace(':', '', $time);
381 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500384 return mktime(
385 substr($time, 8, 2),
386 substr($time, 10, 2),
387 substr($time, 12, 2),
388 substr($time, 4, 2),
389 substr($time, 6, 2),
390 substr($time, 0, 4)
391 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
393}
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395// ------------------------------------------------------------------------
396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397if ( ! function_exists('unix_to_human'))
398{
Timothy Warren01b129a2012-04-27 11:36:50 -0400399 /**
400 * Unix to "Human"
401 *
402 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
403 *
404 * @param int Unix timestamp
405 * @param bool whether to show seconds
406 * @param string format: us or euro
407 * @return string
408 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
410 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500411 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 if ($fmt == 'us')
414 {
415 $r .= date('h', $time).':'.date('i', $time);
416 }
417 else
418 {
419 $r .= date('H', $time).':'.date('i', $time);
420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 if ($seconds)
423 {
424 $r .= ':'.date('s', $time);
425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 if ($fmt == 'us')
428 {
429 $r .= ' '.date('A', $time);
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 return $r;
433 }
434}
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436// ------------------------------------------------------------------------
437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438if ( ! function_exists('human_to_unix'))
439{
Timothy Warren01b129a2012-04-27 11:36:50 -0400440 /**
441 * Convert "human" date to GMT
442 *
443 * Reverses the above process
444 *
445 * @param string format: us or euro
446 * @return int
447 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 function human_to_unix($datestr = '')
449 {
450 if ($datestr == '')
451 {
452 return FALSE;
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Andrey Andreevae31eb52012-05-17 14:54:15 +0300455 $datestr = preg_replace('/\040+/', ' ', trim($datestr));
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 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))
458 {
459 return FALSE;
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Jones36591092010-03-05 10:05:51 -0600462 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000463
Andrey Andreevae31eb52012-05-17 14:54:15 +0300464 $ex = explode('-', $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200465
Andrey Andreevae31eb52012-05-17 14:54:15 +0300466 $year = (strlen($ex[0]) === 2) ? '20'.$ex[0] : $ex[0];
467 $month = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
468 $day = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000469
Andrey Andreevae31eb52012-05-17 14:54:15 +0300470 $ex = explode(':', $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200471
Andrey Andreevae31eb52012-05-17 14:54:15 +0300472 $hour = (strlen($ex[0]) === 1) ? '0'.$ex[0] : $ex[0];
473 $min = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000474
Andrey Andreevae31eb52012-05-17 14:54:15 +0300475 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300477 $sec = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
479 else
480 {
481 // Unless specified, seconds get set to zero.
482 $sec = '00';
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 if (isset($split['2']))
486 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300487 $ampm = strtolower($split[2]);
Barry Mienydd671972010-10-04 16:33:58 +0200488
Andrey Andreeve92df332012-03-26 22:44:20 +0300489 if (substr($ampm, 0, 1) === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500490 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300491 $hour += 12;
Greg Akerf9168392011-08-29 19:29:05 -0500492 }
Barry Mienydd671972010-10-04 16:33:58 +0200493
Andrey Andreeve92df332012-03-26 22:44:20 +0300494 if (substr($ampm, 0, 1) === 'a' && $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500495 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500496 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500497 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500498
Andrey Andreevae31eb52012-05-17 14:54:15 +0300499 if (strlen($hour) === 1)
Greg Akerf9168392011-08-29 19:29:05 -0500500 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500501 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500502 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 return mktime($hour, $min, $sec, $month, $day, $year);
506 }
507}
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509// ------------------------------------------------------------------------
510
Kyle Farris896d95a2011-08-21 23:03:54 -0300511if ( ! function_exists('nice_date'))
512{
Timothy Warren01b129a2012-04-27 11:36:50 -0400513 /**
514 * Turns many "reasonably-date-like" strings into something
515 * that is actually useful. This only works for dates after unix epoch.
516 *
517 * @param string The terribly formatted date-like string
518 * @param string Date format to return (same as php date function)
519 * @return string
520 */
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500521 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300522 {
523 if (empty($bad_date))
524 {
525 return 'Unknown';
526 }
Greg Akerf9168392011-08-29 19:29:05 -0500527
Kyle Farris896d95a2011-08-21 23:03:54 -0300528 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500529 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300530 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300531 if (in_array(substr($bad_date, 0, 2), array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300532 {
533 $year = substr($bad_date, 0, 4);
534 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500535 }
536 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300537 {
538 $month = substr($bad_date, 0, 2);
539 $year = substr($bad_date, 2, 4);
540 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500541
Andrey Andreevae31eb52012-05-17 14:54:15 +0300542 return date($format, strtotime($year.'-'.$month.'-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300543 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500544
Kyle Farris896d95a2011-08-21 23:03:54 -0300545 // Date Like: YYYYMMDD
Andrey Andreevae31eb52012-05-17 14:54:15 +0300546 if (preg_match('/^\d{8}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300547 {
548 $month = substr($bad_date, 0, 2);
549 $day = substr($bad_date, 2, 2);
550 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500551
Andrey Andreevae31eb52012-05-17 14:54:15 +0300552 return date($format, strtotime($month.'/01/'.$year));
Kyle Farris896d95a2011-08-21 23:03:54 -0300553 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500554
Kyle Farris896d95a2011-08-21 23:03:54 -0300555 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
Andrey Andreevae31eb52012-05-17 14:54:15 +0300556 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/', $bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500557 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300558 list($m, $d, $y) = explode('-', $bad_date);
Andrey Andreevae31eb52012-05-17 14:54:15 +0300559 return date($format, strtotime($y.'-'.$m.'-'.$d));
Kyle Farris896d95a2011-08-21 23:03:54 -0300560 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500561
Kyle Farris896d95a2011-08-21 23:03:54 -0300562 // Any other kind of string, when converted into UNIX time,
563 // produces "0 seconds after epoc..." is probably bad...
564 // return "Invalid Date".
565 if (date('U', strtotime($bad_date)) == '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500566 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300567 return 'Invalid Date';
Kyle Farris896d95a2011-08-21 23:03:54 -0300568 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500569
Kyle Farris896d95a2011-08-21 23:03:54 -0300570 // It's probably a valid-ish date format already
571 return date($format, strtotime($bad_date));
572 }
573}
574
575// ------------------------------------------------------------------------
576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577if ( ! function_exists('timezone_menu'))
578{
Timothy Warren01b129a2012-04-27 11:36:50 -0400579 /**
580 * Timezone Menu
581 *
582 * Generates a drop-down menu of timezones.
583 *
584 * @param string timezone
585 * @param string classname
586 * @param string menu name
587 * @return string
588 */
Andrey Andreevae31eb52012-05-17 14:54:15 +0300589 function timezone_menu($default = 'UTC', $class = '', $name = 'timezones')
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
591 $CI =& get_instance();
592 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200593
Greg Akerc964e722011-08-29 19:31:29 -0500594 $default = ($default == 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000595
596 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 if ($class != '')
599 {
600 $menu .= ' class="'.$class.'"';
601 }
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 foreach (timezones() as $key => $val)
606 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300607 $selected = ($default == $key) ? ' selected="selected"' : '';
608 $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 }
610
Andrey Andreevae31eb52012-05-17 14:54:15 +0300611 return $menu.'</select>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 }
613}
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615// ------------------------------------------------------------------------
616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617if ( ! function_exists('timezones'))
618{
Timothy Warren01b129a2012-04-27 11:36:50 -0400619 /**
620 * Timezones
621 *
622 * Returns an array of timezones. This is a helper function
623 * for various other ones in this library
624 *
625 * @param string timezone
626 * @return string
627 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 function timezones($tz = '')
629 {
630 // Note: Don't change the order of these even though
631 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200632
633 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500634 'UM12' => -12,
635 'UM11' => -11,
636 'UM10' => -10,
637 'UM95' => -9.5,
638 'UM9' => -9,
639 'UM8' => -8,
640 'UM7' => -7,
641 'UM6' => -6,
642 'UM5' => -5,
643 'UM45' => -4.5,
644 'UM4' => -4,
645 'UM35' => -3.5,
646 'UM3' => -3,
647 'UM2' => -2,
648 'UM1' => -1,
649 'UTC' => 0,
650 'UP1' => +1,
651 'UP2' => +2,
652 'UP3' => +3,
653 'UP35' => +3.5,
654 'UP4' => +4,
655 'UP45' => +4.5,
656 'UP5' => +5,
657 'UP55' => +5.5,
658 'UP575' => +5.75,
659 'UP6' => +6,
660 'UP65' => +6.5,
661 'UP7' => +7,
662 'UP8' => +8,
663 'UP875' => +8.75,
664 'UP9' => +9,
665 'UP95' => +9.5,
666 'UP10' => +10,
667 'UP105' => +10.5,
668 'UP11' => +11,
669 'UP115' => +11.5,
670 'UP12' => +12,
671 'UP1275' => +12.75,
672 'UP13' => +13,
673 'UP14' => +14
674 );
Barry Mienydd671972010-10-04 16:33:58 +0200675
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 if ($tz == '')
677 {
678 return $zones;
679 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500680
Greg Akerf9168392011-08-29 19:29:05 -0500681 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500682
Andrey Andreeve92df332012-03-26 22:44:20 +0300683 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 }
685}
686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000688/* Location: ./system/helpers/date_helper.php */