blob: 531d1d32f229c3ac0750dd0726fde96302a876c3 [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 '',
Greg Akerf9168392011-08-29 19:29:05 -0500103 preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr)
104 );
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(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500319 gmdate("H", $time),
320 gmdate("i", $time),
321 gmdate("s", $time),
322 gmdate("m", $time),
323 gmdate("d", $time),
Greg Akerf9168392011-08-29 19:29:05 -0500324 gmdate("Y", $time)
325 );
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600456 $datestr = preg_replace("/\040+/", ' ', $datestr);
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 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))
459 {
460 return FALSE;
461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Jones36591092010-03-05 10:05:51 -0600463 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000464
465 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Jones4b9c6292011-07-01 17:40:48 -0500467 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
468 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
469 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000470
471 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones4b9c6292011-07-01 17:40:48 -0500474 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000475
Derek Jones1322ec52009-03-11 17:01:14 +0000476 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500478 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480 else
481 {
482 // Unless specified, seconds get set to zero.
483 $sec = '00';
484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 if (isset($split['2']))
487 {
488 $ampm = strtolower($split['2']);
Barry Mienydd671972010-10-04 16:33:58 +0200489
Andrey Andreeve92df332012-03-26 22:44:20 +0300490 if (substr($ampm, 0, 1) === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500491 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500492 $hour = $hour + 12;
Greg Akerf9168392011-08-29 19:29:05 -0500493 }
Barry Mienydd671972010-10-04 16:33:58 +0200494
Andrey Andreeve92df332012-03-26 22:44:20 +0300495 if (substr($ampm, 0, 1) === 'a' && $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500496 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500497 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500498 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 if (strlen($hour) == 1)
Greg Akerf9168392011-08-29 19:29:05 -0500501 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500502 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500503 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 return mktime($hour, $min, $sec, $month, $day, $year);
507 }
508}
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510// ------------------------------------------------------------------------
511
Kyle Farris896d95a2011-08-21 23:03:54 -0300512if ( ! function_exists('nice_date'))
513{
Timothy Warren01b129a2012-04-27 11:36:50 -0400514 /**
515 * Turns many "reasonably-date-like" strings into something
516 * that is actually useful. This only works for dates after unix epoch.
517 *
518 * @param string The terribly formatted date-like string
519 * @param string Date format to return (same as php date function)
520 * @return string
521 */
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500522 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300523 {
524 if (empty($bad_date))
525 {
526 return 'Unknown';
527 }
Greg Akerf9168392011-08-29 19:29:05 -0500528
Kyle Farris896d95a2011-08-21 23:03:54 -0300529 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500530 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300531 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500532 if (in_array(substr($bad_date, 0, 2),array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300533 {
534 $year = substr($bad_date, 0, 4);
535 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500536 }
537 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300538 {
539 $month = substr($bad_date, 0, 2);
540 $year = substr($bad_date, 2, 4);
541 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500542
Kyle Farris896d95a2011-08-21 23:03:54 -0300543 return date($format, strtotime($year . '-' . $month . '-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300544 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500545
Kyle Farris896d95a2011-08-21 23:03:54 -0300546 // Date Like: YYYYMMDD
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500547 if (preg_match('/^\d{8}$/',$bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300548 {
549 $month = substr($bad_date, 0, 2);
550 $day = substr($bad_date, 2, 2);
551 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500552
Kyle Farris896d95a2011-08-21 23:03:54 -0300553 return date($format, strtotime($month . '/01/' . $year));
554 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500555
Kyle Farris896d95a2011-08-21 23:03:54 -0300556 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
557 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500558 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300559 list($m, $d, $y) = explode('-', $bad_date);
560 return date($format, strtotime("{$y}-{$m}-{$d}"));
561 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500562
Kyle Farris896d95a2011-08-21 23:03:54 -0300563 // Any other kind of string, when converted into UNIX time,
564 // produces "0 seconds after epoc..." is probably bad...
565 // return "Invalid Date".
566 if (date('U', strtotime($bad_date)) == '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500567 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300568 return "Invalid Date";
569 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500570
Kyle Farris896d95a2011-08-21 23:03:54 -0300571 // It's probably a valid-ish date format already
572 return date($format, strtotime($bad_date));
573 }
574}
575
576// ------------------------------------------------------------------------
577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578if ( ! function_exists('timezone_menu'))
579{
Timothy Warren01b129a2012-04-27 11:36:50 -0400580 /**
581 * Timezone Menu
582 *
583 * Generates a drop-down menu of timezones.
584 *
585 * @param string timezone
586 * @param string classname
587 * @param string menu name
588 * @return string
589 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
591 {
592 $CI =& get_instance();
593 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200594
Greg Akerc964e722011-08-29 19:31:29 -0500595 $default = ($default == 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000596
597 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 if ($class != '')
600 {
601 $menu .= ' class="'.$class.'"';
602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 foreach (timezones() as $key => $val)
607 {
608 $selected = ($default == $key) ? " selected='selected'" : '';
609 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
610 }
611
612 $menu .= "</select>";
613
614 return $menu;
615 }
616}
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618// ------------------------------------------------------------------------
619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620if ( ! function_exists('timezones'))
621{
Timothy Warren01b129a2012-04-27 11:36:50 -0400622 /**
623 * Timezones
624 *
625 * Returns an array of timezones. This is a helper function
626 * for various other ones in this library
627 *
628 * @param string timezone
629 * @return string
630 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 function timezones($tz = '')
632 {
633 // Note: Don't change the order of these even though
634 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200635
636 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500637 'UM12' => -12,
638 'UM11' => -11,
639 'UM10' => -10,
640 'UM95' => -9.5,
641 'UM9' => -9,
642 'UM8' => -8,
643 'UM7' => -7,
644 'UM6' => -6,
645 'UM5' => -5,
646 'UM45' => -4.5,
647 'UM4' => -4,
648 'UM35' => -3.5,
649 'UM3' => -3,
650 'UM2' => -2,
651 'UM1' => -1,
652 'UTC' => 0,
653 'UP1' => +1,
654 'UP2' => +2,
655 'UP3' => +3,
656 'UP35' => +3.5,
657 'UP4' => +4,
658 'UP45' => +4.5,
659 'UP5' => +5,
660 'UP55' => +5.5,
661 'UP575' => +5.75,
662 'UP6' => +6,
663 'UP65' => +6.5,
664 'UP7' => +7,
665 'UP8' => +8,
666 'UP875' => +8.75,
667 'UP9' => +9,
668 'UP95' => +9.5,
669 'UP10' => +10,
670 'UP105' => +10.5,
671 'UP11' => +11,
672 'UP115' => +11.5,
673 'UP12' => +12,
674 'UP1275' => +12.75,
675 'UP13' => +13,
676 'UP14' => +14
677 );
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 if ($tz == '')
680 {
681 return $zones;
682 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500683
Greg Akerf9168392011-08-29 19:29:05 -0500684 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500685
Andrey Andreeve92df332012-03-26 22:44:20 +0300686 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 }
688}
689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000691/* Location: ./system/helpers/date_helper.php */