Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * CodeIgniter Date Helpers |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Helpers |
| 35 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/helpers/date_helper.html |
| 38 | */ |
| 39 | |
| 40 | // ------------------------------------------------------------------------ |
| 41 | |
| 42 | /** |
| 43 | * Get "now" time |
| 44 | * |
| 45 | * Returns time() or its GMT equivalent based on the config file preference |
| 46 | * |
| 47 | * @access public |
| 48 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 49 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | if ( ! function_exists('now')) |
| 51 | { |
| 52 | function now() |
| 53 | { |
| 54 | $CI =& get_instance(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 55 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | if (strtolower($CI->config->item('time_reference')) == 'gmt') |
| 57 | { |
| 58 | $now = time(); |
| 59 | $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 60 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | if (strlen($system_time) < 10) |
| 62 | { |
| 63 | $system_time = time(); |
| 64 | log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.'); |
| 65 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 66 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | return $system_time; |
| 68 | } |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 69 | |
| 70 | return time(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 73 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | // ------------------------------------------------------------------------ |
| 75 | |
| 76 | /** |
| 77 | * Convert MySQL Style Datecodes |
| 78 | * |
| 79 | * This function is identical to PHPs date() function, |
| 80 | * except that it allows date codes to be formatted using |
| 81 | * the MySQL style, where each code letter is preceded |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 82 | * with a percent sign: %Y %m %d etc... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | * |
| 84 | * The benefit of doing dates this way is that you don't |
| 85 | * have to worry about escaping your text letters that |
| 86 | * match the date codes. |
| 87 | * |
| 88 | * @access public |
| 89 | * @param string |
| 90 | * @param integer |
| 91 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | if ( ! function_exists('mdate')) |
| 94 | { |
| 95 | function mdate($datestr = '', $time = '') |
| 96 | { |
| 97 | if ($datestr == '') |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 98 | { |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 99 | return ''; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 100 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 101 | |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 102 | $time = ($time == '') ? now() : $time; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 104 | $datestr = str_replace( |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 105 | '%\\', |
| 106 | '', |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 107 | preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr) |
| 108 | ); |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 109 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | return date($datestr, $time); |
| 111 | } |
| 112 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 113 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | // ------------------------------------------------------------------------ |
| 115 | |
| 116 | /** |
| 117 | * Standard Date |
| 118 | * |
| 119 | * Returns a date formatted according to the submitted standard. |
| 120 | * |
| 121 | * @access public |
| 122 | * @param string the chosen format |
| 123 | * @param integer Unix timestamp |
| 124 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | if ( ! function_exists('standard_date')) |
| 127 | { |
| 128 | function standard_date($fmt = 'DATE_RFC822', $time = '') |
| 129 | { |
| 130 | $formats = array( |
| 131 | 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q', |
| 132 | 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC', |
Greg Aker | 3b889a9 | 2011-01-10 12:35:57 -0600 | [diff] [blame] | 133 | 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%Q', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O', |
Syahril Zulkefli | 90658ad | 2011-11-13 23:43:37 +0800 | [diff] [blame] | 135 | 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O', |
| 137 | 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O', |
| 138 | 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O', |
| 139 | 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q' |
| 140 | ); |
| 141 | |
| 142 | if ( ! isset($formats[$fmt])) |
| 143 | { |
| 144 | return FALSE; |
| 145 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | return mdate($formats[$fmt], $time); |
| 148 | } |
| 149 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | // ------------------------------------------------------------------------ |
| 152 | |
| 153 | /** |
| 154 | * Timespan |
| 155 | * |
| 156 | * Returns a span of seconds in this format: |
| 157 | * 10 days 14 hours 36 minutes 47 seconds |
| 158 | * |
| 159 | * @access public |
| 160 | * @param integer a number of seconds |
| 161 | * @param integer Unix timestamp |
| 162 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | if ( ! function_exists('timespan')) |
| 165 | { |
| 166 | function timespan($seconds = 1, $time = '') |
| 167 | { |
| 168 | $CI =& get_instance(); |
| 169 | $CI->lang->load('date'); |
| 170 | |
| 171 | if ( ! is_numeric($seconds)) |
| 172 | { |
| 173 | $seconds = 1; |
| 174 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | if ( ! is_numeric($time)) |
| 177 | { |
| 178 | $time = time(); |
| 179 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 180 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 181 | $seconds = ($time <= $seconds) ? 1 : $time - $seconds; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 182 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | $str = ''; |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 184 | $years = floor($seconds / 31557600); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 185 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | if ($years > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 187 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 191 | $seconds -= $years * 31557600; |
| 192 | $months = floor($seconds / 2629743); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | if ($years > 0 OR $months > 0) |
| 195 | { |
| 196 | if ($months > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 197 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 201 | $seconds -= $months * 2629743; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | $weeks = floor($seconds / 604800); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | if ($years > 0 OR $months > 0 OR $weeks > 0) |
| 207 | { |
| 208 | if ($weeks > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 209 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', '; |
| 211 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 212 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | $seconds -= $weeks * 604800; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 214 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | |
| 216 | $days = floor($seconds / 86400); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 217 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | if ($months > 0 OR $weeks > 0 OR $days > 0) |
| 219 | { |
| 220 | if ($days > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 221 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', '; |
| 223 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 224 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | $seconds -= $days * 86400; |
| 226 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 227 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | $hours = floor($seconds / 3600); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 229 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | if ($days > 0 OR $hours > 0) |
| 231 | { |
| 232 | if ($hours > 0) |
| 233 | { |
| 234 | $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', '; |
| 235 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 236 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | $seconds -= $hours * 3600; |
| 238 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 239 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | $minutes = floor($seconds / 60); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 241 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | if ($days > 0 OR $hours > 0 OR $minutes > 0) |
| 243 | { |
| 244 | if ($minutes > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', '; |
| 247 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 248 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | $seconds -= $minutes * 60; |
| 250 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 251 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | if ($str == '') |
| 253 | { |
| 254 | $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', '; |
| 255 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 256 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | return substr(trim($str), 0, -1); |
| 258 | } |
| 259 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 260 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | // ------------------------------------------------------------------------ |
| 262 | |
| 263 | /** |
| 264 | * Number of days in a month |
| 265 | * |
| 266 | * Takes a month/year as input and returns the number of days |
| 267 | * for the given month/year. Takes leap years into consideration. |
| 268 | * |
| 269 | * @access public |
| 270 | * @param integer a numeric month |
| 271 | * @param integer a numeric year |
| 272 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 273 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | if ( ! function_exists('days_in_month')) |
| 275 | { |
| 276 | function days_in_month($month = 0, $year = '') |
| 277 | { |
| 278 | if ($month < 1 OR $month > 12) |
| 279 | { |
| 280 | return 0; |
| 281 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | if ( ! is_numeric($year) OR strlen($year) != 4) |
| 284 | { |
| 285 | $year = date('Y'); |
| 286 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 287 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | if ($month == 2) |
| 289 | { |
| 290 | if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0)) |
| 291 | { |
| 292 | return 29; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
| 297 | return $days_in_month[$month - 1]; |
| 298 | } |
| 299 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 300 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | // ------------------------------------------------------------------------ |
| 302 | |
| 303 | /** |
| 304 | * Converts a local Unix timestamp to GMT |
| 305 | * |
| 306 | * @access public |
| 307 | * @param integer Unix timestamp |
| 308 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 309 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | if ( ! function_exists('local_to_gmt')) |
| 311 | { |
| 312 | function local_to_gmt($time = '') |
| 313 | { |
| 314 | if ($time == '') |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 315 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | $time = time(); |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 317 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 318 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 319 | return mktime( |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 320 | gmdate("H", $time), |
| 321 | gmdate("i", $time), |
| 322 | gmdate("s", $time), |
| 323 | gmdate("m", $time), |
| 324 | gmdate("d", $time), |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 325 | gmdate("Y", $time) |
| 326 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 329 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | // ------------------------------------------------------------------------ |
| 331 | |
| 332 | /** |
| 333 | * Converts GMT time to a localized value |
| 334 | * |
| 335 | * Takes a Unix timestamp (in GMT) as input, and returns |
| 336 | * at the local value based on the timezone and DST setting |
| 337 | * submitted |
| 338 | * |
| 339 | * @access public |
| 340 | * @param integer Unix timestamp |
| 341 | * @param string timezone |
| 342 | * @param bool whether DST is active |
| 343 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 344 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | if ( ! function_exists('gmt_to_local')) |
| 346 | { |
| 347 | function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 348 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | if ($time == '') |
| 350 | { |
| 351 | return now(); |
| 352 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 353 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | $time += timezones($timezone) * 3600; |
| 355 | |
| 356 | if ($dst == TRUE) |
| 357 | { |
| 358 | $time += 3600; |
| 359 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 360 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | return $time; |
| 362 | } |
| 363 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 364 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | // ------------------------------------------------------------------------ |
| 366 | |
| 367 | /** |
| 368 | * Converts a MySQL Timestamp to Unix |
| 369 | * |
| 370 | * @access public |
| 371 | * @param integer Unix timestamp |
| 372 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 373 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | if ( ! function_exists('mysql_to_unix')) |
| 375 | { |
| 376 | function mysql_to_unix($time = '') |
| 377 | { |
| 378 | // We'll remove certain characters for backward compatibility |
| 379 | // since the formatting changed with MySQL 4.1 |
| 380 | // YYYY-MM-DD HH:MM:SS |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 381 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | $time = str_replace('-', '', $time); |
| 383 | $time = str_replace(':', '', $time); |
| 384 | $time = str_replace(' ', '', $time); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 385 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 386 | // YYYYMMDDHHMMSS |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 387 | return mktime( |
| 388 | substr($time, 8, 2), |
| 389 | substr($time, 10, 2), |
| 390 | substr($time, 12, 2), |
| 391 | substr($time, 4, 2), |
| 392 | substr($time, 6, 2), |
| 393 | substr($time, 0, 4) |
| 394 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 397 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | // ------------------------------------------------------------------------ |
| 399 | |
| 400 | /** |
| 401 | * Unix to "Human" |
| 402 | * |
| 403 | * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM |
| 404 | * |
| 405 | * @access public |
| 406 | * @param integer Unix timestamp |
| 407 | * @param bool whether to show seconds |
| 408 | * @param string format: us or euro |
| 409 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 410 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | if ( ! function_exists('unix_to_human')) |
| 412 | { |
| 413 | function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us') |
| 414 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 415 | $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 416 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | if ($fmt == 'us') |
| 418 | { |
| 419 | $r .= date('h', $time).':'.date('i', $time); |
| 420 | } |
| 421 | else |
| 422 | { |
| 423 | $r .= date('H', $time).':'.date('i', $time); |
| 424 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 425 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | if ($seconds) |
| 427 | { |
| 428 | $r .= ':'.date('s', $time); |
| 429 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 430 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | if ($fmt == 'us') |
| 432 | { |
| 433 | $r .= ' '.date('A', $time); |
| 434 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 435 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | return $r; |
| 437 | } |
| 438 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 439 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | // ------------------------------------------------------------------------ |
| 441 | |
| 442 | /** |
| 443 | * Convert "human" date to GMT |
| 444 | * |
| 445 | * Reverses the above process |
| 446 | * |
| 447 | * @access public |
| 448 | * @param string format: us or euro |
| 449 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 450 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | if ( ! function_exists('human_to_unix')) |
| 452 | { |
| 453 | function human_to_unix($datestr = '') |
| 454 | { |
| 455 | if ($datestr == '') |
| 456 | { |
| 457 | return FALSE; |
| 458 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 459 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | $datestr = trim($datestr); |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 461 | $datestr = preg_replace("/\040+/", ' ', $datestr); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 462 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | 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)) |
| 464 | { |
| 465 | return FALSE; |
| 466 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 467 | |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 468 | $split = explode(' ', $datestr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | |
| 470 | $ex = explode("-", $split['0']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 471 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 472 | $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0']; |
| 473 | $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1']; |
| 474 | $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | |
| 476 | $ex = explode(":", $split['1']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 477 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0']; |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 479 | $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | |
Derek Jones | 1322ec5 | 2009-03-11 17:01:14 +0000 | [diff] [blame] | 481 | if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 483 | $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | } |
| 485 | else |
| 486 | { |
| 487 | // Unless specified, seconds get set to zero. |
| 488 | $sec = '00'; |
| 489 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 490 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | if (isset($split['2'])) |
| 492 | { |
| 493 | $ampm = strtolower($split['2']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 494 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 495 | if (substr($ampm, 0, 1) == 'p' AND $hour < 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 496 | { |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 497 | $hour = $hour + 12; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 498 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 499 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | if (substr($ampm, 0, 1) == 'a' AND $hour == 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 501 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 502 | $hour = '00'; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 503 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 504 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | if (strlen($hour) == 1) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 506 | { |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 507 | $hour = '0'.$hour; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 508 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 509 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 510 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | return mktime($hour, $min, $sec, $month, $day, $year); |
| 512 | } |
| 513 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 514 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 515 | // ------------------------------------------------------------------------ |
| 516 | |
| 517 | /** |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 518 | * Turns many "reasonably-date-like" strings into something |
| 519 | * that is actually useful. This only works for dates after unix epoch. |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 520 | * |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 521 | * @access public |
| 522 | * @param string The terribly formatted date-like string |
| 523 | * @param string Date format to return (same as php date function) |
| 524 | * @return string |
| 525 | */ |
| 526 | if ( ! function_exists('nice_date')) |
| 527 | { |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 528 | function nice_date($bad_date = '', $format = FALSE) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 529 | { |
| 530 | if (empty($bad_date)) |
| 531 | { |
| 532 | return 'Unknown'; |
| 533 | } |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 534 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 535 | // Date like: YYYYMM |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 536 | if (preg_match('/^\d{6}$/', $bad_date)) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 537 | { |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 538 | if (in_array(substr($bad_date, 0, 2),array('19', '20'))) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 539 | { |
| 540 | $year = substr($bad_date, 0, 4); |
| 541 | $month = substr($bad_date, 4, 2); |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 542 | } |
| 543 | else |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 544 | { |
| 545 | $month = substr($bad_date, 0, 2); |
| 546 | $year = substr($bad_date, 2, 4); |
| 547 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 548 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 549 | return date($format, strtotime($year . '-' . $month . '-01')); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 550 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 551 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 552 | // Date Like: YYYYMMDD |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 553 | if (preg_match('/^\d{8}$/',$bad_date)) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 554 | { |
| 555 | $month = substr($bad_date, 0, 2); |
| 556 | $day = substr($bad_date, 2, 2); |
| 557 | $year = substr($bad_date, 4, 4); |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 558 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 559 | return date($format, strtotime($month . '/01/' . $year)); |
| 560 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 561 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 562 | // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between) |
| 563 | if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date)) |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 564 | { |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 565 | list($m, $d, $y) = explode('-', $bad_date); |
| 566 | return date($format, strtotime("{$y}-{$m}-{$d}")); |
| 567 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 568 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 569 | // Any other kind of string, when converted into UNIX time, |
| 570 | // produces "0 seconds after epoc..." is probably bad... |
| 571 | // return "Invalid Date". |
| 572 | if (date('U', strtotime($bad_date)) == '0') |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 573 | { |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 574 | return "Invalid Date"; |
| 575 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 576 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 577 | // It's probably a valid-ish date format already |
| 578 | return date($format, strtotime($bad_date)); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // ------------------------------------------------------------------------ |
| 583 | |
| 584 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 585 | * Timezone Menu |
| 586 | * |
| 587 | * Generates a drop-down menu of timezones. |
| 588 | * |
| 589 | * @access public |
| 590 | * @param string timezone |
| 591 | * @param string classname |
| 592 | * @param string menu name |
| 593 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 594 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 595 | if ( ! function_exists('timezone_menu')) |
| 596 | { |
| 597 | function timezone_menu($default = 'UTC', $class = "", $name = 'timezones') |
| 598 | { |
| 599 | $CI =& get_instance(); |
| 600 | $CI->lang->load('date'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 601 | |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 602 | $default = ($default == 'GMT') ? 'UTC' : $default; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | |
| 604 | $menu = '<select name="'.$name.'"'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 605 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 606 | if ($class != '') |
| 607 | { |
| 608 | $menu .= ' class="'.$class.'"'; |
| 609 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 610 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 611 | $menu .= ">\n"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 612 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 613 | foreach (timezones() as $key => $val) |
| 614 | { |
| 615 | $selected = ($default == $key) ? " selected='selected'" : ''; |
| 616 | $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n"; |
| 617 | } |
| 618 | |
| 619 | $menu .= "</select>"; |
| 620 | |
| 621 | return $menu; |
| 622 | } |
| 623 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 624 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 625 | // ------------------------------------------------------------------------ |
| 626 | |
| 627 | /** |
| 628 | * Timezones |
| 629 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 630 | * Returns an array of timezones. This is a helper function |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 631 | * for various other ones in this library |
| 632 | * |
| 633 | * @access public |
| 634 | * @param string timezone |
| 635 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 636 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 637 | if ( ! function_exists('timezones')) |
| 638 | { |
| 639 | function timezones($tz = '') |
| 640 | { |
| 641 | // Note: Don't change the order of these even though |
| 642 | // some items appear to be in the wrong order |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 643 | |
| 644 | $zones = array( |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 645 | 'UM12' => -12, |
| 646 | 'UM11' => -11, |
| 647 | 'UM10' => -10, |
| 648 | 'UM95' => -9.5, |
| 649 | 'UM9' => -9, |
| 650 | 'UM8' => -8, |
| 651 | 'UM7' => -7, |
| 652 | 'UM6' => -6, |
| 653 | 'UM5' => -5, |
| 654 | 'UM45' => -4.5, |
| 655 | 'UM4' => -4, |
| 656 | 'UM35' => -3.5, |
| 657 | 'UM3' => -3, |
| 658 | 'UM2' => -2, |
| 659 | 'UM1' => -1, |
| 660 | 'UTC' => 0, |
| 661 | 'UP1' => +1, |
| 662 | 'UP2' => +2, |
| 663 | 'UP3' => +3, |
| 664 | 'UP35' => +3.5, |
| 665 | 'UP4' => +4, |
| 666 | 'UP45' => +4.5, |
| 667 | 'UP5' => +5, |
| 668 | 'UP55' => +5.5, |
| 669 | 'UP575' => +5.75, |
| 670 | 'UP6' => +6, |
| 671 | 'UP65' => +6.5, |
| 672 | 'UP7' => +7, |
| 673 | 'UP8' => +8, |
| 674 | 'UP875' => +8.75, |
| 675 | 'UP9' => +9, |
| 676 | 'UP95' => +9.5, |
| 677 | 'UP10' => +10, |
| 678 | 'UP105' => +10.5, |
| 679 | 'UP11' => +11, |
| 680 | 'UP115' => +11.5, |
| 681 | 'UP12' => +12, |
| 682 | 'UP1275' => +12.75, |
| 683 | 'UP13' => +13, |
| 684 | 'UP14' => +14 |
| 685 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 686 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 687 | if ($tz == '') |
| 688 | { |
| 689 | return $zones; |
| 690 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 691 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 692 | $tz = ($tz == 'GMT') ? 'UTC' : $tz; |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 693 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 694 | return ( ! isset($zones[$tz])) ? 0 : $zones[$tz]; |
| 695 | } |
| 696 | } |
| 697 | |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame^] | 698 | // ------------------------------------------------------------------------ |
| 699 | |
| 700 | /** |
| 701 | * Date range |
| 702 | * |
| 703 | * Returns a list of dates within a specified period. |
| 704 | * |
| 705 | * @access public |
| 706 | * @param int unix_start UNIX timestamp of period start date |
| 707 | * @param int unix_end|days UNIX timestamp of period end date |
| 708 | * or interval in days. |
| 709 | * @param mixed is_unix Specifies wether the second @param |
| 710 | * is a UNIX timestamp or day interval |
| 711 | * - TRUE or 'unix' for a timestamp |
| 712 | * - FALSE or 'days' for an interval |
| 713 | * @param string date_format Output date format, same as in date() |
| 714 | * @return array |
| 715 | */ |
| 716 | if ( ! function_exists('date_range')) |
| 717 | { |
| 718 | function date_range($unix_start = '', $mixed = '', $is_unix = TRUE, $format = 'Y-m-d') |
| 719 | { |
| 720 | if ($unix_start == '' OR $mixed == '' OR $format == '') |
| 721 | { |
| 722 | return FALSE; |
| 723 | } |
| 724 | |
| 725 | $is_unix = ! ( ! $is_unix OR $is_unix === 'days'); |
| 726 | |
| 727 | // Validate input and try strtotime() on invalid timestamps/intervals, just in case |
| 728 | if ( ( ! preg_match('/^[0-9]+$/', $unix_start) && ($unix_start = @strtotime($unix_time)) === FALSE) |
| 729 | OR ( ! preg_match('/^[0-9]+$/', $mixed) && ($is_unix === FALSE OR ($mixed = @strtotime($mixed)) === FALSE)) |
| 730 | OR ($is_unix === TRUE && $mixed < $unix_start)) |
| 731 | { |
| 732 | return FALSE; |
| 733 | } |
| 734 | |
| 735 | if ($is_unix && ($unix_start == $mixed OR date($format, $unix_start) === date($format, $mixed))) |
| 736 | { |
| 737 | return array($start_date); |
| 738 | } |
| 739 | |
| 740 | $range = array(); |
| 741 | |
| 742 | if (is_php('5.2')) |
| 743 | { |
| 744 | /* NOTE: Even though the DateTime object has many useful features, it appears that |
| 745 | * it doesn't always handle properly timezones, when timestamps are passed |
| 746 | * directly to its constructor. Neither of the following gave proper results: |
| 747 | * |
| 748 | * new DateTime('<timestamp>') |
| 749 | * new DateTime('<timestamp>', '<timezone>') |
| 750 | * |
| 751 | * --- available in PHP 5.3: |
| 752 | * |
| 753 | * DateTime::createFromFormat('<format>', '<timestamp>') |
| 754 | * DateTime::createFromFormat('<format>', '<timestamp>', '<timezone') |
| 755 | * |
| 756 | * ... so we'll have to set the timestamp after the object is instantiated. |
| 757 | * Furthermore, in PHP 5.3 we can use DateTime::setTimestamp() to do that and |
| 758 | * given that we have UNIX timestamps - we should use it. |
| 759 | */ |
| 760 | $from = new DateTime(); |
| 761 | |
| 762 | if (is_php('5.3')) |
| 763 | { |
| 764 | $from->setTimestamp($unix_start); |
| 765 | if ($is_unix) |
| 766 | { |
| 767 | $arg = new DateTime(); |
| 768 | $arg->setTimestamp($mixed); |
| 769 | } |
| 770 | else |
| 771 | { |
| 772 | $arg = (int) $mixed; |
| 773 | } |
| 774 | $period = new DatePeriod($from, new DateInterval('P1D'), $arg); |
| 775 | $range = array(); |
| 776 | foreach ($period as $date) |
| 777 | { |
| 778 | $range[] = $date->format($format); |
| 779 | } |
| 780 | |
| 781 | /* If a period end date was passed to the DatePeriod constructor, it might not |
| 782 | * be in our results. Not sure if this is a bug or it's just possible because |
| 783 | * the end date might actually be less than 24 hours away from the previously |
| 784 | * generated DateTime object, but either way - we have to append it manually. |
| 785 | */ |
| 786 | if ( ! is_int($arg) && $range[count($range) - 1] !== $arg->format($format)) |
| 787 | { |
| 788 | $range[] = $arg->format($format); |
| 789 | } |
| 790 | |
| 791 | return $range; |
| 792 | } |
| 793 | |
| 794 | $from->setDate(date('Y', $unix_start), date('n', $unix_start), date('j', $unix_start)); |
| 795 | $from->setTime(date('G', $unix_start), date('i', $unix_start), date('s', $unix_start)); |
| 796 | if ($is_unix) |
| 797 | { |
| 798 | $arg = new DateTime(); |
| 799 | $arg->setDate(date('Y', $mixed), date('n', $mixed), date('j', $mixed)); |
| 800 | $arg->setTime(date('G', $mixed), date('i', $mixed), date('s', $mixed)); |
| 801 | } |
| 802 | else |
| 803 | { |
| 804 | $arg = (int) $mixed; |
| 805 | } |
| 806 | $range[] = $from->format($format); |
| 807 | |
| 808 | if (is_int($arg)) // Day intervals |
| 809 | { |
| 810 | do |
| 811 | { |
| 812 | $from->modify('+1 day'); |
| 813 | $range[] = $from->format($format); |
| 814 | } |
| 815 | while (--$arg > 0); |
| 816 | } |
| 817 | else // end date UNIX timestamp |
| 818 | { |
| 819 | for ($from->modify('+1 day'), $end_check = $arg->format('Ymd'); $from->format('Ymd') < $end_check; $from->modify('+1 day')) |
| 820 | { |
| 821 | $range[] = $from->format($format); |
| 822 | } |
| 823 | |
| 824 | // Our loop only appended dates prior to our end date |
| 825 | $range[] = $arg->format($format); |
| 826 | } |
| 827 | |
| 828 | return $range; |
| 829 | } |
| 830 | |
| 831 | /* ---------------------------------------------------------------------------------- |
| 832 | * PHP Version is < 5.2. We have no other option, but to calculate manually ... |
| 833 | * |
| 834 | * NOTE: If we do something like this: |
| 835 | * |
| 836 | * $unix_timestamp + 86400 |
| 837 | * |
| 838 | * ... due to DST, there's a possibility of calculation errors and/or incorrect |
| 839 | * hours generated (if the specified format displays such data) due to DST. |
| 840 | */ |
| 841 | |
| 842 | $from = $to = array(); |
| 843 | sscanf(date('Y-n-j G:i:s', $unix_start), '%d-%d-%d %d:%d:%d', $from['y'], $from['mo'], $from['d'], $from['h'], $from['mi'], $from['s']); |
| 844 | |
| 845 | // If we don't have the end timestamp, let mktime() calculate it |
| 846 | $unix_end = ($is_unix) ? (int) $mixed : mktime($from['h'], $from['mi'], $from['s'], $from['mo'], $from['d'] + $mixed, $from['y']); |
| 847 | |
| 848 | $end_check = date('Ymd', $unix_end); |
| 849 | while (date('Ymd', $unix_start = mktime($from['h'], $from['mi'], $from['s'], $from['mo'], $from['d'], $from['y'])) !== $end_check) |
| 850 | { |
| 851 | $range[] = date($format, $unix_start); |
| 852 | $from['d']++; |
| 853 | } |
| 854 | |
| 855 | // Our loop only appended dates prior to our end date |
| 856 | $range[] = date($format, $unix_end); |
| 857 | |
| 858 | return $range; |
| 859 | } |
| 860 | } |
| 861 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 862 | /* End of file date_helper.php */ |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame^] | 863 | /* Location: ./system/helpers/date_helper.php */ |