blob: d5acec23fa2891cad4aa674679ae0ee989f02398 [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 Eguia895e98c2012-06-08 23:01:31 +020045 * Returns time() based on the timezone parameter or on the "timezone"
46 * 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 Eguia83105952012-03-27 18:18:15 +020053 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020054
Iban Eguia83105952012-03-27 18:18:15 +020055 if (is_null($timezone))
Iban Eguia16760bb2012-03-27 18:51:52 +020056 {
Iban Eguia83105952012-03-27 18:18:15 +020057 $timezone = $CI->config->item('timezone');
Iban Eguia16760bb2012-03-27 18:51:52 +020058 }
Barry Mienydd671972010-10-04 16:33:58 +020059
Iban Eguiae15e3dd2012-06-09 23:52:27 +020060 $time = time();
61 if(strtolower($timezone) != 'local')
62 {
63 $local = new DateTime(NULL, new DateTimeZone(date_default_timezone_get()));
64 $now = new DateTime(NULL, new DateTimeZone($timezone));
65 $lcl_offset = $local->getOffset();
66 $tz_offset = $now->getOffset();
67 $offset = $tz_offset - $lcl_offset;
68 $time = $time + $offset;
69 }
Barry Mienydd671972010-10-04 16:33:58 +020070
Iban Eguia83105952012-03-27 18:18:15 +020071 return $time;
Derek Allard2067d1a2008-11-13 22:59:24 +000072 }
73}
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075// ------------------------------------------------------------------------
76
Derek Allard2067d1a2008-11-13 22:59:24 +000077if ( ! function_exists('mdate'))
78{
Timothy Warren01b129a2012-04-27 11:36:50 -040079 /**
80 * Convert MySQL Style Datecodes
81 *
82 * This function is identical to PHPs date() function,
83 * except that it allows date codes to be formatted using
84 * the MySQL style, where each code letter is preceded
85 * with a percent sign: %Y %m %d etc...
86 *
87 * The benefit of doing dates this way is that you don't
88 * have to worry about escaping your text letters that
89 * match the date codes.
90 *
91 * @param string
92 * @param int
93 * @return int
94 */
Derek Allard2067d1a2008-11-13 22:59:24 +000095 function mdate($datestr = '', $time = '')
96 {
Alex Bilbie773ccc32012-06-02 11:11:08 +010097 if ($datestr === '')
Greg Akerf9168392011-08-29 19:29:05 -050098 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -050099 return '';
Greg Akerf9168392011-08-29 19:29:05 -0500100 }
Barry Mienydd671972010-10-04 16:33:58 +0200101
Alex Bilbie773ccc32012-06-02 11:11:08 +0100102 $time = ($time === '') ? now() : $time;
Barry Mienydd671972010-10-04 16:33:58 +0200103
Greg Akerf9168392011-08-29 19:29:05 -0500104 $datestr = str_replace(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500105 '%\\',
106 '',
Andrey Andreevae31eb52012-05-17 14:54:15 +0300107 preg_replace('/([a-z]+?){1}/i', '\\\\\\1', $datestr)
Greg Akerf9168392011-08-29 19:29:05 -0500108 );
Greg Akerc964e722011-08-29 19:31:29 -0500109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 return date($datestr, $time);
111 }
112}
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114// ------------------------------------------------------------------------
115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116if ( ! function_exists('standard_date'))
117{
Timothy Warren01b129a2012-04-27 11:36:50 -0400118 /**
119 * Standard Date
120 *
121 * Returns a date formatted according to the submitted standard.
122 *
123 * @param string the chosen format
124 * @param int Unix timestamp
125 * @return string
126 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 function standard_date($fmt = 'DATE_RFC822', $time = '')
128 {
129 $formats = array(
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400130 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400132 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
Syahril Zulkefli90658ad2011-11-13 23:43:37 +0800134 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC',
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
136 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400137 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400139 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O'
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 );
141
142 if ( ! isset($formats[$fmt]))
143 {
144 return FALSE;
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 return mdate($formats[$fmt], $time);
148 }
149}
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151// ------------------------------------------------------------------------
152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153if ( ! function_exists('timespan'))
154{
Timothy Warren01b129a2012-04-27 11:36:50 -0400155 /**
156 * Timespan
157 *
158 * Returns a span of seconds in this format:
159 * 10 days 14 hours 36 minutes 47 seconds
160 *
161 * @param int a number of seconds
162 * @param int Unix timestamp
163 * @param int a number of display units
164 * @return string
165 */
Roger Herbert8d69aa12012-03-14 08:44:55 +0000166 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
168 $CI =& get_instance();
169 $CI->lang->load('date');
170
171 if ( ! is_numeric($seconds))
172 {
173 $seconds = 1;
174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 if ( ! is_numeric($time))
177 {
178 $time = time();
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Roger Herbert04c146d2012-03-11 15:31:01 +0000181 if ( ! is_numeric($units))
182 {
Roger Herbert8d69aa12012-03-14 08:44:55 +0000183 $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000184 }
185
Greg Akerf9168392011-08-29 19:29:05 -0500186 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200187
Roger Herbert04c146d2012-03-11 15:31:01 +0000188 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500189 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200192 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000193 $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
Barry Mienydd671972010-10-04 16:33:58 +0200194 }
195
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500196 $seconds -= $years * 31557600;
197 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200198
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000199 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
201 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200202 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000203 $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month'));
Barry Mienydd671972010-10-04 16:33:58 +0200204 }
205
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500206 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
208
209 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200210
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000211 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
213 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200214 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000215 $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200219 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000220
221 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200222
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000223 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
225 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200226 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000227 $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 $seconds -= $days * 86400;
231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200234
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000235 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
237 if ($hours > 0)
238 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000239 $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 $seconds -= $hours * 3600;
243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200246
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000247 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
249 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200250 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000251 $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 $seconds -= $minutes * 60;
255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Roger Herbert597eb212012-03-14 09:06:17 +0000257 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000259 $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Roger Herbert04c146d2012-03-11 15:31:01 +0000262 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
264}
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266// ------------------------------------------------------------------------
267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268if ( ! function_exists('days_in_month'))
269{
Timothy Warren01b129a2012-04-27 11:36:50 -0400270 /**
271 * Number of days in a month
272 *
273 * Takes a month/year as input and returns the number of days
274 * for the given month/year. Takes leap years into consideration.
275 *
276 * @param int a numeric month
277 * @param int a numeric year
278 * @return int
279 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 function days_in_month($month = 0, $year = '')
281 {
282 if ($month < 1 OR $month > 12)
283 {
284 return 0;
285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Alex Bilbie773ccc32012-06-02 11:11:08 +0100287 if ( ! is_numeric($year) OR strlen($year) !== 4)
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 $year = date('Y');
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 if ($month == 2)
293 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100294 if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
296 return 29;
297 }
298 }
299
300 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
301 return $days_in_month[$month - 1];
302 }
303}
Derek Jones36591092010-03-05 10:05:51 -0600304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305// ------------------------------------------------------------------------
306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307if ( ! function_exists('local_to_gmt'))
308{
Timothy Warren01b129a2012-04-27 11:36:50 -0400309 /**
310 * Converts a local Unix timestamp to GMT
311 *
312 * @param int Unix timestamp
313 * @return int
314 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 function local_to_gmt($time = '')
316 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100317 if ($time === '')
Greg Akerf9168392011-08-29 19:29:05 -0500318 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500320 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500321
Greg Akerf9168392011-08-29 19:29:05 -0500322 return mktime(
Andrey Andreevae31eb52012-05-17 14:54:15 +0300323 gmdate('H', $time),
324 gmdate('i', $time),
325 gmdate('s', $time),
326 gmdate('m', $time),
327 gmdate('d', $time),
328 gmdate('Y', $time)
Greg Akerf9168392011-08-29 19:29:05 -0500329 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
331}
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333// ------------------------------------------------------------------------
334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335if ( ! function_exists('gmt_to_local'))
336{
Timothy Warren01b129a2012-04-27 11:36:50 -0400337 /**
338 * Converts GMT time to a localized value
339 *
340 * Takes a Unix timestamp (in GMT) as input, and returns
341 * at the local value based on the timezone and DST setting
342 * submitted
343 *
344 * @param int Unix timestamp
345 * @param string timezone
346 * @param bool whether DST is active
347 * @return int
348 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200350 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100351 if ($time === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
353 return now();
354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 $time += timezones($timezone) * 3600;
357
Alex Bilbie773ccc32012-06-02 11:11:08 +0100358 if ($dst === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 $time += 3600;
361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 return $time;
364 }
365}
Derek Jones36591092010-03-05 10:05:51 -0600366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367// ------------------------------------------------------------------------
368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369if ( ! function_exists('mysql_to_unix'))
370{
Timothy Warren01b129a2012-04-27 11:36:50 -0400371 /**
372 * Converts a MySQL Timestamp to Unix
373 *
374 * @param int Unix timestamp
375 * @return int
376 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 function mysql_to_unix($time = '')
378 {
379 // We'll remove certain characters for backward compatibility
380 // since the formatting changed with MySQL 4.1
381 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 $time = str_replace('-', '', $time);
384 $time = str_replace(':', '', $time);
385 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500388 return mktime(
389 substr($time, 8, 2),
390 substr($time, 10, 2),
391 substr($time, 12, 2),
392 substr($time, 4, 2),
393 substr($time, 6, 2),
394 substr($time, 0, 4)
395 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
397}
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399// ------------------------------------------------------------------------
400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401if ( ! function_exists('unix_to_human'))
402{
Timothy Warren01b129a2012-04-27 11:36:50 -0400403 /**
404 * Unix to "Human"
405 *
406 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
407 *
408 * @param int Unix timestamp
409 * @param bool whether to show seconds
410 * @param string format: us or euro
411 * @return string
412 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
414 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500415 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200416
Alex Bilbie773ccc32012-06-02 11:11:08 +0100417 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
419 $r .= date('h', $time).':'.date('i', $time);
420 }
421 else
422 {
423 $r .= date('H', $time).':'.date('i', $time);
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 if ($seconds)
427 {
428 $r .= ':'.date('s', $time);
429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Alex Bilbie773ccc32012-06-02 11:11:08 +0100431 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
433 $r .= ' '.date('A', $time);
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 return $r;
437 }
438}
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440// ------------------------------------------------------------------------
441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442if ( ! function_exists('human_to_unix'))
443{
Timothy Warren01b129a2012-04-27 11:36:50 -0400444 /**
445 * Convert "human" date to GMT
446 *
447 * Reverses the above process
448 *
449 * @param string format: us or euro
450 * @return int
451 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 function human_to_unix($datestr = '')
453 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100454 if ($datestr === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
456 return FALSE;
457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Andrey Andreevae31eb52012-05-17 14:54:15 +0300459 $datestr = preg_replace('/\040+/', ' ', trim($datestr));
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 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))
462 {
463 return FALSE;
464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Jones36591092010-03-05 10:05:51 -0600466 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000467
Andrey Andreevae31eb52012-05-17 14:54:15 +0300468 $ex = explode('-', $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200469
Andrey Andreevae31eb52012-05-17 14:54:15 +0300470 $year = (strlen($ex[0]) === 2) ? '20'.$ex[0] : $ex[0];
471 $month = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
472 $day = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000473
Andrey Andreevae31eb52012-05-17 14:54:15 +0300474 $ex = explode(':', $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200475
Andrey Andreevae31eb52012-05-17 14:54:15 +0300476 $hour = (strlen($ex[0]) === 1) ? '0'.$ex[0] : $ex[0];
477 $min = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000478
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300479 if (isset($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300481 $sec = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
483 else
484 {
485 // Unless specified, seconds get set to zero.
486 $sec = '00';
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300489 if (isset($split[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300491 $ampm = strtolower($split[2]);
Barry Mienydd671972010-10-04 16:33:58 +0200492
Andrey Andreeve92df332012-03-26 22:44:20 +0300493 if (substr($ampm, 0, 1) === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500494 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300495 $hour += 12;
Greg Akerf9168392011-08-29 19:29:05 -0500496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Andrey Andreeve92df332012-03-26 22:44:20 +0300498 if (substr($ampm, 0, 1) === 'a' && $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500499 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500500 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500501 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500502
Andrey Andreevae31eb52012-05-17 14:54:15 +0300503 if (strlen($hour) === 1)
Greg Akerf9168392011-08-29 19:29:05 -0500504 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500505 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500506 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 return mktime($hour, $min, $sec, $month, $day, $year);
510 }
511}
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513// ------------------------------------------------------------------------
514
Kyle Farris896d95a2011-08-21 23:03:54 -0300515if ( ! function_exists('nice_date'))
516{
Timothy Warren01b129a2012-04-27 11:36:50 -0400517 /**
518 * Turns many "reasonably-date-like" strings into something
519 * that is actually useful. This only works for dates after unix epoch.
520 *
521 * @param string The terribly formatted date-like string
522 * @param string Date format to return (same as php date function)
523 * @return string
524 */
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500525 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300526 {
527 if (empty($bad_date))
528 {
529 return 'Unknown';
530 }
Greg Akerf9168392011-08-29 19:29:05 -0500531
Kyle Farris896d95a2011-08-21 23:03:54 -0300532 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500533 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300534 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300535 if (in_array(substr($bad_date, 0, 2), array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300536 {
537 $year = substr($bad_date, 0, 4);
538 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500539 }
540 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300541 {
542 $month = substr($bad_date, 0, 2);
543 $year = substr($bad_date, 2, 4);
544 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500545
Andrey Andreevae31eb52012-05-17 14:54:15 +0300546 return date($format, strtotime($year.'-'.$month.'-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300547 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500548
Kyle Farris896d95a2011-08-21 23:03:54 -0300549 // Date Like: YYYYMMDD
Andrey Andreevae31eb52012-05-17 14:54:15 +0300550 if (preg_match('/^\d{8}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300551 {
552 $month = substr($bad_date, 0, 2);
553 $day = substr($bad_date, 2, 2);
554 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500555
Andrey Andreevae31eb52012-05-17 14:54:15 +0300556 return date($format, strtotime($month.'/01/'.$year));
Kyle Farris896d95a2011-08-21 23:03:54 -0300557 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500558
Kyle Farris896d95a2011-08-21 23:03:54 -0300559 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
Andrey Andreevae31eb52012-05-17 14:54:15 +0300560 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/', $bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500561 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300562 list($m, $d, $y) = explode('-', $bad_date);
Andrey Andreevae31eb52012-05-17 14:54:15 +0300563 return date($format, strtotime($y.'-'.$m.'-'.$d));
Kyle Farris896d95a2011-08-21 23:03:54 -0300564 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500565
Kyle Farris896d95a2011-08-21 23:03:54 -0300566 // Any other kind of string, when converted into UNIX time,
567 // produces "0 seconds after epoc..." is probably bad...
568 // return "Invalid Date".
Alex Bilbie773ccc32012-06-02 11:11:08 +0100569 if (date('U', strtotime($bad_date)) === '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500570 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300571 return 'Invalid Date';
Kyle Farris896d95a2011-08-21 23:03:54 -0300572 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500573
Kyle Farris896d95a2011-08-21 23:03:54 -0300574 // It's probably a valid-ish date format already
575 return date($format, strtotime($bad_date));
576 }
577}
578
579// ------------------------------------------------------------------------
580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581if ( ! function_exists('timezone_menu'))
582{
Timothy Warren01b129a2012-04-27 11:36:50 -0400583 /**
584 * Timezone Menu
585 *
586 * Generates a drop-down menu of timezones.
587 *
588 * @param string timezone
589 * @param string classname
590 * @param string menu name
591 * @return string
592 */
Andrey Andreevae31eb52012-05-17 14:54:15 +0300593 function timezone_menu($default = 'UTC', $class = '', $name = 'timezones')
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 $CI =& get_instance();
596 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200597
Alex Bilbie773ccc32012-06-02 11:11:08 +0100598 $default = ($default === 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000599
600 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200601
Alex Bilbie773ccc32012-06-02 11:11:08 +0100602 if ($class !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
604 $menu .= ' class="'.$class.'"';
605 }
Barry Mienydd671972010-10-04 16:33:58 +0200606
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200608
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 foreach (timezones() as $key => $val)
610 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100611 $selected = ($default === $key) ? ' selected="selected"' : '';
Andrey Andreevae31eb52012-05-17 14:54:15 +0300612 $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 }
614
Andrey Andreevae31eb52012-05-17 14:54:15 +0300615 return $menu.'</select>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 }
617}
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619// ------------------------------------------------------------------------
620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621if ( ! function_exists('timezones'))
622{
Timothy Warren01b129a2012-04-27 11:36:50 -0400623 /**
624 * Timezones
625 *
626 * Returns an array of timezones. This is a helper function
627 * for various other ones in this library
628 *
629 * @param string timezone
630 * @return string
631 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 function timezones($tz = '')
633 {
634 // Note: Don't change the order of these even though
635 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200636
637 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500638 'UM12' => -12,
639 'UM11' => -11,
640 'UM10' => -10,
641 'UM95' => -9.5,
642 'UM9' => -9,
643 'UM8' => -8,
644 'UM7' => -7,
645 'UM6' => -6,
646 'UM5' => -5,
647 'UM45' => -4.5,
648 'UM4' => -4,
649 'UM35' => -3.5,
650 'UM3' => -3,
651 'UM2' => -2,
652 'UM1' => -1,
653 'UTC' => 0,
654 'UP1' => +1,
655 'UP2' => +2,
656 'UP3' => +3,
657 'UP35' => +3.5,
658 'UP4' => +4,
659 'UP45' => +4.5,
660 'UP5' => +5,
661 'UP55' => +5.5,
662 'UP575' => +5.75,
663 'UP6' => +6,
664 'UP65' => +6.5,
665 'UP7' => +7,
666 'UP8' => +8,
667 'UP875' => +8.75,
668 'UP9' => +9,
669 'UP95' => +9.5,
670 'UP10' => +10,
671 'UP105' => +10.5,
672 'UP11' => +11,
673 'UP115' => +11.5,
674 'UP12' => +12,
675 'UP1275' => +12.75,
676 'UP13' => +13,
677 'UP14' => +14
678 );
Barry Mienydd671972010-10-04 16:33:58 +0200679
Alex Bilbie773ccc32012-06-02 11:11:08 +0100680 if ($tz === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 {
682 return $zones;
683 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500684
Alex Bilbie773ccc32012-06-02 11:11:08 +0100685 $tz = ($tz === 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500686
Andrey Andreeve92df332012-03-26 22:44:20 +0300687 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 }
689}
690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000692/* Location: ./system/helpers/date_helper.php */