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 | } |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 57 | |
| 58 | return time(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 61 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | // ------------------------------------------------------------------------ |
| 63 | |
| 64 | /** |
| 65 | * Convert MySQL Style Datecodes |
| 66 | * |
| 67 | * This function is identical to PHPs date() function, |
| 68 | * except that it allows date codes to be formatted using |
| 69 | * the MySQL style, where each code letter is preceded |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 70 | * with a percent sign: %Y %m %d etc... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | * |
| 72 | * The benefit of doing dates this way is that you don't |
| 73 | * have to worry about escaping your text letters that |
| 74 | * match the date codes. |
| 75 | * |
| 76 | * @access public |
| 77 | * @param string |
| 78 | * @param integer |
| 79 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 80 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | if ( ! function_exists('mdate')) |
| 82 | { |
| 83 | function mdate($datestr = '', $time = '') |
| 84 | { |
| 85 | if ($datestr == '') |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 86 | { |
| 87 | return ''; |
| 88 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 90 | $time = ($time == '') ? now() : $time; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 91 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 92 | $datestr = str_replace( |
| 93 | '%\\', |
| 94 | '', |
| 95 | preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr) |
| 96 | ); |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 97 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | return date($datestr, $time); |
| 99 | } |
| 100 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 101 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | // ------------------------------------------------------------------------ |
| 103 | |
| 104 | /** |
| 105 | * Standard Date |
| 106 | * |
| 107 | * Returns a date formatted according to the submitted standard. |
| 108 | * |
| 109 | * @access public |
| 110 | * @param string the chosen format |
| 111 | * @param integer Unix timestamp |
| 112 | * @return string |
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 | if ( ! function_exists('standard_date')) |
| 115 | { |
| 116 | function standard_date($fmt = 'DATE_RFC822', $time = '') |
| 117 | { |
| 118 | $formats = array( |
| 119 | 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q', |
| 120 | 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC', |
Greg Aker | 3b889a9 | 2011-01-10 12:35:57 -0600 | [diff] [blame] | 121 | 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%Q', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O', |
| 123 | 'DATE_RFC850' => '%l, %d-%M-%y %H:%m:%i UTC', |
| 124 | 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O', |
| 125 | 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O', |
| 126 | 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O', |
| 127 | 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q' |
| 128 | ); |
| 129 | |
| 130 | if ( ! isset($formats[$fmt])) |
| 131 | { |
| 132 | return FALSE; |
| 133 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 134 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | return mdate($formats[$fmt], $time); |
| 136 | } |
| 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 | // ------------------------------------------------------------------------ |
| 140 | |
| 141 | /** |
| 142 | * Timespan |
| 143 | * |
| 144 | * Returns a span of seconds in this format: |
| 145 | * 10 days 14 hours 36 minutes 47 seconds |
| 146 | * |
| 147 | * @access public |
| 148 | * @param integer a number of seconds |
| 149 | * @param integer Unix timestamp |
| 150 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | if ( ! function_exists('timespan')) |
| 153 | { |
| 154 | function timespan($seconds = 1, $time = '') |
| 155 | { |
| 156 | $CI =& get_instance(); |
| 157 | $CI->lang->load('date'); |
| 158 | |
| 159 | if ( ! is_numeric($seconds)) |
| 160 | { |
| 161 | $seconds = 1; |
| 162 | } |
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 ( ! is_numeric($time)) |
| 165 | { |
| 166 | $time = time(); |
| 167 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 168 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 169 | $seconds = ($time <= $seconds) ? 1 : $time - $seconds; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 170 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | $str = ''; |
| 172 | $years = floor($seconds / 31536000); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 173 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | if ($years > 0) |
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 | $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | $seconds -= $years * 31536000; |
| 180 | $months = floor($seconds / 2628000); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | if ($years > 0 OR $months > 0) |
| 183 | { |
| 184 | if ($months > 0) |
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 | $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | $seconds -= $months * 2628000; |
| 190 | } |
| 191 | |
| 192 | $weeks = floor($seconds / 604800); |
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 OR $weeks > 0) |
| 195 | { |
| 196 | if ($weeks > 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 .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', '; |
| 199 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 200 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | $seconds -= $weeks * 604800; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | |
| 204 | $days = floor($seconds / 86400); |
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 ($months > 0 OR $weeks > 0 OR $days > 0) |
| 207 | { |
| 208 | if ($days > 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 .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', '; |
| 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 -= $days * 86400; |
| 214 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 215 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | $hours = floor($seconds / 3600); |
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 ($days > 0 OR $hours > 0) |
| 219 | { |
| 220 | if ($hours > 0) |
| 221 | { |
| 222 | $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', '; |
| 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 -= $hours * 3600; |
| 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 | $minutes = floor($seconds / 60); |
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 OR $minutes > 0) |
| 231 | { |
| 232 | if ($minutes > 0) |
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 | $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', '; |
| 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 -= $minutes * 60; |
| 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 | if ($str == '') |
| 241 | { |
| 242 | $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', '; |
| 243 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 245 | return substr(trim($str), 0, -1); |
| 246 | } |
| 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 | // ------------------------------------------------------------------------ |
| 250 | |
| 251 | /** |
| 252 | * Number of days in a month |
| 253 | * |
| 254 | * Takes a month/year as input and returns the number of days |
| 255 | * for the given month/year. Takes leap years into consideration. |
| 256 | * |
| 257 | * @access public |
| 258 | * @param integer a numeric month |
| 259 | * @param integer a numeric year |
| 260 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 261 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | if ( ! function_exists('days_in_month')) |
| 263 | { |
| 264 | function days_in_month($month = 0, $year = '') |
| 265 | { |
| 266 | if ($month < 1 OR $month > 12) |
| 267 | { |
| 268 | return 0; |
| 269 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 270 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | if ( ! is_numeric($year) OR strlen($year) != 4) |
| 272 | { |
| 273 | $year = date('Y'); |
| 274 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 275 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | if ($month == 2) |
| 277 | { |
| 278 | if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0)) |
| 279 | { |
| 280 | return 29; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
| 285 | return $days_in_month[$month - 1]; |
| 286 | } |
| 287 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 288 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 289 | // ------------------------------------------------------------------------ |
| 290 | |
| 291 | /** |
| 292 | * Converts a local Unix timestamp to GMT |
| 293 | * |
| 294 | * @access public |
| 295 | * @param integer Unix timestamp |
| 296 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 297 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | if ( ! function_exists('local_to_gmt')) |
| 299 | { |
| 300 | function local_to_gmt($time = '') |
| 301 | { |
| 302 | if ($time == '') |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 303 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | $time = time(); |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | return mktime( |
| 308 | gmdate("H", $time), |
| 309 | gmdate("i", $time), |
| 310 | gmdate("s", $time), |
| 311 | gmdate("m", $time), |
| 312 | gmdate("d", $time), |
| 313 | gmdate("Y", $time) |
| 314 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 317 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | // ------------------------------------------------------------------------ |
| 319 | |
| 320 | /** |
| 321 | * Converts GMT time to a localized value |
| 322 | * |
| 323 | * Takes a Unix timestamp (in GMT) as input, and returns |
| 324 | * at the local value based on the timezone and DST setting |
| 325 | * submitted |
| 326 | * |
| 327 | * @access public |
| 328 | * @param integer Unix timestamp |
| 329 | * @param string timezone |
| 330 | * @param bool whether DST is active |
| 331 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 332 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | if ( ! function_exists('gmt_to_local')) |
| 334 | { |
| 335 | function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE) |
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 ($time == '') |
| 338 | { |
| 339 | return now(); |
| 340 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 341 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | $time += timezones($timezone) * 3600; |
| 343 | |
| 344 | if ($dst == TRUE) |
| 345 | { |
| 346 | $time += 3600; |
| 347 | } |
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 | return $time; |
| 350 | } |
| 351 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 352 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | // ------------------------------------------------------------------------ |
| 354 | |
| 355 | /** |
| 356 | * Converts a MySQL Timestamp to Unix |
| 357 | * |
| 358 | * @access public |
| 359 | * @param integer Unix timestamp |
| 360 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 361 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | if ( ! function_exists('mysql_to_unix')) |
| 363 | { |
| 364 | function mysql_to_unix($time = '') |
| 365 | { |
| 366 | // We'll remove certain characters for backward compatibility |
| 367 | // since the formatting changed with MySQL 4.1 |
| 368 | // YYYY-MM-DD HH:MM:SS |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 369 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | $time = str_replace('-', '', $time); |
| 371 | $time = str_replace(':', '', $time); |
| 372 | $time = str_replace(' ', '', $time); |
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 | // YYYYMMDDHHMMSS |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 375 | return mktime( |
| 376 | substr($time, 8, 2), |
| 377 | substr($time, 10, 2), |
| 378 | substr($time, 12, 2), |
| 379 | substr($time, 4, 2), |
| 380 | substr($time, 6, 2), |
| 381 | substr($time, 0, 4) |
| 382 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
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 | // ------------------------------------------------------------------------ |
| 387 | |
| 388 | /** |
| 389 | * Unix to "Human" |
| 390 | * |
| 391 | * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM |
| 392 | * |
| 393 | * @access public |
| 394 | * @param integer Unix timestamp |
| 395 | * @param bool whether to show seconds |
| 396 | * @param string format: us or euro |
| 397 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 398 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | if ( ! function_exists('unix_to_human')) |
| 400 | { |
| 401 | function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us') |
| 402 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 403 | $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 404 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | if ($fmt == 'us') |
| 406 | { |
| 407 | $r .= date('h', $time).':'.date('i', $time); |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | $r .= date('H', $time).':'.date('i', $time); |
| 412 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 413 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | if ($seconds) |
| 415 | { |
| 416 | $r .= ':'.date('s', $time); |
| 417 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 418 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | if ($fmt == 'us') |
| 420 | { |
| 421 | $r .= ' '.date('A', $time); |
| 422 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 423 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | return $r; |
| 425 | } |
| 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 | // ------------------------------------------------------------------------ |
| 429 | |
| 430 | /** |
| 431 | * Convert "human" date to GMT |
| 432 | * |
| 433 | * Reverses the above process |
| 434 | * |
| 435 | * @access public |
| 436 | * @param string format: us or euro |
| 437 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 438 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | if ( ! function_exists('human_to_unix')) |
| 440 | { |
| 441 | function human_to_unix($datestr = '') |
| 442 | { |
| 443 | if ($datestr == '') |
| 444 | { |
| 445 | return FALSE; |
| 446 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 447 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | $datestr = trim($datestr); |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 449 | $datestr = preg_replace("/\040+/", ' ', $datestr); |
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 ( ! 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)) |
| 452 | { |
| 453 | return FALSE; |
| 454 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 455 | |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 456 | $split = explode(' ', $datestr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | |
| 458 | $ex = explode("-", $split['0']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 459 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 460 | $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0']; |
| 461 | $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1']; |
| 462 | $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | |
| 464 | $ex = explode(":", $split['1']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 465 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0']; |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 467 | $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | |
Derek Jones | 1322ec5 | 2009-03-11 17:01:14 +0000 | [diff] [blame] | 469 | if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 471 | $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | } |
| 473 | else |
| 474 | { |
| 475 | // Unless specified, seconds get set to zero. |
| 476 | $sec = '00'; |
| 477 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 478 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | if (isset($split['2'])) |
| 480 | { |
| 481 | $ampm = strtolower($split['2']); |
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 (substr($ampm, 0, 1) == 'p' AND $hour < 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 484 | { |
| 485 | $hour = $hour + 12; |
| 486 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 487 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | if (substr($ampm, 0, 1) == 'a' AND $hour == 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 489 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 490 | $hour = '00'; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 491 | } |
| 492 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | if (strlen($hour) == 1) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 494 | { |
| 495 | $hour = '0'.$hour; |
| 496 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 497 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 498 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | return mktime($hour, $min, $sec, $month, $day, $year); |
| 500 | } |
| 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 | // ------------------------------------------------------------------------ |
| 504 | |
| 505 | /** |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 506 | * Turns many "reasonably-date-like" strings into something |
| 507 | * that is actually useful. This only works for dates after unix epoch. |
| 508 | * |
| 509 | * @access public |
| 510 | * @param string The terribly formatted date-like string |
| 511 | * @param string Date format to return (same as php date function) |
| 512 | * @return string |
| 513 | */ |
| 514 | if ( ! function_exists('nice_date')) |
| 515 | { |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 516 | function nice_date($bad_date = '', $format = FALSE) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 517 | { |
| 518 | if (empty($bad_date)) |
| 519 | { |
| 520 | return 'Unknown'; |
| 521 | } |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 522 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 523 | // Date like: YYYYMM |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 524 | if (preg_match('/^\d{6}$/', $bad_date)) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 525 | { |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 526 | if (in_array(substr($bad_date, 0, 2),array('19', '20'))) |
| 527 | { |
| 528 | $year = substr($bad_date, 0, 4); |
| 529 | $month = substr($bad_date, 4, 2); |
| 530 | } |
| 531 | else |
| 532 | { |
| 533 | $month = substr($bad_date, 0, 2); |
| 534 | $year = substr($bad_date, 2, 4); |
| 535 | } |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 536 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 537 | return date($format, strtotime($year . '-' . $month . '-01')); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // Date Like: YYYYMMDD |
| 541 | if (preg_match('/^\d{8}$/',$bad_date)) |
| 542 | { |
| 543 | $month = substr($bad_date, 0, 2); |
| 544 | $day = substr($bad_date, 2, 2); |
| 545 | $year = substr($bad_date, 4, 4); |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 546 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 547 | return date($format, strtotime($month . '/01/' . $year)); |
| 548 | } |
| 549 | |
| 550 | // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between) |
| 551 | if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date)) |
| 552 | { |
| 553 | list($m, $d, $y) = explode('-', $bad_date); |
| 554 | return date($format, strtotime("{$y}-{$m}-{$d}")); |
| 555 | } |
| 556 | |
| 557 | // Any other kind of string, when converted into UNIX time, |
| 558 | // produces "0 seconds after epoc..." is probably bad... |
| 559 | // return "Invalid Date". |
| 560 | if (date('U', strtotime($bad_date)) == '0') |
| 561 | { |
| 562 | return "Invalid Date"; |
| 563 | } |
| 564 | |
| 565 | // It's probably a valid-ish date format already |
| 566 | return date($format, strtotime($bad_date)); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | // ------------------------------------------------------------------------ |
| 571 | |
| 572 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 573 | * Timezone Menu |
| 574 | * |
| 575 | * Generates a drop-down menu of timezones. |
| 576 | * |
| 577 | * @access public |
| 578 | * @param string timezone |
| 579 | * @param string classname |
| 580 | * @param string menu name |
| 581 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 582 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 583 | if ( ! function_exists('timezone_menu')) |
| 584 | { |
| 585 | function timezone_menu($default = 'UTC', $class = "", $name = 'timezones') |
| 586 | { |
| 587 | $CI =& get_instance(); |
| 588 | $CI->lang->load('date'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 589 | |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 590 | $default = ($default == 'GMT') ? 'UTC' : $default; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | |
| 592 | $menu = '<select name="'.$name.'"'; |
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 ($class != '') |
| 595 | { |
| 596 | $menu .= ' class="'.$class.'"'; |
| 597 | } |
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 | $menu .= ">\n"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 600 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 601 | foreach (timezones() as $key => $val) |
| 602 | { |
| 603 | $selected = ($default == $key) ? " selected='selected'" : ''; |
| 604 | $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n"; |
| 605 | } |
| 606 | |
| 607 | $menu .= "</select>"; |
| 608 | |
| 609 | return $menu; |
| 610 | } |
| 611 | } |
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 | // ------------------------------------------------------------------------ |
| 614 | |
| 615 | /** |
| 616 | * Timezones |
| 617 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 618 | * Returns an array of timezones. This is a helper function |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 619 | * for various other ones in this library |
| 620 | * |
| 621 | * @access public |
| 622 | * @param string timezone |
| 623 | * @return string |
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 | if ( ! 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 631 | |
| 632 | $zones = array( |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 633 | '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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 674 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 675 | if ($tz == '') |
| 676 | { |
| 677 | return $zones; |
| 678 | } |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 679 | |
| 680 | $tz = ($tz == 'GMT') ? 'UTC' : $tz; |
| 681 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 682 | return ( ! isset($zones[$tz])) ? 0 : $zones[$tz]; |
| 683 | } |
| 684 | } |
| 685 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 686 | /* End of file date_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 687 | /* Location: ./system/helpers/date_helper.php */ |