Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -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 | 37f4b9c | 2011-07-01 17:56:50 -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 == '') |
| 88 | return ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | if ($time == '') |
| 91 | $time = now(); |
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 | $datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr)); |
| 94 | return date($datestr, $time); |
| 95 | } |
| 96 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 97 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | // ------------------------------------------------------------------------ |
| 99 | |
| 100 | /** |
| 101 | * Standard Date |
| 102 | * |
| 103 | * Returns a date formatted according to the submitted standard. |
| 104 | * |
| 105 | * @access public |
| 106 | * @param string the chosen format |
| 107 | * @param integer Unix timestamp |
| 108 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 109 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | if ( ! function_exists('standard_date')) |
| 111 | { |
| 112 | function standard_date($fmt = 'DATE_RFC822', $time = '') |
| 113 | { |
| 114 | $formats = array( |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 115 | 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC', |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 117 | 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O', |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 119 | 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O', |
| 121 | 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O', |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 122 | 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O', |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 124 | 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | ); |
| 126 | |
| 127 | if ( ! isset($formats[$fmt])) |
| 128 | { |
| 129 | return FALSE; |
| 130 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 131 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | return mdate($formats[$fmt], $time); |
| 133 | } |
| 134 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | // ------------------------------------------------------------------------ |
| 137 | |
| 138 | /** |
| 139 | * Timespan |
| 140 | * |
| 141 | * Returns a span of seconds in this format: |
| 142 | * 10 days 14 hours 36 minutes 47 seconds |
| 143 | * |
| 144 | * @access public |
| 145 | * @param integer a number of seconds |
| 146 | * @param integer Unix timestamp |
| 147 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 148 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | if ( ! function_exists('timespan')) |
| 150 | { |
| 151 | function timespan($seconds = 1, $time = '') |
| 152 | { |
| 153 | $CI =& get_instance(); |
| 154 | $CI->lang->load('date'); |
| 155 | |
| 156 | if ( ! is_numeric($seconds)) |
| 157 | { |
| 158 | $seconds = 1; |
| 159 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 160 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | if ( ! is_numeric($time)) |
| 162 | { |
| 163 | $time = time(); |
| 164 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 165 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | if ($time <= $seconds) |
| 167 | { |
| 168 | $seconds = 1; |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | $seconds = $time - $seconds; |
| 173 | } |
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 == '') |
| 307 | $time = time(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 308 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time)); |
| 310 | } |
| 311 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 312 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 313 | // ------------------------------------------------------------------------ |
| 314 | |
| 315 | /** |
| 316 | * Converts GMT time to a localized value |
| 317 | * |
| 318 | * Takes a Unix timestamp (in GMT) as input, and returns |
| 319 | * at the local value based on the timezone and DST setting |
| 320 | * submitted |
| 321 | * |
| 322 | * @access public |
| 323 | * @param integer Unix timestamp |
| 324 | * @param string timezone |
| 325 | * @param bool whether DST is active |
| 326 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 327 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | if ( ! function_exists('gmt_to_local')) |
| 329 | { |
| 330 | function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | if ($time == '') |
| 333 | { |
| 334 | return now(); |
| 335 | } |
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 | $time += timezones($timezone) * 3600; |
| 338 | |
| 339 | if ($dst == TRUE) |
| 340 | { |
| 341 | $time += 3600; |
| 342 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 343 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | return $time; |
| 345 | } |
| 346 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 347 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | // ------------------------------------------------------------------------ |
| 349 | |
| 350 | /** |
| 351 | * Converts a MySQL Timestamp to Unix |
| 352 | * |
| 353 | * @access public |
| 354 | * @param integer Unix timestamp |
| 355 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 356 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | if ( ! function_exists('mysql_to_unix')) |
| 358 | { |
| 359 | function mysql_to_unix($time = '') |
| 360 | { |
| 361 | // We'll remove certain characters for backward compatibility |
| 362 | // since the formatting changed with MySQL 4.1 |
| 363 | // YYYY-MM-DD HH:MM:SS |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 364 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | $time = str_replace('-', '', $time); |
| 366 | $time = str_replace(':', '', $time); |
| 367 | $time = str_replace(' ', '', $time); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 368 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | // YYYYMMDDHHMMSS |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 370 | return mktime( |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | substr($time, 8, 2), |
| 372 | substr($time, 10, 2), |
| 373 | substr($time, 12, 2), |
| 374 | substr($time, 4, 2), |
| 375 | substr($time, 6, 2), |
| 376 | substr($time, 0, 4) |
| 377 | ); |
| 378 | } |
| 379 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 380 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 381 | // ------------------------------------------------------------------------ |
| 382 | |
| 383 | /** |
| 384 | * Unix to "Human" |
| 385 | * |
| 386 | * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM |
| 387 | * |
| 388 | * @access public |
| 389 | * @param integer Unix timestamp |
| 390 | * @param bool whether to show seconds |
| 391 | * @param string format: us or euro |
| 392 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 393 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | if ( ! function_exists('unix_to_human')) |
| 395 | { |
| 396 | function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us') |
| 397 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 398 | $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 399 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | if ($fmt == 'us') |
| 401 | { |
| 402 | $r .= date('h', $time).':'.date('i', $time); |
| 403 | } |
| 404 | else |
| 405 | { |
| 406 | $r .= date('H', $time).':'.date('i', $time); |
| 407 | } |
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 ($seconds) |
| 410 | { |
| 411 | $r .= ':'.date('s', $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 ($fmt == 'us') |
| 415 | { |
| 416 | $r .= ' '.date('A', $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 | return $r; |
| 420 | } |
| 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 | // ------------------------------------------------------------------------ |
| 424 | |
| 425 | /** |
| 426 | * Convert "human" date to GMT |
| 427 | * |
| 428 | * Reverses the above process |
| 429 | * |
| 430 | * @access public |
| 431 | * @param string format: us or euro |
| 432 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 433 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | if ( ! function_exists('human_to_unix')) |
| 435 | { |
| 436 | function human_to_unix($datestr = '') |
| 437 | { |
| 438 | if ($datestr == '') |
| 439 | { |
| 440 | return FALSE; |
| 441 | } |
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 | $datestr = trim($datestr); |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 444 | $datestr = preg_replace("/\040+/", ' ', $datestr); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 445 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | 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)) |
| 447 | { |
| 448 | return FALSE; |
| 449 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 450 | |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 451 | $split = explode(' ', $datestr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | |
| 453 | $ex = explode("-", $split['0']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 454 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 455 | $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0']; |
| 456 | $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1']; |
| 457 | $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | |
| 459 | $ex = explode(":", $split['1']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 460 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0']; |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 462 | $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | |
Derek Jones | 1322ec5 | 2009-03-11 17:01:14 +0000 | [diff] [blame] | 464 | if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 466 | $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | } |
| 468 | else |
| 469 | { |
| 470 | // Unless specified, seconds get set to zero. |
| 471 | $sec = '00'; |
| 472 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 473 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | if (isset($split['2'])) |
| 475 | { |
| 476 | $ampm = strtolower($split['2']); |
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 | if (substr($ampm, 0, 1) == 'p' AND $hour < 12) |
| 479 | $hour = $hour + 12; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 480 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | if (substr($ampm, 0, 1) == 'a' AND $hour == 12) |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 482 | $hour = '00'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 483 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | if (strlen($hour) == 1) |
| 485 | $hour = '0'.$hour; |
| 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 | return mktime($hour, $min, $sec, $month, $day, $year); |
| 489 | } |
| 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 | // ------------------------------------------------------------------------ |
| 493 | |
| 494 | /** |
| 495 | * Timezone Menu |
| 496 | * |
| 497 | * Generates a drop-down menu of timezones. |
| 498 | * |
| 499 | * @access public |
| 500 | * @param string timezone |
| 501 | * @param string classname |
| 502 | * @param string menu name |
| 503 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 504 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | if ( ! function_exists('timezone_menu')) |
| 506 | { |
| 507 | function timezone_menu($default = 'UTC', $class = "", $name = 'timezones') |
| 508 | { |
| 509 | $CI =& get_instance(); |
| 510 | $CI->lang->load('date'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 511 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 512 | if ($default == 'GMT') |
| 513 | $default = 'UTC'; |
| 514 | |
| 515 | $menu = '<select name="'.$name.'"'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 516 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | if ($class != '') |
| 518 | { |
| 519 | $menu .= ' class="'.$class.'"'; |
| 520 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 521 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | $menu .= ">\n"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 523 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | foreach (timezones() as $key => $val) |
| 525 | { |
| 526 | $selected = ($default == $key) ? " selected='selected'" : ''; |
| 527 | $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n"; |
| 528 | } |
| 529 | |
| 530 | $menu .= "</select>"; |
| 531 | |
| 532 | return $menu; |
| 533 | } |
| 534 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 535 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | // ------------------------------------------------------------------------ |
| 537 | |
| 538 | /** |
| 539 | * Timezones |
| 540 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 541 | * Returns an array of timezones. This is a helper function |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 542 | * for various other ones in this library |
| 543 | * |
| 544 | * @access public |
| 545 | * @param string timezone |
| 546 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 547 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 548 | if ( ! function_exists('timezones')) |
| 549 | { |
| 550 | function timezones($tz = '') |
| 551 | { |
| 552 | // Note: Don't change the order of these even though |
| 553 | // some items appear to be in the wrong order |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 554 | |
| 555 | $zones = array( |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 556 | 'UM12' => -12, |
| 557 | 'UM11' => -11, |
| 558 | 'UM10' => -10, |
| 559 | 'UM95' => -9.5, |
| 560 | 'UM9' => -9, |
| 561 | 'UM8' => -8, |
| 562 | 'UM7' => -7, |
| 563 | 'UM6' => -6, |
| 564 | 'UM5' => -5, |
| 565 | 'UM45' => -4.5, |
| 566 | 'UM4' => -4, |
| 567 | 'UM35' => -3.5, |
| 568 | 'UM3' => -3, |
| 569 | 'UM2' => -2, |
| 570 | 'UM1' => -1, |
| 571 | 'UTC' => 0, |
| 572 | 'UP1' => +1, |
| 573 | 'UP2' => +2, |
| 574 | 'UP3' => +3, |
| 575 | 'UP35' => +3.5, |
| 576 | 'UP4' => +4, |
| 577 | 'UP45' => +4.5, |
| 578 | 'UP5' => +5, |
| 579 | 'UP55' => +5.5, |
| 580 | 'UP575' => +5.75, |
| 581 | 'UP6' => +6, |
| 582 | 'UP65' => +6.5, |
| 583 | 'UP7' => +7, |
| 584 | 'UP8' => +8, |
| 585 | 'UP875' => +8.75, |
| 586 | 'UP9' => +9, |
| 587 | 'UP95' => +9.5, |
| 588 | 'UP10' => +10, |
| 589 | 'UP105' => +10.5, |
| 590 | 'UP11' => +11, |
| 591 | 'UP115' => +11.5, |
| 592 | 'UP12' => +12, |
| 593 | 'UP1275' => +12.75, |
| 594 | 'UP13' => +13, |
| 595 | 'UP14' => +14 |
| 596 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 597 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 598 | if ($tz == '') |
| 599 | { |
| 600 | return $zones; |
| 601 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 602 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | if ($tz == 'GMT') |
| 604 | $tz = 'UTC'; |
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 | return ( ! isset($zones[$tz])) ? 0 : $zones[$tz]; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | |
| 611 | /* End of file date_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 612 | /* Location: ./system/helpers/date_helper.php */ |