blob: 14d973f6552055537d4d3c28b091ae45ff6e8657 [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
40/**
41 * Get "now" time
42 *
Iban Eguia7bf0a4f2012-03-27 18:36:15 +020043 * Returns time() based on the timezone parameter or on the "timezone"
44 * setting
Derek Allard2067d1a2008-11-13 22:59:24 +000045 *
Iban Eguia7bf0a4f2012-03-27 18:36:15 +020046 * @param string
Andrey Andreeve92df332012-03-26 22:44:20 +030047 * @return int
Barry Mienydd671972010-10-04 16:33:58 +020048 */
Derek Allard2067d1a2008-11-13 22:59:24 +000049if ( ! function_exists('now'))
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
71/**
72 * Convert MySQL Style Datecodes
73 *
74 * This function is identical to PHPs date() function,
75 * except that it allows date codes to be formatted using
76 * the MySQL style, where each code letter is preceded
Derek Jones4b9c6292011-07-01 17:40:48 -050077 * with a percent sign: %Y %m %d etc...
Derek Allard2067d1a2008-11-13 22:59:24 +000078 *
79 * The benefit of doing dates this way is that you don't
80 * have to worry about escaping your text letters that
81 * match the date codes.
82 *
Derek Allard2067d1a2008-11-13 22:59:24 +000083 * @param string
Andrey Andreeve92df332012-03-26 22:44:20 +030084 * @param int
85 * @return int
Barry Mienydd671972010-10-04 16:33:58 +020086 */
Derek Allard2067d1a2008-11-13 22:59:24 +000087if ( ! function_exists('mdate'))
88{
89 function mdate($datestr = '', $time = '')
90 {
91 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
Greg Akerc964e722011-08-29 19:31:29 -050096 $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 '',
Greg Akerf9168392011-08-29 19:29:05 -0500101 preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr)
102 );
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
110/**
111 * Standard Date
112 *
113 * Returns a date formatted according to the submitted standard.
114 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 * @param string the chosen format
Andrey Andreeve92df332012-03-26 22:44:20 +0300116 * @param int Unix timestamp
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200118 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000119if ( ! function_exists('standard_date'))
120{
121 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
147/**
148 * Timespan
149 *
150 * Returns a span of seconds in this format:
151 * 10 days 14 hours 36 minutes 47 seconds
152 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300153 * @param int a number of seconds
154 * @param int Unix timestamp
155 * @param int a number of display units
Roger Herbertb81f9092012-03-12 12:46:02 +0000156 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200157 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000158if ( ! function_exists('timespan'))
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
262/**
263 * Number of days in a month
264 *
265 * Takes a month/year as input and returns the number of days
266 * for the given month/year. Takes leap years into consideration.
267 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300268 * @param int a numeric month
269 * @param int a numeric year
270 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200271 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000272if ( ! function_exists('days_in_month'))
273{
274 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
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 if ( ! is_numeric($year) OR strlen($year) != 4)
282 {
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 {
Andrey Andreeve92df332012-03-26 22:44:20 +0300288 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
301/**
302 * Converts a local Unix timestamp to GMT
303 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300304 * @param int Unix timestamp
305 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200306 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000307if ( ! function_exists('local_to_gmt'))
308{
309 function local_to_gmt($time = '')
310 {
311 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(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500317 gmdate("H", $time),
318 gmdate("i", $time),
319 gmdate("s", $time),
320 gmdate("m", $time),
321 gmdate("d", $time),
Greg Akerf9168392011-08-29 19:29:05 -0500322 gmdate("Y", $time)
323 );
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
329/**
330 * Converts GMT time to a localized value
331 *
332 * Takes a Unix timestamp (in GMT) as input, and returns
333 * at the local value based on the timezone and DST setting
334 * submitted
335 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300336 * @param int Unix timestamp
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 * @param string timezone
338 * @param bool whether DST is active
Andrey Andreeve92df332012-03-26 22:44:20 +0300339 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200340 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000341if ( ! function_exists('gmt_to_local'))
342{
343 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200344 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 if ($time == '')
346 {
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
352 if ($dst == TRUE)
353 {
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
363/**
364 * Converts a MySQL Timestamp to Unix
365 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300366 * @param int Unix timestamp
367 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200368 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000369if ( ! function_exists('mysql_to_unix'))
370{
371 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
395/**
396 * Unix to "Human"
397 *
398 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
399 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300400 * @param int Unix timestamp
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 * @param bool whether to show seconds
402 * @param string format: us or euro
403 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200404 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000405if ( ! function_exists('unix_to_human'))
406{
407 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
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 if ($fmt == 'us')
412 {
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 if ($fmt == 'us')
426 {
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
436/**
437 * Convert "human" date to GMT
438 *
439 * Reverses the above process
440 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 * @param string format: us or euro
Andrey Andreeve92df332012-03-26 22:44:20 +0300442 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200443 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000444if ( ! function_exists('human_to_unix'))
445{
446 function human_to_unix($datestr = '')
447 {
448 if ($datestr == '')
449 {
450 return FALSE;
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600454 $datestr = preg_replace("/\040+/", ' ', $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
463 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Jones4b9c6292011-07-01 17:40:48 -0500465 $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
469 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones4b9c6292011-07-01 17:40:48 -0500472 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000473
Derek Jones1322ec52009-03-11 17:01:14 +0000474 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500476 $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
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 if (isset($split['2']))
485 {
486 $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 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500490 $hour = $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
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 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
510/**
Kyle Farris896d95a2011-08-21 23:03:54 -0300511 * Turns many "reasonably-date-like" strings into something
512 * that is actually useful. This only works for dates after unix epoch.
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500513 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300514 * @param string The terribly formatted date-like string
515 * @param string Date format to return (same as php date function)
516 * @return string
Kyle Farris896d95a2011-08-21 23:03:54 -0300517 */
518if ( ! function_exists('nice_date'))
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 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500530 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
Kyle Farris896d95a2011-08-21 23:03:54 -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
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500545 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
Kyle Farris896d95a2011-08-21 23:03:54 -0300551 return date($format, strtotime($month . '/01/' . $year));
552 }
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)
555 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);
558 return date($format, strtotime("{$y}-{$m}-{$d}"));
559 }
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".
564 if (date('U', strtotime($bad_date)) == '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500565 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300566 return "Invalid Date";
567 }
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
576/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * Timezone Menu
578 *
579 * Generates a drop-down menu of timezones.
580 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 * @param string timezone
582 * @param string classname
583 * @param string menu name
584 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200585 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000586if ( ! function_exists('timezone_menu'))
587{
588 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
589 {
590 $CI =& get_instance();
591 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200592
Greg Akerc964e722011-08-29 19:31:29 -0500593 $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
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 if ($class != '')
598 {
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 {
606 $selected = ($default == $key) ? " selected='selected'" : '';
607 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
608 }
609
610 $menu .= "</select>";
611
612 return $menu;
613 }
614}
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616// ------------------------------------------------------------------------
617
618/**
619 * Timezones
620 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300621 * Returns an array of timezones. This is a helper function
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 * for various other ones in this library
623 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 * @param string timezone
625 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200626 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000627if ( ! function_exists('timezones'))
628{
629 function timezones($tz = '')
630 {
631 // Note: Don't change the order of these even though
632 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200633
634 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500635 'UM12' => -12,
636 'UM11' => -11,
637 'UM10' => -10,
638 'UM95' => -9.5,
639 'UM9' => -9,
640 'UM8' => -8,
641 'UM7' => -7,
642 'UM6' => -6,
643 'UM5' => -5,
644 'UM45' => -4.5,
645 'UM4' => -4,
646 'UM35' => -3.5,
647 'UM3' => -3,
648 'UM2' => -2,
649 'UM1' => -1,
650 'UTC' => 0,
651 'UP1' => +1,
652 'UP2' => +2,
653 'UP3' => +3,
654 'UP35' => +3.5,
655 'UP4' => +4,
656 'UP45' => +4.5,
657 'UP5' => +5,
658 'UP55' => +5.5,
659 'UP575' => +5.75,
660 'UP6' => +6,
661 'UP65' => +6.5,
662 'UP7' => +7,
663 'UP8' => +8,
664 'UP875' => +8.75,
665 'UP9' => +9,
666 'UP95' => +9.5,
667 'UP10' => +10,
668 'UP105' => +10.5,
669 'UP11' => +11,
670 'UP115' => +11.5,
671 'UP12' => +12,
672 'UP1275' => +12.75,
673 'UP13' => +13,
674 'UP14' => +14
675 );
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 if ($tz == '')
678 {
679 return $zones;
680 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500681
Greg Akerf9168392011-08-29 19:29:05 -0500682 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500683
Andrey Andreeve92df332012-03-26 22:44:20 +0300684 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 }
686}
687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000689/* Location: ./system/helpers/date_helper.php */