blob: 7ff7444e580f70ea8fb59bb45351ba07b65e236f [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))
56 $timezone = $CI->config->item('timezone');
Barry Mienydd671972010-10-04 16:33:58 +020057
Iban Eguia83105952012-03-27 18:18:15 +020058 $timezone = new DateTimeZone($timezone);
59 $now = new DateTime('now', $timezone);
60 $offset = $timezone->getOffset($now);
61 $time = time() + $offset;
Barry Mienydd671972010-10-04 16:33:58 +020062
Iban Eguia83105952012-03-27 18:18:15 +020063 return $time;
Derek Allard2067d1a2008-11-13 22:59:24 +000064 }
65}
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067// ------------------------------------------------------------------------
68
69/**
70 * Convert MySQL Style Datecodes
71 *
72 * This function is identical to PHPs date() function,
73 * except that it allows date codes to be formatted using
74 * the MySQL style, where each code letter is preceded
Derek Jones4b9c6292011-07-01 17:40:48 -050075 * with a percent sign: %Y %m %d etc...
Derek Allard2067d1a2008-11-13 22:59:24 +000076 *
77 * The benefit of doing dates this way is that you don't
78 * have to worry about escaping your text letters that
79 * match the date codes.
80 *
Derek Allard2067d1a2008-11-13 22:59:24 +000081 * @param string
Andrey Andreeve92df332012-03-26 22:44:20 +030082 * @param int
83 * @return int
Barry Mienydd671972010-10-04 16:33:58 +020084 */
Derek Allard2067d1a2008-11-13 22:59:24 +000085if ( ! function_exists('mdate'))
86{
87 function mdate($datestr = '', $time = '')
88 {
89 if ($datestr == '')
Greg Akerf9168392011-08-29 19:29:05 -050090 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -050091 return '';
Greg Akerf9168392011-08-29 19:29:05 -050092 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Greg Akerc964e722011-08-29 19:31:29 -050094 $time = ($time == '') ? now() : $time;
Barry Mienydd671972010-10-04 16:33:58 +020095
Greg Akerf9168392011-08-29 19:29:05 -050096 $datestr = str_replace(
Eric Barnesdc3e4be2011-12-19 13:48:29 -050097 '%\\',
98 '',
Greg Akerf9168392011-08-29 19:29:05 -050099 preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr)
100 );
Greg Akerc964e722011-08-29 19:31:29 -0500101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 return date($datestr, $time);
103 }
104}
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106// ------------------------------------------------------------------------
107
108/**
109 * Standard Date
110 *
111 * Returns a date formatted according to the submitted standard.
112 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * @param string the chosen format
Andrey Andreeve92df332012-03-26 22:44:20 +0300114 * @param int Unix timestamp
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200116 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000117if ( ! function_exists('standard_date'))
118{
119 function standard_date($fmt = 'DATE_RFC822', $time = '')
120 {
121 $formats = array(
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400122 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400124 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
Syahril Zulkefli90658ad2011-11-13 23:43:37 +0800126 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC',
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
128 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400129 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400131 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O'
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 );
133
134 if ( ! isset($formats[$fmt]))
135 {
136 return FALSE;
137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 return mdate($formats[$fmt], $time);
140 }
141}
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143// ------------------------------------------------------------------------
144
145/**
146 * Timespan
147 *
148 * Returns a span of seconds in this format:
149 * 10 days 14 hours 36 minutes 47 seconds
150 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300151 * @param int a number of seconds
152 * @param int Unix timestamp
153 * @param int a number of display units
Roger Herbertb81f9092012-03-12 12:46:02 +0000154 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200155 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000156if ( ! function_exists('timespan'))
157{
Roger Herbert8d69aa12012-03-14 08:44:55 +0000158 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
160 $CI =& get_instance();
161 $CI->lang->load('date');
162
163 if ( ! is_numeric($seconds))
164 {
165 $seconds = 1;
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 if ( ! is_numeric($time))
169 {
170 $time = time();
171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Roger Herbert04c146d2012-03-11 15:31:01 +0000173 if ( ! is_numeric($units))
174 {
Roger Herbert8d69aa12012-03-14 08:44:55 +0000175 $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000176 }
177
Greg Akerf9168392011-08-29 19:29:05 -0500178 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200179
Roger Herbert04c146d2012-03-11 15:31:01 +0000180 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500181 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200184 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000185 $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
Barry Mienydd671972010-10-04 16:33:58 +0200186 }
187
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500188 $seconds -= $years * 31557600;
189 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200190
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000191 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
193 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200194 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000195 $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month'));
Barry Mienydd671972010-10-04 16:33:58 +0200196 }
197
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500198 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200
201 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200202
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000203 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
205 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200206 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000207 $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200211 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000212
213 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200214
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000215 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
217 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200218 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000219 $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 $seconds -= $days * 86400;
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200226
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000227 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
229 if ($hours > 0)
230 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000231 $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 $seconds -= $hours * 3600;
235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200238
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000239 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200242 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000243 $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 $seconds -= $minutes * 60;
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Roger Herbert597eb212012-03-14 09:06:17 +0000249 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000251 $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Roger Herbert04c146d2012-03-11 15:31:01 +0000254 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
256}
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258// ------------------------------------------------------------------------
259
260/**
261 * Number of days in a month
262 *
263 * Takes a month/year as input and returns the number of days
264 * for the given month/year. Takes leap years into consideration.
265 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300266 * @param int a numeric month
267 * @param int a numeric year
268 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200269 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000270if ( ! function_exists('days_in_month'))
271{
272 function days_in_month($month = 0, $year = '')
273 {
274 if ($month < 1 OR $month > 12)
275 {
276 return 0;
277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 if ( ! is_numeric($year) OR strlen($year) != 4)
280 {
281 $year = date('Y');
282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 if ($month == 2)
285 {
Andrey Andreeve92df332012-03-26 22:44:20 +0300286 if ($year % 400 == 0 OR ($year % 4 == 0 && $year % 100 != 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
288 return 29;
289 }
290 }
291
292 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
293 return $days_in_month[$month - 1];
294 }
295}
Derek Jones36591092010-03-05 10:05:51 -0600296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297// ------------------------------------------------------------------------
298
299/**
300 * Converts a local Unix timestamp to GMT
301 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300302 * @param int Unix timestamp
303 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200304 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000305if ( ! function_exists('local_to_gmt'))
306{
307 function local_to_gmt($time = '')
308 {
309 if ($time == '')
Greg Akerf9168392011-08-29 19:29:05 -0500310 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500312 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500313
Greg Akerf9168392011-08-29 19:29:05 -0500314 return mktime(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500315 gmdate("H", $time),
316 gmdate("i", $time),
317 gmdate("s", $time),
318 gmdate("m", $time),
319 gmdate("d", $time),
Greg Akerf9168392011-08-29 19:29:05 -0500320 gmdate("Y", $time)
321 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
323}
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325// ------------------------------------------------------------------------
326
327/**
328 * Converts GMT time to a localized value
329 *
330 * Takes a Unix timestamp (in GMT) as input, and returns
331 * at the local value based on the timezone and DST setting
332 * submitted
333 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300334 * @param int Unix timestamp
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 * @param string timezone
336 * @param bool whether DST is active
Andrey Andreeve92df332012-03-26 22:44:20 +0300337 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200338 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000339if ( ! function_exists('gmt_to_local'))
340{
341 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200342 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 if ($time == '')
344 {
345 return now();
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $time += timezones($timezone) * 3600;
349
350 if ($dst == TRUE)
351 {
352 $time += 3600;
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 return $time;
356 }
357}
Derek Jones36591092010-03-05 10:05:51 -0600358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359// ------------------------------------------------------------------------
360
361/**
362 * Converts a MySQL Timestamp to Unix
363 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300364 * @param int Unix timestamp
365 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200366 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000367if ( ! function_exists('mysql_to_unix'))
368{
369 function mysql_to_unix($time = '')
370 {
371 // We'll remove certain characters for backward compatibility
372 // since the formatting changed with MySQL 4.1
373 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 $time = str_replace('-', '', $time);
376 $time = str_replace(':', '', $time);
377 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500380 return mktime(
381 substr($time, 8, 2),
382 substr($time, 10, 2),
383 substr($time, 12, 2),
384 substr($time, 4, 2),
385 substr($time, 6, 2),
386 substr($time, 0, 4)
387 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
389}
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391// ------------------------------------------------------------------------
392
393/**
394 * Unix to "Human"
395 *
396 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
397 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300398 * @param int Unix timestamp
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * @param bool whether to show seconds
400 * @param string format: us or euro
401 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200402 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000403if ( ! function_exists('unix_to_human'))
404{
405 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
406 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500407 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 if ($fmt == 'us')
410 {
411 $r .= date('h', $time).':'.date('i', $time);
412 }
413 else
414 {
415 $r .= date('H', $time).':'.date('i', $time);
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 if ($seconds)
419 {
420 $r .= ':'.date('s', $time);
421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 if ($fmt == 'us')
424 {
425 $r .= ' '.date('A', $time);
426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 return $r;
429 }
430}
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432// ------------------------------------------------------------------------
433
434/**
435 * Convert "human" date to GMT
436 *
437 * Reverses the above process
438 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * @param string format: us or euro
Andrey Andreeve92df332012-03-26 22:44:20 +0300440 * @return int
Barry Mienydd671972010-10-04 16:33:58 +0200441 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000442if ( ! function_exists('human_to_unix'))
443{
444 function human_to_unix($datestr = '')
445 {
446 if ($datestr == '')
447 {
448 return FALSE;
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600452 $datestr = preg_replace("/\040+/", ' ', $datestr);
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 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))
455 {
456 return FALSE;
457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Jones36591092010-03-05 10:05:51 -0600459 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000460
461 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Jones4b9c6292011-07-01 17:40:48 -0500463 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
464 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
465 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000466
467 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones4b9c6292011-07-01 17:40:48 -0500470 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000471
Derek Jones1322ec52009-03-11 17:01:14 +0000472 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500474 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
476 else
477 {
478 // Unless specified, seconds get set to zero.
479 $sec = '00';
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 if (isset($split['2']))
483 {
484 $ampm = strtolower($split['2']);
Barry Mienydd671972010-10-04 16:33:58 +0200485
Andrey Andreeve92df332012-03-26 22:44:20 +0300486 if (substr($ampm, 0, 1) === 'p' && $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500487 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500488 $hour = $hour + 12;
Greg Akerf9168392011-08-29 19:29:05 -0500489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Andrey Andreeve92df332012-03-26 22:44:20 +0300491 if (substr($ampm, 0, 1) === 'a' && $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500492 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500493 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500494 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 if (strlen($hour) == 1)
Greg Akerf9168392011-08-29 19:29:05 -0500497 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500498 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500499 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 return mktime($hour, $min, $sec, $month, $day, $year);
503 }
504}
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506// ------------------------------------------------------------------------
507
508/**
Kyle Farris896d95a2011-08-21 23:03:54 -0300509 * Turns many "reasonably-date-like" strings into something
510 * that is actually useful. This only works for dates after unix epoch.
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500511 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300512 * @param string The terribly formatted date-like string
513 * @param string Date format to return (same as php date function)
514 * @return string
Kyle Farris896d95a2011-08-21 23:03:54 -0300515 */
516if ( ! function_exists('nice_date'))
517{
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500518 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300519 {
520 if (empty($bad_date))
521 {
522 return 'Unknown';
523 }
Greg Akerf9168392011-08-29 19:29:05 -0500524
Kyle Farris896d95a2011-08-21 23:03:54 -0300525 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500526 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300527 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500528 if (in_array(substr($bad_date, 0, 2),array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300529 {
530 $year = substr($bad_date, 0, 4);
531 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500532 }
533 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300534 {
535 $month = substr($bad_date, 0, 2);
536 $year = substr($bad_date, 2, 4);
537 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500538
Kyle Farris896d95a2011-08-21 23:03:54 -0300539 return date($format, strtotime($year . '-' . $month . '-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300540 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500541
Kyle Farris896d95a2011-08-21 23:03:54 -0300542 // Date Like: YYYYMMDD
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500543 if (preg_match('/^\d{8}$/',$bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300544 {
545 $month = substr($bad_date, 0, 2);
546 $day = substr($bad_date, 2, 2);
547 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500548
Kyle Farris896d95a2011-08-21 23:03:54 -0300549 return date($format, strtotime($month . '/01/' . $year));
550 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500551
Kyle Farris896d95a2011-08-21 23:03:54 -0300552 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
553 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500554 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300555 list($m, $d, $y) = explode('-', $bad_date);
556 return date($format, strtotime("{$y}-{$m}-{$d}"));
557 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500558
Kyle Farris896d95a2011-08-21 23:03:54 -0300559 // Any other kind of string, when converted into UNIX time,
560 // produces "0 seconds after epoc..." is probably bad...
561 // return "Invalid Date".
562 if (date('U', strtotime($bad_date)) == '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500563 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300564 return "Invalid Date";
565 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500566
Kyle Farris896d95a2011-08-21 23:03:54 -0300567 // It's probably a valid-ish date format already
568 return date($format, strtotime($bad_date));
569 }
570}
571
572// ------------------------------------------------------------------------
573
574/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 * Timezone Menu
576 *
577 * Generates a drop-down menu of timezones.
578 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 * @param string timezone
580 * @param string classname
581 * @param string menu name
582 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200583 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000584if ( ! function_exists('timezone_menu'))
585{
586 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
587 {
588 $CI =& get_instance();
589 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200590
Greg Akerc964e722011-08-29 19:31:29 -0500591 $default = ($default == 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000592
593 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 if ($class != '')
596 {
597 $menu .= ' class="'.$class.'"';
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 foreach (timezones() as $key => $val)
603 {
604 $selected = ($default == $key) ? " selected='selected'" : '';
605 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
606 }
607
608 $menu .= "</select>";
609
610 return $menu;
611 }
612}
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614// ------------------------------------------------------------------------
615
616/**
617 * Timezones
618 *
Andrey Andreeve92df332012-03-26 22:44:20 +0300619 * Returns an array of timezones. This is a helper function
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 * for various other ones in this library
621 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 * @param string timezone
623 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200624 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000625if ( ! function_exists('timezones'))
626{
627 function timezones($tz = '')
628 {
629 // Note: Don't change the order of these even though
630 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200631
632 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500633 'UM12' => -12,
634 'UM11' => -11,
635 'UM10' => -10,
636 'UM95' => -9.5,
637 'UM9' => -9,
638 'UM8' => -8,
639 'UM7' => -7,
640 'UM6' => -6,
641 'UM5' => -5,
642 'UM45' => -4.5,
643 'UM4' => -4,
644 'UM35' => -3.5,
645 'UM3' => -3,
646 'UM2' => -2,
647 'UM1' => -1,
648 'UTC' => 0,
649 'UP1' => +1,
650 'UP2' => +2,
651 'UP3' => +3,
652 'UP35' => +3.5,
653 'UP4' => +4,
654 'UP45' => +4.5,
655 'UP5' => +5,
656 'UP55' => +5.5,
657 'UP575' => +5.75,
658 'UP6' => +6,
659 'UP65' => +6.5,
660 'UP7' => +7,
661 'UP8' => +8,
662 'UP875' => +8.75,
663 'UP9' => +9,
664 'UP95' => +9.5,
665 'UP10' => +10,
666 'UP105' => +10.5,
667 'UP11' => +11,
668 'UP115' => +11.5,
669 'UP12' => +12,
670 'UP1275' => +12.75,
671 'UP13' => +13,
672 'UP14' => +14
673 );
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 if ($tz == '')
676 {
677 return $zones;
678 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500679
Greg Akerf9168392011-08-29 19:29:05 -0500680 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500681
Andrey Andreeve92df332012-03-26 22:44:20 +0300682 return isset($zones[$tz]) ? $zones[$tz] : 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 }
684}
685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000687/* Location: ./system/helpers/date_helper.php */