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