blob: 3b0c3289d8569d43573d223b223752c924610f9f [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 Eguia83105952012-03-27 18:18:15 +020060 $timezone = new DateTimeZone($timezone);
61 $now = new DateTime('now', $timezone);
62 $offset = $timezone->getOffset($now);
63 $time = time() + $offset;
Barry Mienydd671972010-10-04 16:33:58 +020064
Iban Eguia83105952012-03-27 18:18:15 +020065 return $time;
Derek Allard2067d1a2008-11-13 22:59:24 +000066 }
67}
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069// ------------------------------------------------------------------------
70
Derek Allard2067d1a2008-11-13 22:59:24 +000071if ( ! function_exists('mdate'))
72{
Timothy Warren01b129a2012-04-27 11:36:50 -040073 /**
74 * Convert MySQL Style Datecodes
75 *
76 * This function is identical to PHPs date() function,
77 * except that it allows date codes to be formatted using
78 * the MySQL style, where each code letter is preceded
79 * with a percent sign: %Y %m %d etc...
80 *
81 * The benefit of doing dates this way is that you don't
82 * have to worry about escaping your text letters that
83 * match the date codes.
84 *
85 * @param string
86 * @param int
87 * @return int
88 */
Derek Allard2067d1a2008-11-13 22:59:24 +000089 function mdate($datestr = '', $time = '')
90 {
Alex Bilbie773ccc32012-06-02 11:11:08 +010091 if ($datestr === '')
Greg Akerf9168392011-08-29 19:29:05 -050092 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -050093 return '';
Greg Akerf9168392011-08-29 19:29:05 -050094 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Alex Bilbie773ccc32012-06-02 11:11:08 +010096 $time = ($time === '') ? now() : $time;
Barry Mienydd671972010-10-04 16:33:58 +020097
Greg Akerf9168392011-08-29 19:29:05 -050098 $datestr = str_replace(
Eric Barnesdc3e4be2011-12-19 13:48:29 -050099 '%\\',
100 '',
Andrey Andreevae31eb52012-05-17 14:54:15 +0300101 preg_replace('/([a-z]+?){1}/i', '\\\\\\1', $datestr)
Greg Akerf9168392011-08-29 19:29:05 -0500102 );
Greg Akerc964e722011-08-29 19:31:29 -0500103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 return date($datestr, $time);
105 }
106}
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108// ------------------------------------------------------------------------
109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110if ( ! function_exists('standard_date'))
111{
Timothy Warren01b129a2012-04-27 11:36:50 -0400112 /**
113 * Standard Date
114 *
115 * Returns a date formatted according to the submitted standard.
116 *
117 * @param string the chosen format
118 * @param int Unix timestamp
119 * @return string
120 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 function standard_date($fmt = 'DATE_RFC822', $time = '')
122 {
123 $formats = array(
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400124 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400126 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
Syahril Zulkefli90658ad2011-11-13 23:43:37 +0800128 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC',
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
130 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400131 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400133 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O'
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 );
135
136 if ( ! isset($formats[$fmt]))
137 {
138 return FALSE;
139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 return mdate($formats[$fmt], $time);
142 }
143}
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145// ------------------------------------------------------------------------
146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147if ( ! function_exists('timespan'))
148{
Timothy Warren01b129a2012-04-27 11:36:50 -0400149 /**
150 * Timespan
151 *
152 * Returns a span of seconds in this format:
153 * 10 days 14 hours 36 minutes 47 seconds
154 *
155 * @param int a number of seconds
156 * @param int Unix timestamp
157 * @param int a number of display units
158 * @return string
159 */
Roger Herbert8d69aa12012-03-14 08:44:55 +0000160 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 $CI =& get_instance();
163 $CI->lang->load('date');
164
165 if ( ! is_numeric($seconds))
166 {
167 $seconds = 1;
168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 if ( ! is_numeric($time))
171 {
172 $time = time();
173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Roger Herbert04c146d2012-03-11 15:31:01 +0000175 if ( ! is_numeric($units))
176 {
Roger Herbert8d69aa12012-03-14 08:44:55 +0000177 $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000178 }
179
Greg Akerf9168392011-08-29 19:29:05 -0500180 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200181
Roger Herbert04c146d2012-03-11 15:31:01 +0000182 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500183 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200186 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000187 $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
Barry Mienydd671972010-10-04 16:33:58 +0200188 }
189
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500190 $seconds -= $years * 31557600;
191 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200192
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000193 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
195 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200196 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000197 $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month'));
Barry Mienydd671972010-10-04 16:33:58 +0200198 }
199
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500200 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
202
203 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200204
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000205 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
207 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200208 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000209 $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200213 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000214
215 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200216
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000217 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
219 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200220 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000221 $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 $seconds -= $days * 86400;
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200228
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000229 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
231 if ($hours > 0)
232 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000233 $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 $seconds -= $hours * 3600;
237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200240
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000241 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
243 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200244 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000245 $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 $seconds -= $minutes * 60;
249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Roger Herbert597eb212012-03-14 09:06:17 +0000251 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000253 $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Roger Herbert04c146d2012-03-11 15:31:01 +0000256 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
258}
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260// ------------------------------------------------------------------------
261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262if ( ! function_exists('days_in_month'))
263{
Timothy Warren01b129a2012-04-27 11:36:50 -0400264 /**
265 * Number of days in a month
266 *
267 * Takes a month/year as input and returns the number of days
268 * for the given month/year. Takes leap years into consideration.
269 *
270 * @param int a numeric month
271 * @param int a numeric year
272 * @return int
273 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 function days_in_month($month = 0, $year = '')
275 {
276 if ($month < 1 OR $month > 12)
277 {
278 return 0;
279 }
Barry Mienydd671972010-10-04 16:33:58 +0200280
Alex Bilbie773ccc32012-06-02 11:11:08 +0100281 if ( ! is_numeric($year) OR strlen($year) !== 4)
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
283 $year = date('Y');
284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 if ($month == 2)
287 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100288 if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
290 return 29;
291 }
292 }
293
294 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
295 return $days_in_month[$month - 1];
296 }
297}
Derek Jones36591092010-03-05 10:05:51 -0600298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299// ------------------------------------------------------------------------
300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301if ( ! function_exists('local_to_gmt'))
302{
Timothy Warren01b129a2012-04-27 11:36:50 -0400303 /**
304 * Converts a local Unix timestamp to GMT
305 *
306 * @param int Unix timestamp
307 * @return int
308 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 function local_to_gmt($time = '')
310 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100311 if ($time === '')
Greg Akerf9168392011-08-29 19:29:05 -0500312 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500314 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500315
Greg Akerf9168392011-08-29 19:29:05 -0500316 return mktime(
Andrey Andreevae31eb52012-05-17 14:54:15 +0300317 gmdate('H', $time),
318 gmdate('i', $time),
319 gmdate('s', $time),
320 gmdate('m', $time),
321 gmdate('d', $time),
322 gmdate('Y', $time)
Greg Akerf9168392011-08-29 19:29:05 -0500323 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
325}
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327// ------------------------------------------------------------------------
328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329if ( ! function_exists('gmt_to_local'))
330{
Timothy Warren01b129a2012-04-27 11:36:50 -0400331 /**
332 * Converts GMT time to a localized value
333 *
334 * Takes a Unix timestamp (in GMT) as input, and returns
335 * at the local value based on the timezone and DST setting
336 * submitted
337 *
338 * @param int Unix timestamp
339 * @param string timezone
340 * @param bool whether DST is active
341 * @return int
342 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200344 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100345 if ($time === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
347 return now();
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 $time += timezones($timezone) * 3600;
351
Alex Bilbie773ccc32012-06-02 11:11:08 +0100352 if ($dst === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
354 $time += 3600;
355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 return $time;
358 }
359}
Derek Jones36591092010-03-05 10:05:51 -0600360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361// ------------------------------------------------------------------------
362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363if ( ! function_exists('mysql_to_unix'))
364{
Timothy Warren01b129a2012-04-27 11:36:50 -0400365 /**
366 * Converts a MySQL Timestamp to Unix
367 *
368 * @param int Unix timestamp
369 * @return int
370 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 function mysql_to_unix($time = '')
372 {
373 // We'll remove certain characters for backward compatibility
374 // since the formatting changed with MySQL 4.1
375 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 $time = str_replace('-', '', $time);
378 $time = str_replace(':', '', $time);
379 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500382 return mktime(
383 substr($time, 8, 2),
384 substr($time, 10, 2),
385 substr($time, 12, 2),
386 substr($time, 4, 2),
387 substr($time, 6, 2),
388 substr($time, 0, 4)
389 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
391}
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393// ------------------------------------------------------------------------
394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395if ( ! function_exists('unix_to_human'))
396{
Timothy Warren01b129a2012-04-27 11:36:50 -0400397 /**
398 * Unix to "Human"
399 *
400 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
401 *
402 * @param int Unix timestamp
403 * @param bool whether to show seconds
404 * @param string format: us or euro
405 * @return string
406 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
408 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500409 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200410
Alex Bilbie773ccc32012-06-02 11:11:08 +0100411 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
413 $r .= date('h', $time).':'.date('i', $time);
414 }
415 else
416 {
417 $r .= date('H', $time).':'.date('i', $time);
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 if ($seconds)
421 {
422 $r .= ':'.date('s', $time);
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Alex Bilbie773ccc32012-06-02 11:11:08 +0100425 if ($fmt === 'us')
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 $r .= ' '.date('A', $time);
428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 return $r;
431 }
432}
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434// ------------------------------------------------------------------------
435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436if ( ! function_exists('human_to_unix'))
437{
Timothy Warren01b129a2012-04-27 11:36:50 -0400438 /**
439 * Convert "human" date to GMT
440 *
441 * Reverses the above process
442 *
443 * @param string format: us or euro
444 * @return int
445 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 function human_to_unix($datestr = '')
447 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100448 if ($datestr === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
450 return FALSE;
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Andrey Andreevae31eb52012-05-17 14:54:15 +0300453 $datestr = preg_replace('/\040+/', ' ', trim($datestr));
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 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))
456 {
457 return FALSE;
458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Jones36591092010-03-05 10:05:51 -0600460 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000461
Andrey Andreevae31eb52012-05-17 14:54:15 +0300462 $ex = explode('-', $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200463
Andrey Andreevae31eb52012-05-17 14:54:15 +0300464 $year = (strlen($ex[0]) === 2) ? '20'.$ex[0] : $ex[0];
465 $month = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
466 $day = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000467
Andrey Andreevae31eb52012-05-17 14:54:15 +0300468 $ex = explode(':', $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200469
Andrey Andreevae31eb52012-05-17 14:54:15 +0300470 $hour = (strlen($ex[0]) === 1) ? '0'.$ex[0] : $ex[0];
471 $min = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000472
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300473 if (isset($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300475 $sec = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477 else
478 {
479 // Unless specified, seconds get set to zero.
480 $sec = '00';
481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300483 if (isset($split[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300485 $ampm = strtolower($split[2]);
Barry Mienydd671972010-10-04 16:33:58 +0200486
Andrey Andreeve92df332012-03-26 22:44:20 +0300487 if (substr($ampm, 0, 1) === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500488 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300489 $hour += 12;
Greg Akerf9168392011-08-29 19:29:05 -0500490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Andrey Andreeve92df332012-03-26 22:44:20 +0300492 if (substr($ampm, 0, 1) === 'a' && $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500493 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500494 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500495 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500496
Andrey Andreevae31eb52012-05-17 14:54:15 +0300497 if (strlen($hour) === 1)
Greg Akerf9168392011-08-29 19:29:05 -0500498 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500499 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500500 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 return mktime($hour, $min, $sec, $month, $day, $year);
504 }
505}
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507// ------------------------------------------------------------------------
508
Kyle Farris896d95a2011-08-21 23:03:54 -0300509if ( ! function_exists('nice_date'))
510{
Timothy Warren01b129a2012-04-27 11:36:50 -0400511 /**
512 * Turns many "reasonably-date-like" strings into something
513 * that is actually useful. This only works for dates after unix epoch.
514 *
515 * @param string The terribly formatted date-like string
516 * @param string Date format to return (same as php date function)
517 * @return string
518 */
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500519 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300520 {
521 if (empty($bad_date))
522 {
523 return 'Unknown';
524 }
Greg Akerf9168392011-08-29 19:29:05 -0500525
Kyle Farris896d95a2011-08-21 23:03:54 -0300526 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500527 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300528 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300529 if (in_array(substr($bad_date, 0, 2), array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300530 {
531 $year = substr($bad_date, 0, 4);
532 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500533 }
534 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300535 {
536 $month = substr($bad_date, 0, 2);
537 $year = substr($bad_date, 2, 4);
538 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500539
Andrey Andreevae31eb52012-05-17 14:54:15 +0300540 return date($format, strtotime($year.'-'.$month.'-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300541 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500542
Kyle Farris896d95a2011-08-21 23:03:54 -0300543 // Date Like: YYYYMMDD
Andrey Andreevae31eb52012-05-17 14:54:15 +0300544 if (preg_match('/^\d{8}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300545 {
546 $month = substr($bad_date, 0, 2);
547 $day = substr($bad_date, 2, 2);
548 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500549
Andrey Andreevae31eb52012-05-17 14:54:15 +0300550 return date($format, strtotime($month.'/01/'.$year));
Kyle Farris896d95a2011-08-21 23:03:54 -0300551 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500552
Kyle Farris896d95a2011-08-21 23:03:54 -0300553 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
Andrey Andreevae31eb52012-05-17 14:54:15 +0300554 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/', $bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500555 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300556 list($m, $d, $y) = explode('-', $bad_date);
Andrey Andreevae31eb52012-05-17 14:54:15 +0300557 return date($format, strtotime($y.'-'.$m.'-'.$d));
Kyle Farris896d95a2011-08-21 23:03:54 -0300558 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500559
Kyle Farris896d95a2011-08-21 23:03:54 -0300560 // Any other kind of string, when converted into UNIX time,
561 // produces "0 seconds after epoc..." is probably bad...
562 // return "Invalid Date".
Alex Bilbie773ccc32012-06-02 11:11:08 +0100563 if (date('U', strtotime($bad_date)) === '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500564 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300565 return 'Invalid Date';
Kyle Farris896d95a2011-08-21 23:03:54 -0300566 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500567
Kyle Farris896d95a2011-08-21 23:03:54 -0300568 // It's probably a valid-ish date format already
569 return date($format, strtotime($bad_date));
570 }
571}
572
573// ------------------------------------------------------------------------
574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575if ( ! function_exists('timezone_menu'))
576{
Timothy Warren01b129a2012-04-27 11:36:50 -0400577 /**
578 * Timezone Menu
579 *
580 * Generates a drop-down menu of timezones.
581 *
582 * @param string timezone
583 * @param string classname
584 * @param string menu name
585 * @return string
586 */
Andrey Andreevae31eb52012-05-17 14:54:15 +0300587 function timezone_menu($default = 'UTC', $class = '', $name = 'timezones')
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 {
589 $CI =& get_instance();
590 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200591
Alex Bilbie773ccc32012-06-02 11:11:08 +0100592 $default = ($default === 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000593
594 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200595
Alex Bilbie773ccc32012-06-02 11:11:08 +0100596 if ($class !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
598 $menu .= ' class="'.$class.'"';
599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 foreach (timezones() as $key => $val)
604 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100605 $selected = ($default === $key) ? ' selected="selected"' : '';
Andrey Andreevae31eb52012-05-17 14:54:15 +0300606 $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 }
608
Andrey Andreevae31eb52012-05-17 14:54:15 +0300609 return $menu.'</select>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 }
611}
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613// ------------------------------------------------------------------------
614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615if ( ! function_exists('timezones'))
616{
Timothy Warren01b129a2012-04-27 11:36:50 -0400617 /**
618 * Timezones
619 *
620 * Returns an array of timezones. This is a helper function
621 * for various other ones in this library
622 *
623 * @param string timezone
624 * @return string
625 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 function timezones($tz = '')
627 {
628 // Note: Don't change the order of these even though
629 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200630
631 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500632 'UM12' => -12,
633 'UM11' => -11,
634 'UM10' => -10,
635 'UM95' => -9.5,
636 'UM9' => -9,
637 'UM8' => -8,
638 'UM7' => -7,
639 'UM6' => -6,
640 'UM5' => -5,
641 'UM45' => -4.5,
642 'UM4' => -4,
643 'UM35' => -3.5,
644 'UM3' => -3,
645 'UM2' => -2,
646 'UM1' => -1,
647 'UTC' => 0,
648 'UP1' => +1,
649 'UP2' => +2,
650 'UP3' => +3,
651 'UP35' => +3.5,
652 'UP4' => +4,
653 'UP45' => +4.5,
654 'UP5' => +5,
655 'UP55' => +5.5,
656 'UP575' => +5.75,
657 'UP6' => +6,
658 'UP65' => +6.5,
659 'UP7' => +7,
660 'UP8' => +8,
661 'UP875' => +8.75,
662 'UP9' => +9,
663 'UP95' => +9.5,
664 'UP10' => +10,
665 'UP105' => +10.5,
666 'UP11' => +11,
667 'UP115' => +11.5,
668 'UP12' => +12,
669 'UP1275' => +12.75,
670 'UP13' => +13,
671 'UP14' => +14
672 );
Barry Mienydd671972010-10-04 16:33:58 +0200673
Alex Bilbie773ccc32012-06-02 11:11:08 +0100674 if ($tz === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 {
676 return $zones;
677 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500678
Alex Bilbie773ccc32012-06-02 11:11:08 +0100679 $tz = ($tz === 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500680
Andrey Andreeve92df332012-03-26 22:44:20 +0300681 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 }
683}
684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000686/* Location: ./system/helpers/date_helper.php */