blob: d5036f64559a54650e41e5a2688b8ad0d9d86b33 [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 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Alex Bilbie773ccc32012-06-02 11:11:08 +010097 $time = ($time === '') ? now() : $time;
Barry Mienydd671972010-10-04 16:33:58 +020098
Greg Akerf9168392011-08-29 19:29:05 -050099 $datestr = str_replace(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500100 '%\\',
101 '',
Andrey Andreevae31eb52012-05-17 14:54:15 +0300102 preg_replace('/([a-z]+?){1}/i', '\\\\\\1', $datestr)
Greg Akerf9168392011-08-29 19:29:05 -0500103 );
Greg Akerc964e722011-08-29 19:31:29 -0500104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 return date($datestr, $time);
106 }
107}
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109// ------------------------------------------------------------------------
110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111if ( ! function_exists('standard_date'))
112{
Timothy Warren01b129a2012-04-27 11:36:50 -0400113 /**
114 * Standard Date
115 *
116 * Returns a date formatted according to the submitted standard.
117 *
118 * @param string the chosen format
119 * @param int Unix timestamp
120 * @return string
121 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 function standard_date($fmt = 'DATE_RFC822', $time = '')
123 {
124 $formats = array(
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400125 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400127 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
Syahril Zulkefli90658ad2011-11-13 23:43:37 +0800129 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC',
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
131 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400132 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400134 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O'
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 );
136
137 if ( ! isset($formats[$fmt]))
138 {
139 return FALSE;
140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 return mdate($formats[$fmt], $time);
143 }
144}
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146// ------------------------------------------------------------------------
147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148if ( ! function_exists('timespan'))
149{
Timothy Warren01b129a2012-04-27 11:36:50 -0400150 /**
151 * Timespan
152 *
153 * Returns a span of seconds in this format:
154 * 10 days 14 hours 36 minutes 47 seconds
155 *
156 * @param int a number of seconds
157 * @param int Unix timestamp
158 * @param int a number of display units
159 * @return string
160 */
Roger Herbert8d69aa12012-03-14 08:44:55 +0000161 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 $CI =& get_instance();
164 $CI->lang->load('date');
165
166 if ( ! is_numeric($seconds))
167 {
168 $seconds = 1;
169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 if ( ! is_numeric($time))
172 {
173 $time = time();
174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Roger Herbert04c146d2012-03-11 15:31:01 +0000176 if ( ! is_numeric($units))
177 {
Roger Herbert8d69aa12012-03-14 08:44:55 +0000178 $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000179 }
180
Greg Akerf9168392011-08-29 19:29:05 -0500181 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200182
Roger Herbert04c146d2012-03-11 15:31:01 +0000183 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500184 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200187 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000188 $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
Barry Mienydd671972010-10-04 16:33:58 +0200189 }
190
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500191 $seconds -= $years * 31557600;
192 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200193
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000194 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
196 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200197 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000198 $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month'));
Barry Mienydd671972010-10-04 16:33:58 +0200199 }
200
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500201 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200205
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000206 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
208 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200209 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000210 $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200214 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
216 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200217
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000218 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
220 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200221 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000222 $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 $seconds -= $days * 86400;
226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200229
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000230 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
232 if ($hours > 0)
233 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000234 $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 $seconds -= $hours * 3600;
238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200241
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000242 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
244 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200245 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000246 $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 $seconds -= $minutes * 60;
250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Roger Herbert597eb212012-03-14 09:06:17 +0000252 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000254 $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Roger Herbert04c146d2012-03-11 15:31:01 +0000257 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
259}
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261// ------------------------------------------------------------------------
262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263if ( ! function_exists('days_in_month'))
264{
Timothy Warren01b129a2012-04-27 11:36:50 -0400265 /**
266 * Number of days in a month
267 *
268 * Takes a month/year as input and returns the number of days
269 * for the given month/year. Takes leap years into consideration.
270 *
271 * @param int a numeric month
272 * @param int a numeric year
273 * @return int
274 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 function days_in_month($month = 0, $year = '')
276 {
277 if ($month < 1 OR $month > 12)
278 {
279 return 0;
280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281
Alex Bilbie773ccc32012-06-02 11:11:08 +0100282 if ( ! is_numeric($year) OR strlen($year) !== 4)
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
284 $year = date('Y');
285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 if ($month == 2)
288 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100289 if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 return 29;
292 }
293 }
294
295 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
296 return $days_in_month[$month - 1];
297 }
298}
Derek Jones36591092010-03-05 10:05:51 -0600299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300// ------------------------------------------------------------------------
301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302if ( ! function_exists('local_to_gmt'))
303{
Timothy Warren01b129a2012-04-27 11:36:50 -0400304 /**
305 * Converts a local Unix timestamp to GMT
306 *
307 * @param int Unix timestamp
308 * @return int
309 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 function local_to_gmt($time = '')
311 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100312 if ($time === '')
Greg Akerf9168392011-08-29 19:29:05 -0500313 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500315 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500316
Andrey Andreev3bbbd262012-06-14 13:35:32 +0300317 return gmmktime(
318 date('H', $time),
319 date('i', $time),
320 date('s', $time),
321 date('m', $time),
322 date('d', $time),
323 date('Y', $time)
Greg Akerf9168392011-08-29 19:29:05 -0500324 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
326}
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328// ------------------------------------------------------------------------
329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330if ( ! function_exists('gmt_to_local'))
331{
Timothy Warren01b129a2012-04-27 11:36:50 -0400332 /**
333 * Converts GMT time to a localized value
334 *
335 * Takes a Unix timestamp (in GMT) as input, and returns
336 * at the local value based on the timezone and DST setting
337 * submitted
338 *
339 * @param int Unix timestamp
340 * @param string timezone
341 * @param bool whether DST is active
342 * @return int
343 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200345 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100346 if ($time === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
348 return now();
349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 $time += timezones($timezone) * 3600;
352
Alex Bilbie773ccc32012-06-02 11:11:08 +0100353 if ($dst === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
355 $time += 3600;
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 return $time;
359 }
360}
Derek Jones36591092010-03-05 10:05:51 -0600361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362// ------------------------------------------------------------------------
363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364if ( ! function_exists('mysql_to_unix'))
365{
Timothy Warren01b129a2012-04-27 11:36:50 -0400366 /**
367 * Converts a MySQL Timestamp to Unix
368 *
369 * @param int Unix timestamp
370 * @return int
371 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 function mysql_to_unix($time = '')
373 {
374 // We'll remove certain characters for backward compatibility
375 // since the formatting changed with MySQL 4.1
376 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200377
Andrey Andreev3bbbd262012-06-14 13:35:32 +0300378 $time = str_replace(array('-', ':', ' '), '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500381 return mktime(
382 substr($time, 8, 2),
383 substr($time, 10, 2),
384 substr($time, 12, 2),
385 substr($time, 4, 2),
386 substr($time, 6, 2),
387 substr($time, 0, 4)
388 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 }
390}
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392// ------------------------------------------------------------------------
393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394if ( ! function_exists('unix_to_human'))
395{
Timothy Warren01b129a2012-04-27 11:36:50 -0400396 /**
397 * Unix to "Human"
398 *
399 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
400 *
401 * @param int Unix timestamp
402 * @param bool whether to show seconds
403 * @param string format: us or euro
404 * @return string
405 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
407 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500408 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200409
Alex Bilbie773ccc32012-06-02 11:11:08 +0100410 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
412 $r .= date('h', $time).':'.date('i', $time);
413 }
414 else
415 {
416 $r .= date('H', $time).':'.date('i', $time);
417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 if ($seconds)
420 {
421 $r .= ':'.date('s', $time);
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Alex Bilbie773ccc32012-06-02 11:11:08 +0100424 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
426 $r .= ' '.date('A', $time);
427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 return $r;
430 }
431}
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433// ------------------------------------------------------------------------
434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435if ( ! function_exists('human_to_unix'))
436{
Timothy Warren01b129a2012-04-27 11:36:50 -0400437 /**
438 * Convert "human" date to GMT
439 *
440 * Reverses the above process
441 *
442 * @param string format: us or euro
443 * @return int
444 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 function human_to_unix($datestr = '')
446 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100447 if ($datestr === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
449 return FALSE;
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Andrey Andreevae31eb52012-05-17 14:54:15 +0300452 $datestr = preg_replace('/\040+/', ' ', trim($datestr));
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 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))
455 {
456 return FALSE;
457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Jones36591092010-03-05 10:05:51 -0600459 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000460
Andrey Andreevae31eb52012-05-17 14:54:15 +0300461 $ex = explode('-', $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200462
Andrey Andreevae31eb52012-05-17 14:54:15 +0300463 $year = (strlen($ex[0]) === 2) ? '20'.$ex[0] : $ex[0];
464 $month = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
465 $day = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000466
Andrey Andreevae31eb52012-05-17 14:54:15 +0300467 $ex = explode(':', $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200468
Andrey Andreevae31eb52012-05-17 14:54:15 +0300469 $hour = (strlen($ex[0]) === 1) ? '0'.$ex[0] : $ex[0];
470 $min = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000471
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300472 if (isset($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300474 $sec = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
476 else
477 {
478 // Unless specified, seconds get set to zero.
479 $sec = '00';
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300482 if (isset($split[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300484 $ampm = strtolower($split[2]);
Barry Mienydd671972010-10-04 16:33:58 +0200485
Andrey Andreeve92df332012-03-26 22:44:20 +0300486 if (substr($ampm, 0, 1) === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500487 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300488 $hour += 12;
Greg Akerf9168392011-08-29 19:29:05 -0500489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Andrey Andreeve92df332012-03-26 22:44:20 +0300491 if (substr($ampm, 0, 1) === 'a' && $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500492 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500493 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500494 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500495
Andrey Andreevae31eb52012-05-17 14:54:15 +0300496 if (strlen($hour) === 1)
Greg Akerf9168392011-08-29 19:29:05 -0500497 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500498 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500499 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 return mktime($hour, $min, $sec, $month, $day, $year);
503 }
504}
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506// ------------------------------------------------------------------------
507
Kyle Farris896d95a2011-08-21 23:03:54 -0300508if ( ! function_exists('nice_date'))
509{
Timothy Warren01b129a2012-04-27 11:36:50 -0400510 /**
511 * Turns many "reasonably-date-like" strings into something
512 * that is actually useful. This only works for dates after unix epoch.
513 *
514 * @param string The terribly formatted date-like string
515 * @param string Date format to return (same as php date function)
516 * @return string
517 */
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500518 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300519 {
520 if (empty($bad_date))
521 {
522 return 'Unknown';
523 }
Greg Akerf9168392011-08-29 19:29:05 -0500524
Kyle Farris896d95a2011-08-21 23:03:54 -0300525 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500526 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300527 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300528 if (in_array(substr($bad_date, 0, 2), array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300529 {
530 $year = substr($bad_date, 0, 4);
531 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500532 }
533 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300534 {
535 $month = substr($bad_date, 0, 2);
536 $year = substr($bad_date, 2, 4);
537 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500538
Andrey Andreevae31eb52012-05-17 14:54:15 +0300539 return date($format, strtotime($year.'-'.$month.'-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300540 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500541
Kyle Farris896d95a2011-08-21 23:03:54 -0300542 // Date Like: YYYYMMDD
Andrey Andreevae31eb52012-05-17 14:54:15 +0300543 if (preg_match('/^\d{8}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300544 {
545 $month = substr($bad_date, 0, 2);
546 $day = substr($bad_date, 2, 2);
547 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500548
Andrey Andreevae31eb52012-05-17 14:54:15 +0300549 return date($format, strtotime($month.'/01/'.$year));
Kyle Farris896d95a2011-08-21 23:03:54 -0300550 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500551
Kyle Farris896d95a2011-08-21 23:03:54 -0300552 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
Andrey Andreevae31eb52012-05-17 14:54:15 +0300553 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/', $bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500554 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300555 list($m, $d, $y) = explode('-', $bad_date);
Andrey Andreevae31eb52012-05-17 14:54:15 +0300556 return date($format, strtotime($y.'-'.$m.'-'.$d));
Kyle Farris896d95a2011-08-21 23:03:54 -0300557 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500558
Kyle Farris896d95a2011-08-21 23:03:54 -0300559 // Any other kind of string, when converted into UNIX time,
560 // produces "0 seconds after epoc..." is probably bad...
561 // return "Invalid Date".
Alex Bilbie773ccc32012-06-02 11:11:08 +0100562 if (date('U', strtotime($bad_date)) === '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500563 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300564 return 'Invalid Date';
Kyle Farris896d95a2011-08-21 23:03:54 -0300565 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500566
Kyle Farris896d95a2011-08-21 23:03:54 -0300567 // It's probably a valid-ish date format already
568 return date($format, strtotime($bad_date));
569 }
570}
571
572// ------------------------------------------------------------------------
573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574if ( ! function_exists('timezone_menu'))
575{
Timothy Warren01b129a2012-04-27 11:36:50 -0400576 /**
577 * Timezone Menu
578 *
579 * Generates a drop-down menu of timezones.
580 *
581 * @param string timezone
582 * @param string classname
583 * @param string menu name
584 * @return string
585 */
Andrey Andreevae31eb52012-05-17 14:54:15 +0300586 function timezone_menu($default = 'UTC', $class = '', $name = 'timezones')
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 $CI =& get_instance();
589 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200590
Alex Bilbie773ccc32012-06-02 11:11:08 +0100591 $default = ($default === 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000592
593 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200594
Alex Bilbie773ccc32012-06-02 11:11:08 +0100595 if ($class !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
597 $menu .= ' class="'.$class.'"';
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 foreach (timezones() as $key => $val)
603 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100604 $selected = ($default === $key) ? ' selected="selected"' : '';
Andrey Andreevae31eb52012-05-17 14:54:15 +0300605 $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 }
607
Andrey Andreevae31eb52012-05-17 14:54:15 +0300608 return $menu.'</select>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 }
610}
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612// ------------------------------------------------------------------------
613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614if ( ! function_exists('timezones'))
615{
Timothy Warren01b129a2012-04-27 11:36:50 -0400616 /**
617 * Timezones
618 *
619 * Returns an array of timezones. This is a helper function
620 * for various other ones in this library
621 *
622 * @param string timezone
623 * @return string
624 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 function timezones($tz = '')
626 {
627 // Note: Don't change the order of these even though
628 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200629
630 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500631 'UM12' => -12,
632 'UM11' => -11,
633 'UM10' => -10,
634 'UM95' => -9.5,
635 'UM9' => -9,
636 'UM8' => -8,
637 'UM7' => -7,
638 'UM6' => -6,
639 'UM5' => -5,
640 'UM45' => -4.5,
641 'UM4' => -4,
642 'UM35' => -3.5,
643 'UM3' => -3,
644 'UM2' => -2,
645 'UM1' => -1,
646 'UTC' => 0,
647 'UP1' => +1,
648 'UP2' => +2,
649 'UP3' => +3,
650 'UP35' => +3.5,
651 'UP4' => +4,
652 'UP45' => +4.5,
653 'UP5' => +5,
654 'UP55' => +5.5,
655 'UP575' => +5.75,
656 'UP6' => +6,
657 'UP65' => +6.5,
658 'UP7' => +7,
659 'UP8' => +8,
660 'UP875' => +8.75,
661 'UP9' => +9,
662 'UP95' => +9.5,
663 'UP10' => +10,
664 'UP105' => +10.5,
665 'UP11' => +11,
666 'UP115' => +11.5,
667 'UP12' => +12,
668 'UP1275' => +12.75,
669 'UP13' => +13,
670 'UP14' => +14
671 );
Barry Mienydd671972010-10-04 16:33:58 +0200672
Alex Bilbie773ccc32012-06-02 11:11:08 +0100673 if ($tz === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
675 return $zones;
676 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500677
Alex Bilbie773ccc32012-06-02 11:11:08 +0100678 $tz = ($tz === 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500679
Andrey Andreeve92df332012-03-26 22:44:20 +0300680 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
682}
683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000685/* Location: ./system/helpers/date_helper.php */