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 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * CodeIgniter Date Helpers |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Helpers |
| 23 | * @category Helpers |
| 24 | * @author ExpressionEngine Dev Team |
| 25 | * @link http://codeigniter.com/user_guide/helpers/date_helper.html |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Get "now" time |
| 32 | * |
| 33 | * Returns time() or its GMT equivalent based on the config file preference |
| 34 | * |
| 35 | * @access public |
| 36 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 37 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | if ( ! function_exists('now')) |
| 39 | { |
| 40 | function now() |
| 41 | { |
| 42 | $CI =& get_instance(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 43 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | if (strtolower($CI->config->item('time_reference')) == 'gmt') |
| 45 | { |
| 46 | $now = time(); |
| 47 | $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] | 48 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | if (strlen($system_time) < 10) |
| 50 | { |
| 51 | $system_time = time(); |
| 52 | log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.'); |
| 53 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 54 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | return $system_time; |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | return time(); |
| 60 | } |
| 61 | } |
| 62 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 63 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | // ------------------------------------------------------------------------ |
| 65 | |
| 66 | /** |
| 67 | * Convert MySQL Style Datecodes |
| 68 | * |
| 69 | * This function is identical to PHPs date() function, |
| 70 | * except that it allows date codes to be formatted using |
| 71 | * the MySQL style, where each code letter is preceded |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 72 | * with a percent sign: %Y %m %d etc... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | * |
| 74 | * The benefit of doing dates this way is that you don't |
| 75 | * have to worry about escaping your text letters that |
| 76 | * match the date codes. |
| 77 | * |
| 78 | * @access public |
| 79 | * @param string |
| 80 | * @param integer |
| 81 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 82 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | if ( ! function_exists('mdate')) |
| 84 | { |
| 85 | function mdate($datestr = '', $time = '') |
| 86 | { |
| 87 | if ($datestr == '') |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 88 | { |
| 89 | return ''; |
| 90 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 91 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | if ($time == '') |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 93 | { |
| 94 | $time = now(); |
| 95 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 96 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 97 | $datestr = str_replace( |
| 98 | '%\\', |
| 99 | '', |
| 100 | preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr) |
| 101 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | return date($datestr, $time); |
| 103 | } |
| 104 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 105 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | // ------------------------------------------------------------------------ |
| 107 | |
| 108 | /** |
| 109 | * Standard Date |
| 110 | * |
| 111 | * Returns a date formatted according to the submitted standard. |
| 112 | * |
| 113 | * @access public |
| 114 | * @param string the chosen format |
| 115 | * @param integer Unix timestamp |
| 116 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 117 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | if ( ! function_exists('standard_date')) |
| 119 | { |
| 120 | function standard_date($fmt = 'DATE_RFC822', $time = '') |
| 121 | { |
| 122 | $formats = array( |
| 123 | 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q', |
| 124 | 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC', |
Greg Aker | 3b889a9 | 2011-01-10 12:35:57 -0600 | [diff] [blame] | 125 | 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%Q', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O', |
| 127 | 'DATE_RFC850' => '%l, %d-%M-%y %H:%m:%i UTC', |
| 128 | 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O', |
| 129 | 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O', |
| 130 | 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O', |
| 131 | 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q' |
| 132 | ); |
| 133 | |
| 134 | if ( ! isset($formats[$fmt])) |
| 135 | { |
| 136 | return FALSE; |
| 137 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | return mdate($formats[$fmt], $time); |
| 140 | } |
| 141 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 142 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | // ------------------------------------------------------------------------ |
| 144 | |
| 145 | /** |
| 146 | * Timespan |
| 147 | * |
| 148 | * Returns a span of seconds in this format: |
| 149 | * 10 days 14 hours 36 minutes 47 seconds |
| 150 | * |
| 151 | * @access public |
| 152 | * @param integer a number of seconds |
| 153 | * @param integer Unix timestamp |
| 154 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | if ( ! function_exists('timespan')) |
| 157 | { |
| 158 | function timespan($seconds = 1, $time = '') |
| 159 | { |
| 160 | $CI =& get_instance(); |
| 161 | $CI->lang->load('date'); |
| 162 | |
| 163 | if ( ! is_numeric($seconds)) |
| 164 | { |
| 165 | $seconds = 1; |
| 166 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | if ( ! is_numeric($time)) |
| 169 | { |
| 170 | $time = time(); |
| 171 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 172 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 173 | $seconds = ($time <= $seconds) ? 1 : $time - $seconds; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | $str = ''; |
| 176 | $years = floor($seconds / 31536000); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | if ($years > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | $seconds -= $years * 31536000; |
| 184 | $months = floor($seconds / 2628000); |
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 OR $months > 0) |
| 187 | { |
| 188 | if ($months > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | $seconds -= $months * 2628000; |
| 194 | } |
| 195 | |
| 196 | $weeks = floor($seconds / 604800); |
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 | if ($years > 0 OR $months > 0 OR $weeks > 0) |
| 199 | { |
| 200 | if ($weeks > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', '; |
| 203 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 204 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | $seconds -= $weeks * 604800; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | |
| 208 | $days = floor($seconds / 86400); |
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 | if ($months > 0 OR $weeks > 0 OR $days > 0) |
| 211 | { |
| 212 | if ($days > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 213 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', '; |
| 215 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | $seconds -= $days * 86400; |
| 218 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 219 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | $hours = floor($seconds / 3600); |
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 | if ($days > 0 OR $hours > 0) |
| 223 | { |
| 224 | if ($hours > 0) |
| 225 | { |
| 226 | $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', '; |
| 227 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 228 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | $seconds -= $hours * 3600; |
| 230 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 231 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | $minutes = floor($seconds / 60); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 233 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | if ($days > 0 OR $hours > 0 OR $minutes > 0) |
| 235 | { |
| 236 | if ($minutes > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 237 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', '; |
| 239 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 240 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | $seconds -= $minutes * 60; |
| 242 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 243 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | if ($str == '') |
| 245 | { |
| 246 | $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', '; |
| 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 | return substr(trim($str), 0, -1); |
| 250 | } |
| 251 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 252 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | // ------------------------------------------------------------------------ |
| 254 | |
| 255 | /** |
| 256 | * Number of days in a month |
| 257 | * |
| 258 | * Takes a month/year as input and returns the number of days |
| 259 | * for the given month/year. Takes leap years into consideration. |
| 260 | * |
| 261 | * @access public |
| 262 | * @param integer a numeric month |
| 263 | * @param integer a numeric year |
| 264 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 265 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | if ( ! function_exists('days_in_month')) |
| 267 | { |
| 268 | function days_in_month($month = 0, $year = '') |
| 269 | { |
| 270 | if ($month < 1 OR $month > 12) |
| 271 | { |
| 272 | return 0; |
| 273 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 274 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | if ( ! is_numeric($year) OR strlen($year) != 4) |
| 276 | { |
| 277 | $year = date('Y'); |
| 278 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | if ($month == 2) |
| 281 | { |
| 282 | if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0)) |
| 283 | { |
| 284 | return 29; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
| 289 | return $days_in_month[$month - 1]; |
| 290 | } |
| 291 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 292 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | // ------------------------------------------------------------------------ |
| 294 | |
| 295 | /** |
| 296 | * Converts a local Unix timestamp to GMT |
| 297 | * |
| 298 | * @access public |
| 299 | * @param integer Unix timestamp |
| 300 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 301 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | if ( ! function_exists('local_to_gmt')) |
| 303 | { |
| 304 | function local_to_gmt($time = '') |
| 305 | { |
| 306 | if ($time == '') |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 307 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | $time = time(); |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 309 | } |
| 310 | |
| 311 | return mktime( |
| 312 | gmdate("H", $time), |
| 313 | gmdate("i", $time), |
| 314 | gmdate("s", $time), |
| 315 | gmdate("m", $time), |
| 316 | gmdate("d", $time), |
| 317 | gmdate("Y", $time) |
| 318 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 321 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | // ------------------------------------------------------------------------ |
| 323 | |
| 324 | /** |
| 325 | * Converts GMT time to a localized value |
| 326 | * |
| 327 | * Takes a Unix timestamp (in GMT) as input, and returns |
| 328 | * at the local value based on the timezone and DST setting |
| 329 | * submitted |
| 330 | * |
| 331 | * @access public |
| 332 | * @param integer Unix timestamp |
| 333 | * @param string timezone |
| 334 | * @param bool whether DST is active |
| 335 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 336 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | if ( ! function_exists('gmt_to_local')) |
| 338 | { |
| 339 | function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 340 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | if ($time == '') |
| 342 | { |
| 343 | return now(); |
| 344 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 345 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | $time += timezones($timezone) * 3600; |
| 347 | |
| 348 | if ($dst == TRUE) |
| 349 | { |
| 350 | $time += 3600; |
| 351 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 352 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | return $time; |
| 354 | } |
| 355 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 356 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | // ------------------------------------------------------------------------ |
| 358 | |
| 359 | /** |
| 360 | * Converts a MySQL Timestamp to Unix |
| 361 | * |
| 362 | * @access public |
| 363 | * @param integer Unix timestamp |
| 364 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 365 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | if ( ! function_exists('mysql_to_unix')) |
| 367 | { |
| 368 | function mysql_to_unix($time = '') |
| 369 | { |
| 370 | // We'll remove certain characters for backward compatibility |
| 371 | // since the formatting changed with MySQL 4.1 |
| 372 | // YYYY-MM-DD HH:MM:SS |
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 | $time = str_replace('-', '', $time); |
| 375 | $time = str_replace(':', '', $time); |
| 376 | $time = str_replace(' ', '', $time); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 377 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | // YYYYMMDDHHMMSS |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 379 | return mktime( |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 380 | substr($time, 8, 2), |
| 381 | substr($time, 10, 2), |
| 382 | substr($time, 12, 2), |
| 383 | substr($time, 4, 2), |
| 384 | substr($time, 6, 2), |
| 385 | substr($time, 0, 4) |
| 386 | ); |
| 387 | } |
| 388 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 389 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | // ------------------------------------------------------------------------ |
| 391 | |
| 392 | /** |
| 393 | * Unix to "Human" |
| 394 | * |
| 395 | * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM |
| 396 | * |
| 397 | * @access public |
| 398 | * @param integer Unix timestamp |
| 399 | * @param bool whether to show seconds |
| 400 | * @param string format: us or euro |
| 401 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | if ( ! function_exists('unix_to_human')) |
| 404 | { |
| 405 | function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us') |
| 406 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 407 | $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 408 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 417 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | if ($seconds) |
| 419 | { |
| 420 | $r .= ':'.date('s', $time); |
| 421 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 422 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | if ($fmt == 'us') |
| 424 | { |
| 425 | $r .= ' '.date('A', $time); |
| 426 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 427 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | return $r; |
| 429 | } |
| 430 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 431 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | // ------------------------------------------------------------------------ |
| 433 | |
| 434 | /** |
| 435 | * Convert "human" date to GMT |
| 436 | * |
| 437 | * Reverses the above process |
| 438 | * |
| 439 | * @access public |
| 440 | * @param string format: us or euro |
| 441 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 442 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | if ( ! function_exists('human_to_unix')) |
| 444 | { |
| 445 | function human_to_unix($datestr = '') |
| 446 | { |
| 447 | if ($datestr == '') |
| 448 | { |
| 449 | return FALSE; |
| 450 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 451 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | $datestr = trim($datestr); |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 453 | $datestr = preg_replace("/\040+/", ' ', $datestr); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 454 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | if ( ! preg_match('/^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr)) |
| 456 | { |
| 457 | return FALSE; |
| 458 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 459 | |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 460 | $split = explode(' ', $datestr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | |
| 462 | $ex = explode("-", $split['0']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 463 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 464 | $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0']; |
| 465 | $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1']; |
| 466 | $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | |
| 468 | $ex = explode(":", $split['1']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 469 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0']; |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 471 | $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | |
Derek Jones | 1322ec5 | 2009-03-11 17:01:14 +0000 | [diff] [blame] | 473 | if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 475 | $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | } |
| 477 | else |
| 478 | { |
| 479 | // Unless specified, seconds get set to zero. |
| 480 | $sec = '00'; |
| 481 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | if (isset($split['2'])) |
| 484 | { |
| 485 | $ampm = strtolower($split['2']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 486 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | if (substr($ampm, 0, 1) == 'p' AND $hour < 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 488 | { |
| 489 | $hour = $hour + 12; |
| 490 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 491 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | if (substr($ampm, 0, 1) == 'a' AND $hour == 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 493 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 494 | $hour = '00'; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 495 | } |
| 496 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 497 | if (strlen($hour) == 1) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 498 | { |
| 499 | $hour = '0'.$hour; |
| 500 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 502 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | return mktime($hour, $min, $sec, $month, $day, $year); |
| 504 | } |
| 505 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 506 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 507 | // ------------------------------------------------------------------------ |
| 508 | |
| 509 | /** |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 510 | * Turns many "reasonably-date-like" strings into something |
| 511 | * that is actually useful. This only works for dates after unix epoch. |
| 512 | * |
| 513 | * @access public |
| 514 | * @param string The terribly formatted date-like string |
| 515 | * @param string Date format to return (same as php date function) |
| 516 | * @return string |
| 517 | */ |
| 518 | if ( ! function_exists('nice_date')) |
| 519 | { |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 520 | function nice_date($bad_date = '', $format = FALSE) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 521 | { |
| 522 | if (empty($bad_date)) |
| 523 | { |
| 524 | return 'Unknown'; |
| 525 | } |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 526 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 527 | // Date like: YYYYMM |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 528 | if (preg_match('/^\d{6}$/', $bad_date)) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 529 | { |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 530 | if (in_array(substr($bad_date, 0, 2),array('19', '20'))) |
| 531 | { |
| 532 | $year = substr($bad_date, 0, 4); |
| 533 | $month = substr($bad_date, 4, 2); |
| 534 | } |
| 535 | else |
| 536 | { |
| 537 | $month = substr($bad_date, 0, 2); |
| 538 | $year = substr($bad_date, 2, 4); |
| 539 | } |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 540 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 541 | return date($format, strtotime($year . '-' . $month . '-01')); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | // Date Like: YYYYMMDD |
| 545 | if (preg_match('/^\d{8}$/',$bad_date)) |
| 546 | { |
| 547 | $month = substr($bad_date, 0, 2); |
| 548 | $day = substr($bad_date, 2, 2); |
| 549 | $year = substr($bad_date, 4, 4); |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 550 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 551 | return date($format, strtotime($month . '/01/' . $year)); |
| 552 | } |
| 553 | |
| 554 | // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between) |
| 555 | if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date)) |
| 556 | { |
| 557 | list($m, $d, $y) = explode('-', $bad_date); |
| 558 | return date($format, strtotime("{$y}-{$m}-{$d}")); |
| 559 | } |
| 560 | |
| 561 | // Any other kind of string, when converted into UNIX time, |
| 562 | // produces "0 seconds after epoc..." is probably bad... |
| 563 | // return "Invalid Date". |
| 564 | if (date('U', strtotime($bad_date)) == '0') |
| 565 | { |
| 566 | return "Invalid Date"; |
| 567 | } |
| 568 | |
| 569 | // It's probably a valid-ish date format already |
| 570 | return date($format, strtotime($bad_date)); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | // ------------------------------------------------------------------------ |
| 575 | |
| 576 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 577 | * Timezone Menu |
| 578 | * |
| 579 | * Generates a drop-down menu of timezones. |
| 580 | * |
| 581 | * @access public |
| 582 | * @param string timezone |
| 583 | * @param string classname |
| 584 | * @param string menu name |
| 585 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 586 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 587 | if ( ! function_exists('timezone_menu')) |
| 588 | { |
| 589 | function timezone_menu($default = 'UTC', $class = "", $name = 'timezones') |
| 590 | { |
| 591 | $CI =& get_instance(); |
| 592 | $CI->lang->load('date'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 593 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 594 | if ($default == 'GMT') |
| 595 | $default = 'UTC'; |
| 596 | |
| 597 | $menu = '<select name="'.$name.'"'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 598 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 599 | if ($class != '') |
| 600 | { |
| 601 | $menu .= ' class="'.$class.'"'; |
| 602 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 603 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 604 | $menu .= ">\n"; |
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 | foreach (timezones() as $key => $val) |
| 607 | { |
| 608 | $selected = ($default == $key) ? " selected='selected'" : ''; |
| 609 | $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n"; |
| 610 | } |
| 611 | |
| 612 | $menu .= "</select>"; |
| 613 | |
| 614 | return $menu; |
| 615 | } |
| 616 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 617 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 618 | // ------------------------------------------------------------------------ |
| 619 | |
| 620 | /** |
| 621 | * Timezones |
| 622 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 623 | * Returns an array of timezones. This is a helper function |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | * for various other ones in this library |
| 625 | * |
| 626 | * @access public |
| 627 | * @param string timezone |
| 628 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 629 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 630 | if ( ! function_exists('timezones')) |
| 631 | { |
| 632 | function timezones($tz = '') |
| 633 | { |
| 634 | // Note: Don't change the order of these even though |
| 635 | // some items appear to be in the wrong order |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 636 | |
| 637 | $zones = array( |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 638 | 'UM12' => -12, |
| 639 | 'UM11' => -11, |
| 640 | 'UM10' => -10, |
| 641 | 'UM95' => -9.5, |
| 642 | 'UM9' => -9, |
| 643 | 'UM8' => -8, |
| 644 | 'UM7' => -7, |
| 645 | 'UM6' => -6, |
| 646 | 'UM5' => -5, |
| 647 | 'UM45' => -4.5, |
| 648 | 'UM4' => -4, |
| 649 | 'UM35' => -3.5, |
| 650 | 'UM3' => -3, |
| 651 | 'UM2' => -2, |
| 652 | 'UM1' => -1, |
| 653 | 'UTC' => 0, |
| 654 | 'UP1' => +1, |
| 655 | 'UP2' => +2, |
| 656 | 'UP3' => +3, |
| 657 | 'UP35' => +3.5, |
| 658 | 'UP4' => +4, |
| 659 | 'UP45' => +4.5, |
| 660 | 'UP5' => +5, |
| 661 | 'UP55' => +5.5, |
| 662 | 'UP575' => +5.75, |
| 663 | 'UP6' => +6, |
| 664 | 'UP65' => +6.5, |
| 665 | 'UP7' => +7, |
| 666 | 'UP8' => +8, |
| 667 | 'UP875' => +8.75, |
| 668 | 'UP9' => +9, |
| 669 | 'UP95' => +9.5, |
| 670 | 'UP10' => +10, |
| 671 | 'UP105' => +10.5, |
| 672 | 'UP11' => +11, |
| 673 | 'UP115' => +11.5, |
| 674 | 'UP12' => +12, |
| 675 | 'UP1275' => +12.75, |
| 676 | 'UP13' => +13, |
| 677 | 'UP14' => +14 |
| 678 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 679 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 680 | if ($tz == '') |
| 681 | { |
| 682 | return $zones; |
| 683 | } |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame^] | 684 | |
| 685 | $tz = ($tz == 'GMT') ? 'UTC' : $tz; |
| 686 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 687 | return ( ! isset($zones[$tz])) ? 0 : $zones[$tz]; |
| 688 | } |
| 689 | } |
| 690 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 691 | /* End of file date_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 692 | /* Location: ./system/helpers/date_helper.php */ |