blob: aecc7d90fa52be45c96fb0072fc23456ae8577c5 [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 *
43 * Returns time() or its GMT equivalent based on the config file preference
44 *
Andrey Andreeve92df332012-03-26 22:44:20 +030045 * @return int
Barry Mienydd671972010-10-04 16:33:58 +020046 */
Derek Allard2067d1a2008-11-13 22:59:24 +000047if ( ! function_exists('now'))
48{
Iban Eguia83105952012-03-27 18:18:15 +020049 function now($timezone = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000050 {
Iban Eguia83105952012-03-27 18:18:15 +020051 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020052
Iban Eguia83105952012-03-27 18:18:15 +020053 if (is_null($timezone))
54 $timezone = $CI->config->item('timezone');
Barry Mienydd671972010-10-04 16:33:58 +020055
Iban Eguia83105952012-03-27 18:18:15 +020056 $timezone = new DateTimeZone($timezone);
57 $now = new DateTime('now', $timezone);
58 $offset = $timezone->getOffset($now);
59 $time = time() + $offset;
Barry Mienydd671972010-10-04 16:33:58 +020060
Iban Eguia83105952012-03-27 18:18:15 +020061 return $time;
Derek Allard2067d1a2008-11-13 22:59:24 +000062 }
63}
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065// ------------------------------------------------------------------------
66
67/**
68 * Convert MySQL Style Datecodes
69 *
70 * This function is identical to PHPs date() function,
71 * except that it allows date codes to be formatted using
72 * the MySQL style, where each code letter is preceded
Derek Jones4b9c6292011-07-01 17:40:48 -050073 * with a percent sign: %Y %m %d etc...
Derek Allard2067d1a2008-11-13 22:59:24 +000074 *
75 * The benefit of doing dates this way is that you don't
76 * have to worry about escaping your text letters that
77 * match the date codes.
78 *
Derek Allard2067d1a2008-11-13 22:59:24 +000079 * @param string
Andrey Andreeve92df332012-03-26 22:44:20 +030080 * @param int
81 * @return int
Barry Mienydd671972010-10-04 16:33:58 +020082 */
Derek Allard2067d1a2008-11-13 22:59:24 +000083if ( ! function_exists('mdate'))
84{
85 function mdate($datestr = '', $time = '')
86 {
87 if ($datestr == '')
Greg Akerf9168392011-08-29 19:29:05 -050088 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -050089 return '';
Greg Akerf9168392011-08-29 19:29:05 -050090 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Greg Akerc964e722011-08-29 19:31:29 -050092 $time = ($time == '') ? now() : $time;
Barry Mienydd671972010-10-04 16:33:58 +020093
Greg Akerf9168392011-08-29 19:29:05 -050094 $datestr = str_replace(
Eric Barnesdc3e4be2011-12-19 13:48:29 -050095 '%\\',
96 '',
Greg Akerf9168392011-08-29 19:29:05 -050097 preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr)
98 );
Greg Akerc964e722011-08-29 19:31:29 -050099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 return date($datestr, $time);
101 }
102}
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104// ------------------------------------------------------------------------
105
106/**
107 * Standard Date
108 *
109 * Returns a date formatted according to the submitted standard.
110 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * @param string the chosen format
Andrey Andreeve92df332012-03-26 22:44:20 +0300112 * @param int Unix timestamp
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200114 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000115if ( ! function_exists('standard_date'))
116{
117 function standard_date($fmt = 'DATE_RFC822', $time = '')
118 {
119 $formats = array(
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400120 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400122 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
Syahril Zulkefli90658ad2011-11-13 23:43:37 +0800124 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC',
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
126 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400127 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400129 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O'
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 );
131
132 if ( ! isset($formats[$fmt]))
133 {
134 return FALSE;
135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 return mdate($formats[$fmt], $time);
138 }
139}
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141// ------------------------------------------------------------------------
142
143/**
144 * Timespan
145 *
146 * Returns a span of seconds in this format:
147 * 10 days 14 hours 36 minutes 47 seconds
148 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300149 * @param int a number of seconds
150 * @param int Unix timestamp
151 * @param int a number of display units
Roger Herbertb81f9092012-03-12 12:46:02 +0000152 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200153 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000154if ( ! function_exists('timespan'))
155{
Roger Herbert8d69aa12012-03-14 08:44:55 +0000156 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
158 $CI =& get_instance();
159 $CI->lang->load('date');
160
161 if ( ! is_numeric($seconds))
162 {
163 $seconds = 1;
164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 if ( ! is_numeric($time))
167 {
168 $time = time();
169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Roger Herbert04c146d2012-03-11 15:31:01 +0000171 if ( ! is_numeric($units))
172 {
Roger Herbert8d69aa12012-03-14 08:44:55 +0000173 $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000174 }
175
Greg Akerf9168392011-08-29 19:29:05 -0500176 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200177
Roger Herbert04c146d2012-03-11 15:31:01 +0000178 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500179 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200182 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000183 $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
Barry Mienydd671972010-10-04 16:33:58 +0200184 }
185
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500186 $seconds -= $years * 31557600;
187 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200188
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000189 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
191 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200192 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000193 $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month'));
Barry Mienydd671972010-10-04 16:33:58 +0200194 }
195
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500196 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198
199 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200200
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000201 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
203 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200204 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000205 $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200209 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000210
211 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200212
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000213 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
215 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200216 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000217 $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 $seconds -= $days * 86400;
221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200224
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000225 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
227 if ($hours > 0)
228 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000229 $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 $seconds -= $hours * 3600;
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200236
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000237 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
239 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200240 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000241 $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 $seconds -= $minutes * 60;
245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Roger Herbert597eb212012-03-14 09:06:17 +0000247 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000249 $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Roger Herbert04c146d2012-03-11 15:31:01 +0000252 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
254}
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256// ------------------------------------------------------------------------
257
258/**
259 * Number of days in a month
260 *
261 * Takes a month/year as input and returns the number of days
262 * for the given month/year. Takes leap years into consideration.
263 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300264 * @param int a numeric month
265 * @param int a numeric year
266 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200267 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000268if ( ! function_exists('days_in_month'))
269{
270 function days_in_month($month = 0, $year = '')
271 {
272 if ($month < 1 OR $month > 12)
273 {
274 return 0;
275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 if ( ! is_numeric($year) OR strlen($year) != 4)
278 {
279 $year = date('Y');
280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 if ($month == 2)
283 {
Andrey Andreeve92df332012-03-26 22:44:20 +0300284 if ($year % 400 == 0 OR ($year % 4 == 0 && $year % 100 != 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
286 return 29;
287 }
288 }
289
290 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
291 return $days_in_month[$month - 1];
292 }
293}
Derek Jones36591092010-03-05 10:05:51 -0600294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295// ------------------------------------------------------------------------
296
297/**
298 * Converts a local Unix timestamp to GMT
299 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300300 * @param int Unix timestamp
301 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200302 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000303if ( ! function_exists('local_to_gmt'))
304{
305 function local_to_gmt($time = '')
306 {
307 if ($time == '')
Greg Akerf9168392011-08-29 19:29:05 -0500308 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500310 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500311
Greg Akerf9168392011-08-29 19:29:05 -0500312 return mktime(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500313 gmdate("H", $time),
314 gmdate("i", $time),
315 gmdate("s", $time),
316 gmdate("m", $time),
317 gmdate("d", $time),
Greg Akerf9168392011-08-29 19:29:05 -0500318 gmdate("Y", $time)
319 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
321}
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323// ------------------------------------------------------------------------
324
325/**
326 * Converts GMT time to a localized value
327 *
328 * Takes a Unix timestamp (in GMT) as input, and returns
329 * at the local value based on the timezone and DST setting
330 * submitted
331 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300332 * @param int Unix timestamp
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @param string timezone
334 * @param bool whether DST is active
Andrey Andreeve92df332012-03-26 22:44:20 +0300335 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200336 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000337if ( ! function_exists('gmt_to_local'))
338{
339 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200340 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 if ($time == '')
342 {
343 return now();
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 $time += timezones($timezone) * 3600;
347
348 if ($dst == TRUE)
349 {
350 $time += 3600;
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 return $time;
354 }
355}
Derek Jones36591092010-03-05 10:05:51 -0600356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357// ------------------------------------------------------------------------
358
359/**
360 * Converts a MySQL Timestamp to Unix
361 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300362 * @param int Unix timestamp
363 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200364 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000365if ( ! function_exists('mysql_to_unix'))
366{
367 function mysql_to_unix($time = '')
368 {
369 // We'll remove certain characters for backward compatibility
370 // since the formatting changed with MySQL 4.1
371 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 $time = str_replace('-', '', $time);
374 $time = str_replace(':', '', $time);
375 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500378 return mktime(
379 substr($time, 8, 2),
380 substr($time, 10, 2),
381 substr($time, 12, 2),
382 substr($time, 4, 2),
383 substr($time, 6, 2),
384 substr($time, 0, 4)
385 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
387}
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389// ------------------------------------------------------------------------
390
391/**
392 * Unix to "Human"
393 *
394 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
395 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300396 * @param int Unix timestamp
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @param bool whether to show seconds
398 * @param string format: us or euro
399 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200400 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000401if ( ! function_exists('unix_to_human'))
402{
403 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
404 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500405 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 if ($fmt == 'us')
408 {
409 $r .= date('h', $time).':'.date('i', $time);
410 }
411 else
412 {
413 $r .= date('H', $time).':'.date('i', $time);
414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 if ($seconds)
417 {
418 $r .= ':'.date('s', $time);
419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 if ($fmt == 'us')
422 {
423 $r .= ' '.date('A', $time);
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 return $r;
427 }
428}
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430// ------------------------------------------------------------------------
431
432/**
433 * Convert "human" date to GMT
434 *
435 * Reverses the above process
436 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 * @param string format: us or euro
Andrey Andreeve92df332012-03-26 22:44:20 +0300438 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200439 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000440if ( ! function_exists('human_to_unix'))
441{
442 function human_to_unix($datestr = '')
443 {
444 if ($datestr == '')
445 {
446 return FALSE;
447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600450 $datestr = preg_replace("/\040+/", ' ', $datestr);
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 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))
453 {
454 return FALSE;
455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Jones36591092010-03-05 10:05:51 -0600457 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000458
459 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Jones4b9c6292011-07-01 17:40:48 -0500461 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
462 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
463 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000464
465 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones4b9c6292011-07-01 17:40:48 -0500468 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000469
Derek Jones1322ec52009-03-11 17:01:14 +0000470 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500472 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474 else
475 {
476 // Unless specified, seconds get set to zero.
477 $sec = '00';
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 if (isset($split['2']))
481 {
482 $ampm = strtolower($split['2']);
Barry Mienydd671972010-10-04 16:33:58 +0200483
Andrey Andreeve92df332012-03-26 22:44:20 +0300484 if (substr($ampm, 0, 1) === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500485 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500486 $hour = $hour + 12;
Greg Akerf9168392011-08-29 19:29:05 -0500487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Andrey Andreeve92df332012-03-26 22:44:20 +0300489 if (substr($ampm, 0, 1) === 'a' && $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500490 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500491 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500492 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 if (strlen($hour) == 1)
Greg Akerf9168392011-08-29 19:29:05 -0500495 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500496 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500497 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 return mktime($hour, $min, $sec, $month, $day, $year);
501 }
502}
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504// ------------------------------------------------------------------------
505
506/**
Kyle Farris896d95a2011-08-21 23:03:54 -0300507 * Turns many "reasonably-date-like" strings into something
508 * that is actually useful. This only works for dates after unix epoch.
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500509 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300510 * @param string The terribly formatted date-like string
511 * @param string Date format to return (same as php date function)
512 * @return string
Kyle Farris896d95a2011-08-21 23:03:54 -0300513 */
514if ( ! function_exists('nice_date'))
515{
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500516 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300517 {
518 if (empty($bad_date))
519 {
520 return 'Unknown';
521 }
Greg Akerf9168392011-08-29 19:29:05 -0500522
Kyle Farris896d95a2011-08-21 23:03:54 -0300523 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500524 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300525 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500526 if (in_array(substr($bad_date, 0, 2),array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300527 {
528 $year = substr($bad_date, 0, 4);
529 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500530 }
531 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300532 {
533 $month = substr($bad_date, 0, 2);
534 $year = substr($bad_date, 2, 4);
535 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500536
Kyle Farris896d95a2011-08-21 23:03:54 -0300537 return date($format, strtotime($year . '-' . $month . '-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300538 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500539
Kyle Farris896d95a2011-08-21 23:03:54 -0300540 // Date Like: YYYYMMDD
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500541 if (preg_match('/^\d{8}$/',$bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300542 {
543 $month = substr($bad_date, 0, 2);
544 $day = substr($bad_date, 2, 2);
545 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500546
Kyle Farris896d95a2011-08-21 23:03:54 -0300547 return date($format, strtotime($month . '/01/' . $year));
548 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500549
Kyle Farris896d95a2011-08-21 23:03:54 -0300550 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
551 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500552 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300553 list($m, $d, $y) = explode('-', $bad_date);
554 return date($format, strtotime("{$y}-{$m}-{$d}"));
555 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500556
Kyle Farris896d95a2011-08-21 23:03:54 -0300557 // Any other kind of string, when converted into UNIX time,
558 // produces "0 seconds after epoc..." is probably bad...
559 // return "Invalid Date".
560 if (date('U', strtotime($bad_date)) == '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500561 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300562 return "Invalid Date";
563 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500564
Kyle Farris896d95a2011-08-21 23:03:54 -0300565 // It's probably a valid-ish date format already
566 return date($format, strtotime($bad_date));
567 }
568}
569
570// ------------------------------------------------------------------------
571
572/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 * Timezone Menu
574 *
575 * Generates a drop-down menu of timezones.
576 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * @param string timezone
578 * @param string classname
579 * @param string menu name
580 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200581 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000582if ( ! function_exists('timezone_menu'))
583{
584 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
585 {
586 $CI =& get_instance();
587 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200588
Greg Akerc964e722011-08-29 19:31:29 -0500589 $default = ($default == 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000590
591 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 if ($class != '')
594 {
595 $menu .= ' class="'.$class.'"';
596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 foreach (timezones() as $key => $val)
601 {
602 $selected = ($default == $key) ? " selected='selected'" : '';
603 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
604 }
605
606 $menu .= "</select>";
607
608 return $menu;
609 }
610}
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612// ------------------------------------------------------------------------
613
614/**
615 * Timezones
616 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300617 * Returns an array of timezones. This is a helper function
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 * for various other ones in this library
619 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 * @param string timezone
621 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200622 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000623if ( ! function_exists('timezones'))
624{
625 function timezones($tz = '')
626 {
627 // Note: Don't change the order of these even though
628 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200629
630 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500631 'UM12' => -12,
632 'UM11' => -11,
633 'UM10' => -10,
634 'UM95' => -9.5,
635 'UM9' => -9,
636 'UM8' => -8,
637 'UM7' => -7,
638 'UM6' => -6,
639 'UM5' => -5,
640 'UM45' => -4.5,
641 'UM4' => -4,
642 'UM35' => -3.5,
643 'UM3' => -3,
644 'UM2' => -2,
645 'UM1' => -1,
646 'UTC' => 0,
647 'UP1' => +1,
648 'UP2' => +2,
649 'UP3' => +3,
650 'UP35' => +3.5,
651 'UP4' => +4,
652 'UP45' => +4.5,
653 'UP5' => +5,
654 'UP55' => +5.5,
655 'UP575' => +5.75,
656 'UP6' => +6,
657 'UP65' => +6.5,
658 'UP7' => +7,
659 'UP8' => +8,
660 'UP875' => +8.75,
661 'UP9' => +9,
662 'UP95' => +9.5,
663 'UP10' => +10,
664 'UP105' => +10.5,
665 'UP11' => +11,
666 'UP115' => +11.5,
667 'UP12' => +12,
668 'UP1275' => +12.75,
669 'UP13' => +13,
670 'UP14' => +14
671 );
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 if ($tz == '')
674 {
675 return $zones;
676 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500677
Greg Akerf9168392011-08-29 19:29:05 -0500678 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500679
Andrey Andreeve92df332012-03-26 22:44:20 +0300680 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
682}
683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000685/* Location: ./system/helpers/date_helper.php */