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 | */ |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame^] | 124 | function standard_date($fmt = 'DATE_RFC822', $time = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | { |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame^] | 126 | if (empty($time)) |
| 127 | { |
| 128 | $time = now(); |
| 129 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame^] | 131 | // Procedural style pre-defined constants from the DateTime extension |
| 132 | if (strpos($fmt, 'DATE_') !== 0 OR defined($fmt) === FALSE) |
| 133 | { |
| 134 | return FALSE; |
| 135 | } |
| 136 | |
| 137 | return date(constant($fmt), $time); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 140 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | // ------------------------------------------------------------------------ |
| 142 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | if ( ! function_exists('timespan')) |
| 144 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 145 | /** |
| 146 | * Timespan |
| 147 | * |
| 148 | * Returns a span of seconds in this format: |
| 149 | * 10 days 14 hours 36 minutes 47 seconds |
| 150 | * |
| 151 | * @param int a number of seconds |
| 152 | * @param int Unix timestamp |
| 153 | * @param int a number of display units |
| 154 | * @return string |
| 155 | */ |
Roger Herbert | 8d69aa1 | 2012-03-14 08:44:55 +0000 | [diff] [blame] | 156 | function timespan($seconds = 1, $time = '', $units = 7) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | { |
| 158 | $CI =& get_instance(); |
| 159 | $CI->lang->load('date'); |
| 160 | |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 161 | is_numeric($seconds) OR $seconds = 1; |
| 162 | is_numeric($time) OR $time = time(); |
| 163 | is_numeric($units) OR $units = 7; |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 164 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 165 | $seconds = ($time <= $seconds) ? 1 : $time - $seconds; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 167 | $str = array(); |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 168 | $years = floor($seconds / 31557600); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | if ($years > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 171 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 172 | $str[] = $years.' '.$CI->lang->line($years > 1 ? 'date_years' : 'date_year'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 175 | $seconds -= $years * 31557600; |
| 176 | $months = floor($seconds / 2629743); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 178 | if (count($str) < $units && ($years > 0 OR $months > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | { |
| 180 | if ($months > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 182 | $str[] = $months.' '.$CI->lang->line($months > 1 ? 'date_months' : 'date_month'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 185 | $seconds -= $months * 2629743; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | $weeks = floor($seconds / 604800); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 190 | if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | { |
| 192 | if ($weeks > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 194 | $str[] = $weeks.' '.$CI->lang->line($weeks > 1 ? 'date_weeks' : 'date_week'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | $seconds -= $weeks * 604800; |
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 | |
| 200 | $days = floor($seconds / 86400); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 202 | if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | { |
| 204 | if ($days > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 206 | $str[] = $days.' '.$CI->lang->line($days > 1 ? 'date_days' : 'date_day'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 208 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | $seconds -= $days * 86400; |
| 210 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | $hours = floor($seconds / 3600); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 213 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 214 | if (count($str) < $units && ($days > 0 OR $hours > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | { |
| 216 | if ($hours > 0) |
| 217 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 218 | $str[] = $hours.' '.$CI->lang->line($hours > 1 ? 'date_hours' : 'date_hour'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | $seconds -= $hours * 3600; |
| 222 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | $minutes = floor($seconds / 60); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 226 | if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | { |
| 228 | if ($minutes > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 229 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 230 | $str[] = $minutes.' '.$CI->lang->line($minutes > 1 ? 'date_minutes' : 'date_minute'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | $seconds -= $minutes * 60; |
| 234 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 235 | |
Roger Herbert | 597eb21 | 2012-03-14 09:06:17 +0000 | [diff] [blame] | 236 | if (count($str) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 238 | $str[] = $seconds.' '.$CI->lang->line($seconds > 1 ? 'date_seconds' : 'date_second'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 240 | |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 241 | return implode(', ', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | } |
| 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 | // ------------------------------------------------------------------------ |
| 246 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | if ( ! function_exists('days_in_month')) |
| 248 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 249 | /** |
| 250 | * Number of days in a month |
| 251 | * |
| 252 | * Takes a month/year as input and returns the number of days |
| 253 | * for the given month/year. Takes leap years into consideration. |
| 254 | * |
| 255 | * @param int a numeric month |
| 256 | * @param int a numeric year |
| 257 | * @return int |
| 258 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | function days_in_month($month = 0, $year = '') |
| 260 | { |
| 261 | if ($month < 1 OR $month > 12) |
| 262 | { |
| 263 | return 0; |
| 264 | } |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 265 | elseif ( ! is_numeric($year) OR strlen($year) !== 4) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | { |
| 267 | $year = date('Y'); |
| 268 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 269 | |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 270 | if ($year >= 1970) |
| 271 | { |
| 272 | return (int) date('t', mktime(12, 0, 0, $month, 1, $year)); |
| 273 | } |
| 274 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | if ($month == 2) |
| 276 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 277 | if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | { |
| 279 | return 29; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
| 284 | return $days_in_month[$month - 1]; |
| 285 | } |
| 286 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 287 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | // ------------------------------------------------------------------------ |
| 289 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | if ( ! function_exists('local_to_gmt')) |
| 291 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 292 | /** |
| 293 | * Converts a local Unix timestamp to GMT |
| 294 | * |
| 295 | * @param int Unix timestamp |
| 296 | * @return int |
| 297 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | function local_to_gmt($time = '') |
| 299 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 300 | if ($time === '') |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 301 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | $time = time(); |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 303 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 304 | |
Andrey Andreev | b089e15 | 2012-06-16 19:11:40 +0300 | [diff] [blame] | 305 | return mktime( |
| 306 | gmdate('G', $time), |
| 307 | gmdate('i', $time), |
| 308 | gmdate('s', $time), |
| 309 | gmdate('n', $time), |
| 310 | gmdate('j', $time), |
| 311 | gmdate('Y', $time) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 312 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 315 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | // ------------------------------------------------------------------------ |
| 317 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | if ( ! function_exists('gmt_to_local')) |
| 319 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 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 | * @param int Unix timestamp |
| 328 | * @param string timezone |
| 329 | * @param bool whether DST is active |
| 330 | * @return int |
| 331 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 334 | if ($time === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | { |
| 336 | return now(); |
| 337 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 338 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | $time += timezones($timezone) * 3600; |
| 340 | |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 341 | return ($dst === TRUE) ? $time + 3600 : $time; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 344 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | // ------------------------------------------------------------------------ |
| 346 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | if ( ! function_exists('mysql_to_unix')) |
| 348 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 349 | /** |
| 350 | * Converts a MySQL Timestamp to Unix |
| 351 | * |
| 352 | * @param int Unix timestamp |
| 353 | * @return int |
| 354 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | function mysql_to_unix($time = '') |
| 356 | { |
| 357 | // We'll remove certain characters for backward compatibility |
| 358 | // since the formatting changed with MySQL 4.1 |
| 359 | // YYYY-MM-DD HH:MM:SS |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 360 | |
Andrey Andreev | 3bbbd26 | 2012-06-14 13:35:32 +0300 | [diff] [blame] | 361 | $time = str_replace(array('-', ':', ' '), '', $time); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 362 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | // YYYYMMDDHHMMSS |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 364 | return mktime( |
| 365 | substr($time, 8, 2), |
| 366 | substr($time, 10, 2), |
| 367 | substr($time, 12, 2), |
| 368 | substr($time, 4, 2), |
| 369 | substr($time, 6, 2), |
| 370 | substr($time, 0, 4) |
| 371 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 374 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | // ------------------------------------------------------------------------ |
| 376 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | if ( ! function_exists('unix_to_human')) |
| 378 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 379 | /** |
| 380 | * Unix to "Human" |
| 381 | * |
| 382 | * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM |
| 383 | * |
| 384 | * @param int Unix timestamp |
| 385 | * @param bool whether to show seconds |
| 386 | * @param string format: us or euro |
| 387 | * @return string |
| 388 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us') |
| 390 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 391 | $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 392 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 393 | if ($fmt === 'us') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | { |
| 395 | $r .= date('h', $time).':'.date('i', $time); |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | $r .= date('H', $time).':'.date('i', $time); |
| 400 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 401 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | if ($seconds) |
| 403 | { |
| 404 | $r .= ':'.date('s', $time); |
| 405 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 406 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 407 | if ($fmt === 'us') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 409 | return $r.' '.date('A', $time); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 411 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | return $r; |
| 413 | } |
| 414 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 415 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | // ------------------------------------------------------------------------ |
| 417 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | if ( ! function_exists('human_to_unix')) |
| 419 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 420 | /** |
| 421 | * Convert "human" date to GMT |
| 422 | * |
| 423 | * Reverses the above process |
| 424 | * |
| 425 | * @param string format: us or euro |
| 426 | * @return int |
| 427 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | function human_to_unix($datestr = '') |
| 429 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 430 | if ($datestr === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | { |
| 432 | return FALSE; |
| 433 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 434 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 435 | $datestr = preg_replace('/\040+/', ' ', trim($datestr)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 436 | |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 437 | 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] | 438 | { |
| 439 | return FALSE; |
| 440 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 441 | |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 442 | $split = explode(' ', $datestr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 444 | list($year, $month, $day) = explode('-', $split[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 446 | $ex = explode(':', $split['1']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 447 | |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 448 | $hour = (int) $ex[0]; |
| 449 | $min = (int) $ex[1]; |
| 450 | $sec = ( ! empty($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2])) |
| 451 | ? (int) $ex[2] : 0; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 452 | |
Andrey Andreev | 5036c9c | 2012-06-04 15:34:56 +0300 | [diff] [blame] | 453 | if (isset($split[2])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 454 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 455 | $ampm = strtolower($split[2]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 456 | |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 457 | if ($ampm[0] === 'p' && $hour < 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 458 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 459 | $hour += 12; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 460 | } |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 461 | elseif ($ampm[0] === 'a' && $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 | $hour = 0; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 464 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 466 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | return mktime($hour, $min, $sec, $month, $day, $year); |
| 468 | } |
| 469 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 470 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | // ------------------------------------------------------------------------ |
| 472 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 473 | if ( ! function_exists('nice_date')) |
| 474 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 475 | /** |
| 476 | * Turns many "reasonably-date-like" strings into something |
| 477 | * that is actually useful. This only works for dates after unix epoch. |
| 478 | * |
| 479 | * @param string The terribly formatted date-like string |
| 480 | * @param string Date format to return (same as php date function) |
| 481 | * @return string |
| 482 | */ |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 483 | function nice_date($bad_date = '', $format = FALSE) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 484 | { |
| 485 | if (empty($bad_date)) |
| 486 | { |
| 487 | return 'Unknown'; |
| 488 | } |
Andrey Andreev | d9b44be | 2012-06-15 16:07:08 +0300 | [diff] [blame] | 489 | elseif (empty($format)) |
| 490 | { |
| 491 | $format = 'U'; |
| 492 | } |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 493 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 494 | // Date like: YYYYMM |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 495 | if (preg_match('/^\d{6}$/i', $bad_date)) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 496 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 497 | if (in_array(substr($bad_date, 0, 2), array('19', '20'))) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 498 | { |
| 499 | $year = substr($bad_date, 0, 4); |
| 500 | $month = substr($bad_date, 4, 2); |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 501 | } |
| 502 | else |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 503 | { |
| 504 | $month = substr($bad_date, 0, 2); |
| 505 | $year = substr($bad_date, 2, 4); |
| 506 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 507 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 508 | return date($format, strtotime($year.'-'.$month.'-01')); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 509 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 510 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 511 | // Date Like: YYYYMMDD |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 512 | 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] | 513 | { |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 514 | return date($format, strtotime($matches[1].'/01/'.$matches[2])); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 515 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 516 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 517 | // 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] | 518 | 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] | 519 | { |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 520 | return date($format, strtotime($matches[3].'-'.$matches[1].'-'.$matches[2])); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 521 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 522 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 523 | // Any other kind of string, when converted into UNIX time, |
| 524 | // produces "0 seconds after epoc..." is probably bad... |
| 525 | // return "Invalid Date". |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 526 | if (date('U', strtotime($bad_date)) === '0') |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 527 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 528 | return 'Invalid Date'; |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 529 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 530 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 531 | // It's probably a valid-ish date format already |
| 532 | return date($format, strtotime($bad_date)); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | // ------------------------------------------------------------------------ |
| 537 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 538 | if ( ! function_exists('timezone_menu')) |
| 539 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 540 | /** |
| 541 | * Timezone Menu |
| 542 | * |
| 543 | * Generates a drop-down menu of timezones. |
| 544 | * |
| 545 | * @param string timezone |
| 546 | * @param string classname |
| 547 | * @param string menu name |
Mat Whitney | 7540ded | 2012-06-22 12:02:10 -0700 | [diff] [blame] | 548 | * @param mixed attributes |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 549 | * @return string |
| 550 | */ |
Mat Whitney | 7540ded | 2012-06-22 12:02:10 -0700 | [diff] [blame] | 551 | function timezone_menu($default = 'UTC', $class = '', $name = 'timezones', $attributes = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | { |
| 553 | $CI =& get_instance(); |
| 554 | $CI->lang->load('date'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 555 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 556 | $default = ($default === 'GMT') ? 'UTC' : $default; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 557 | |
| 558 | $menu = '<select name="'.$name.'"'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 559 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 560 | if ($class !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 561 | { |
| 562 | $menu .= ' class="'.$class.'"'; |
| 563 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 564 | |
Mat Whitney | 7540ded | 2012-06-22 12:02:10 -0700 | [diff] [blame] | 565 | // Generate a string from the attributes submitted, if any |
| 566 | if (is_array($attributes)) |
| 567 | { |
| 568 | $atts = ''; |
| 569 | foreach ($attributes as $key => $val) |
| 570 | { |
| 571 | $atts .= ' '.$key.'="'.$val.'"'; |
| 572 | } |
| 573 | $attributes = $atts; |
| 574 | } |
| 575 | elseif (is_string($attributes) && strlen($attributes) > 0) |
| 576 | { |
| 577 | $attributes = ' '.$attributes; |
| 578 | } |
| 579 | |
| 580 | $menu .= $attributes.">\n"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 581 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | foreach (timezones() as $key => $val) |
| 583 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 584 | $selected = ($default === $key) ? ' selected="selected"' : ''; |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 585 | $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 588 | return $menu.'</select>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 591 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 592 | // ------------------------------------------------------------------------ |
| 593 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 594 | if ( ! function_exists('timezones')) |
| 595 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 596 | /** |
| 597 | * Timezones |
| 598 | * |
| 599 | * Returns an array of timezones. This is a helper function |
| 600 | * for various other ones in this library |
| 601 | * |
| 602 | * @param string timezone |
| 603 | * @return string |
| 604 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 605 | function timezones($tz = '') |
| 606 | { |
| 607 | // Note: Don't change the order of these even though |
| 608 | // some items appear to be in the wrong order |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 609 | |
| 610 | $zones = array( |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 611 | 'UM12' => -12, |
| 612 | 'UM11' => -11, |
| 613 | 'UM10' => -10, |
| 614 | 'UM95' => -9.5, |
| 615 | 'UM9' => -9, |
| 616 | 'UM8' => -8, |
| 617 | 'UM7' => -7, |
| 618 | 'UM6' => -6, |
| 619 | 'UM5' => -5, |
| 620 | 'UM45' => -4.5, |
| 621 | 'UM4' => -4, |
| 622 | 'UM35' => -3.5, |
| 623 | 'UM3' => -3, |
| 624 | 'UM2' => -2, |
| 625 | 'UM1' => -1, |
| 626 | 'UTC' => 0, |
| 627 | 'UP1' => +1, |
| 628 | 'UP2' => +2, |
| 629 | 'UP3' => +3, |
| 630 | 'UP35' => +3.5, |
| 631 | 'UP4' => +4, |
| 632 | 'UP45' => +4.5, |
| 633 | 'UP5' => +5, |
| 634 | 'UP55' => +5.5, |
| 635 | 'UP575' => +5.75, |
| 636 | 'UP6' => +6, |
| 637 | 'UP65' => +6.5, |
| 638 | 'UP7' => +7, |
| 639 | 'UP8' => +8, |
| 640 | 'UP875' => +8.75, |
| 641 | 'UP9' => +9, |
| 642 | 'UP95' => +9.5, |
| 643 | 'UP10' => +10, |
| 644 | 'UP105' => +10.5, |
| 645 | 'UP11' => +11, |
| 646 | 'UP115' => +11.5, |
| 647 | 'UP12' => +12, |
| 648 | 'UP1275' => +12.75, |
| 649 | 'UP13' => +13, |
| 650 | 'UP14' => +14 |
| 651 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 652 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 653 | if ($tz === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 654 | { |
| 655 | return $zones; |
| 656 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 657 | |
Andrey Andreev | e92df33 | 2012-03-26 22:44:20 +0300 | [diff] [blame] | 658 | return isset($zones[$tz]) ? $zones[$tz] : 0; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 662 | /* End of file date_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 663 | /* Location: ./system/helpers/date_helper.php */ |