blob: a5c46e47b0a088e6c4900d1561a6d78655d282ed [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
Greg Akerf9168392011-08-29 19:29:05 -0500317 return mktime(
Andrey Andreevae31eb52012-05-17 14:54:15 +0300318 gmdate('H', $time),
319 gmdate('i', $time),
320 gmdate('s', $time),
321 gmdate('m', $time),
322 gmdate('d', $time),
323 gmdate('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
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 $time = str_replace('-', '', $time);
379 $time = str_replace(':', '', $time);
380 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500383 return mktime(
384 substr($time, 8, 2),
385 substr($time, 10, 2),
386 substr($time, 12, 2),
387 substr($time, 4, 2),
388 substr($time, 6, 2),
389 substr($time, 0, 4)
390 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
392}
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394// ------------------------------------------------------------------------
395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396if ( ! function_exists('unix_to_human'))
397{
Timothy Warren01b129a2012-04-27 11:36:50 -0400398 /**
399 * Unix to "Human"
400 *
401 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
402 *
403 * @param int Unix timestamp
404 * @param bool whether to show seconds
405 * @param string format: us or euro
406 * @return string
407 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
409 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500410 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200411
Alex Bilbie773ccc32012-06-02 11:11:08 +0100412 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 $r .= date('h', $time).':'.date('i', $time);
415 }
416 else
417 {
418 $r .= date('H', $time).':'.date('i', $time);
419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 if ($seconds)
422 {
423 $r .= ':'.date('s', $time);
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Alex Bilbie773ccc32012-06-02 11:11:08 +0100426 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
428 $r .= ' '.date('A', $time);
429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 return $r;
432 }
433}
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435// ------------------------------------------------------------------------
436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437if ( ! function_exists('human_to_unix'))
438{
Timothy Warren01b129a2012-04-27 11:36:50 -0400439 /**
440 * Convert "human" date to GMT
441 *
442 * Reverses the above process
443 *
444 * @param string format: us or euro
445 * @return int
446 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 function human_to_unix($datestr = '')
448 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100449 if ($datestr === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
451 return FALSE;
452 }
Barry Mienydd671972010-10-04 16:33:58 +0200453
Andrey Andreevae31eb52012-05-17 14:54:15 +0300454 $datestr = preg_replace('/\040+/', ' ', trim($datestr));
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 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))
457 {
458 return FALSE;
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Jones36591092010-03-05 10:05:51 -0600461 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000462
Andrey Andreevae31eb52012-05-17 14:54:15 +0300463 $ex = explode('-', $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200464
Andrey Andreevae31eb52012-05-17 14:54:15 +0300465 $year = (strlen($ex[0]) === 2) ? '20'.$ex[0] : $ex[0];
466 $month = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
467 $day = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000468
Andrey Andreevae31eb52012-05-17 14:54:15 +0300469 $ex = explode(':', $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200470
Andrey Andreevae31eb52012-05-17 14:54:15 +0300471 $hour = (strlen($ex[0]) === 1) ? '0'.$ex[0] : $ex[0];
472 $min = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000473
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300474 if (isset($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300476 $sec = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 }
478 else
479 {
480 // Unless specified, seconds get set to zero.
481 $sec = '00';
482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300484 if (isset($split[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300486 $ampm = strtolower($split[2]);
Barry Mienydd671972010-10-04 16:33:58 +0200487
Andrey Andreeve92df332012-03-26 22:44:20 +0300488 if (substr($ampm, 0, 1) === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500489 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300490 $hour += 12;
Greg Akerf9168392011-08-29 19:29:05 -0500491 }
Barry Mienydd671972010-10-04 16:33:58 +0200492
Andrey Andreeve92df332012-03-26 22:44:20 +0300493 if (substr($ampm, 0, 1) === 'a' && $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500494 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500495 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500496 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500497
Andrey Andreevae31eb52012-05-17 14:54:15 +0300498 if (strlen($hour) === 1)
Greg Akerf9168392011-08-29 19:29:05 -0500499 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500500 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500501 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 return mktime($hour, $min, $sec, $month, $day, $year);
505 }
506}
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508// ------------------------------------------------------------------------
509
Kyle Farris896d95a2011-08-21 23:03:54 -0300510if ( ! function_exists('nice_date'))
511{
Timothy Warren01b129a2012-04-27 11:36:50 -0400512 /**
513 * Turns many "reasonably-date-like" strings into something
514 * that is actually useful. This only works for dates after unix epoch.
515 *
516 * @param string The terribly formatted date-like string
517 * @param string Date format to return (same as php date function)
518 * @return string
519 */
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500520 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300521 {
522 if (empty($bad_date))
523 {
524 return 'Unknown';
525 }
Greg Akerf9168392011-08-29 19:29:05 -0500526
Kyle Farris896d95a2011-08-21 23:03:54 -0300527 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500528 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300529 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300530 if (in_array(substr($bad_date, 0, 2), array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300531 {
532 $year = substr($bad_date, 0, 4);
533 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500534 }
535 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300536 {
537 $month = substr($bad_date, 0, 2);
538 $year = substr($bad_date, 2, 4);
539 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500540
Andrey Andreevae31eb52012-05-17 14:54:15 +0300541 return date($format, strtotime($year.'-'.$month.'-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300542 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500543
Kyle Farris896d95a2011-08-21 23:03:54 -0300544 // Date Like: YYYYMMDD
Andrey Andreevae31eb52012-05-17 14:54:15 +0300545 if (preg_match('/^\d{8}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300546 {
547 $month = substr($bad_date, 0, 2);
548 $day = substr($bad_date, 2, 2);
549 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500550
Andrey Andreevae31eb52012-05-17 14:54:15 +0300551 return date($format, strtotime($month.'/01/'.$year));
Kyle Farris896d95a2011-08-21 23:03:54 -0300552 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500553
Kyle Farris896d95a2011-08-21 23:03:54 -0300554 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
Andrey Andreevae31eb52012-05-17 14:54:15 +0300555 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/', $bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500556 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300557 list($m, $d, $y) = explode('-', $bad_date);
Andrey Andreevae31eb52012-05-17 14:54:15 +0300558 return date($format, strtotime($y.'-'.$m.'-'.$d));
Kyle Farris896d95a2011-08-21 23:03:54 -0300559 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500560
Kyle Farris896d95a2011-08-21 23:03:54 -0300561 // Any other kind of string, when converted into UNIX time,
562 // produces "0 seconds after epoc..." is probably bad...
563 // return "Invalid Date".
Alex Bilbie773ccc32012-06-02 11:11:08 +0100564 if (date('U', strtotime($bad_date)) === '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500565 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300566 return 'Invalid Date';
Kyle Farris896d95a2011-08-21 23:03:54 -0300567 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500568
Kyle Farris896d95a2011-08-21 23:03:54 -0300569 // It's probably a valid-ish date format already
570 return date($format, strtotime($bad_date));
571 }
572}
573
574// ------------------------------------------------------------------------
575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576if ( ! function_exists('timezone_menu'))
577{
Timothy Warren01b129a2012-04-27 11:36:50 -0400578 /**
579 * Timezone Menu
580 *
581 * Generates a drop-down menu of timezones.
582 *
583 * @param string timezone
584 * @param string classname
585 * @param string menu name
586 * @return string
587 */
Andrey Andreevae31eb52012-05-17 14:54:15 +0300588 function timezone_menu($default = 'UTC', $class = '', $name = 'timezones')
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
590 $CI =& get_instance();
591 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200592
Alex Bilbie773ccc32012-06-02 11:11:08 +0100593 $default = ($default === 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000594
595 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200596
Alex Bilbie773ccc32012-06-02 11:11:08 +0100597 if ($class !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
599 $menu .= ' class="'.$class.'"';
600 }
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 foreach (timezones() as $key => $val)
605 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100606 $selected = ($default === $key) ? ' selected="selected"' : '';
Andrey Andreevae31eb52012-05-17 14:54:15 +0300607 $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 }
609
Andrey Andreevae31eb52012-05-17 14:54:15 +0300610 return $menu.'</select>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 }
612}
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614// ------------------------------------------------------------------------
615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616if ( ! function_exists('timezones'))
617{
Timothy Warren01b129a2012-04-27 11:36:50 -0400618 /**
619 * Timezones
620 *
621 * Returns an array of timezones. This is a helper function
622 * for various other ones in this library
623 *
624 * @param string timezone
625 * @return string
626 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 function timezones($tz = '')
628 {
629 // Note: Don't change the order of these even though
630 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200631
632 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500633 'UM12' => -12,
634 'UM11' => -11,
635 'UM10' => -10,
636 'UM95' => -9.5,
637 'UM9' => -9,
638 'UM8' => -8,
639 'UM7' => -7,
640 'UM6' => -6,
641 'UM5' => -5,
642 'UM45' => -4.5,
643 'UM4' => -4,
644 'UM35' => -3.5,
645 'UM3' => -3,
646 'UM2' => -2,
647 'UM1' => -1,
648 'UTC' => 0,
649 'UP1' => +1,
650 'UP2' => +2,
651 'UP3' => +3,
652 'UP35' => +3.5,
653 'UP4' => +4,
654 'UP45' => +4.5,
655 'UP5' => +5,
656 'UP55' => +5.5,
657 'UP575' => +5.75,
658 'UP6' => +6,
659 'UP65' => +6.5,
660 'UP7' => +7,
661 'UP8' => +8,
662 'UP875' => +8.75,
663 'UP9' => +9,
664 'UP95' => +9.5,
665 'UP10' => +10,
666 'UP105' => +10.5,
667 'UP11' => +11,
668 'UP115' => +11.5,
669 'UP12' => +12,
670 'UP1275' => +12.75,
671 'UP13' => +13,
672 'UP14' => +14
673 );
Barry Mienydd671972010-10-04 16:33:58 +0200674
Alex Bilbie773ccc32012-06-02 11:11:08 +0100675 if ($tz === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
677 return $zones;
678 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500679
Alex Bilbie773ccc32012-06-02 11:11:08 +0100680 $tz = ($tz === 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500681
Andrey Andreeve92df332012-03-26 22:44:20 +0300682 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 }
684}
685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000687/* Location: ./system/helpers/date_helper.php */