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)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | { |
Iban Eguia | feb14da | 2012-06-12 16:09:36 +0200 | [diff] [blame] | 55 | $timezone = config_item('time_reference'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | } |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [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 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [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 | * |
Andrey Andreev | ac57033 | 2012-07-04 13:04:10 +0300 | [diff] [blame] | 120 | * As of PHP 5.2, the DateTime extension provides constants that |
| 121 | * serve for the exact same purpose and are used with date(). |
| 122 | * Due to that, this function is DEPRECATED and should be removed |
| 123 | * in CodeIgniter 3.1+. |
| 124 | * |
| 125 | * Here are two examples of how you should replace it: |
| 126 | * |
| 127 | * date(DATE_RFC822, now()); // default |
| 128 | * date(DATE_W3C, $time); // a different format and time |
| 129 | * |
| 130 | * Reference: http://www.php.net/manual/en/class.datetime.php#datetime.constants.types |
| 131 | * |
| 132 | * @deprecated |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 133 | * @param string the chosen format |
| 134 | * @param int Unix timestamp |
| 135 | * @return string |
| 136 | */ |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 137 | function standard_date($fmt = 'DATE_RFC822', $time = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | { |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 139 | if (empty($time)) |
| 140 | { |
| 141 | $time = now(); |
| 142 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 144 | // Procedural style pre-defined constants from the DateTime extension |
| 145 | if (strpos($fmt, 'DATE_') !== 0 OR defined($fmt) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | { |
| 147 | return FALSE; |
| 148 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 150 | return date(constant($fmt), $time); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | // ------------------------------------------------------------------------ |
| 155 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | if ( ! function_exists('timespan')) |
| 157 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 158 | /** |
| 159 | * Timespan |
| 160 | * |
| 161 | * Returns a span of seconds in this format: |
| 162 | * 10 days 14 hours 36 minutes 47 seconds |
| 163 | * |
| 164 | * @param int a number of seconds |
| 165 | * @param int Unix timestamp |
| 166 | * @param int a number of display units |
| 167 | * @return string |
| 168 | */ |
Roger Herbert | 8d69aa1 | 2012-03-14 08:44:55 +0000 | [diff] [blame] | 169 | function timespan($seconds = 1, $time = '', $units = 7) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | { |
| 171 | $CI =& get_instance(); |
| 172 | $CI->lang->load('date'); |
| 173 | |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 174 | is_numeric($seconds) OR $seconds = 1; |
| 175 | is_numeric($time) OR $time = time(); |
| 176 | is_numeric($units) OR $units = 7; |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 177 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 178 | $seconds = ($time <= $seconds) ? 1 : $time - $seconds; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 180 | $str = array(); |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 181 | $years = floor($seconds / 31557600); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 182 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | if ($years > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 184 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 185 | $str[] = $years.' '.$CI->lang->line($years > 1 ? 'date_years' : 'date_year'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 188 | $seconds -= $years * 31557600; |
| 189 | $months = floor($seconds / 2629743); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 190 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 191 | if (count($str) < $units && ($years > 0 OR $months > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | { |
| 193 | if ($months > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 194 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 195 | $str[] = $months.' '.$CI->lang->line($months > 1 ? 'date_months' : 'date_month'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 198 | $seconds -= $months * 2629743; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | $weeks = floor($seconds / 604800); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 203 | if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | { |
| 205 | if ($weeks > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 207 | $str[] = $weeks.' '.$CI->lang->line($weeks > 1 ? 'date_weeks' : 'date_week'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | } |
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 | $seconds -= $weeks * 604800; |
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 | |
| 213 | $days = floor($seconds / 86400); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 214 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 215 | if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | { |
| 217 | if ($days > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 218 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 219 | $str[] = $days.' '.$CI->lang->line($days > 1 ? 'date_days' : 'date_day'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | } |
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 | $seconds -= $days * 86400; |
| 223 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 224 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | $hours = floor($seconds / 3600); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 227 | if (count($str) < $units && ($days > 0 OR $hours > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | { |
| 229 | if ($hours > 0) |
| 230 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 231 | $str[] = $hours.' '.$CI->lang->line($hours > 1 ? 'date_hours' : 'date_hour'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | } |
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 | $seconds -= $hours * 3600; |
| 235 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 236 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | $minutes = floor($seconds / 60); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 238 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 239 | if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | { |
| 241 | if ($minutes > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 242 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 243 | $str[] = $minutes.' '.$CI->lang->line($minutes > 1 ? 'date_minutes' : 'date_minute'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | $seconds -= $minutes * 60; |
| 247 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 248 | |
Roger Herbert | 597eb21 | 2012-03-14 09:06:17 +0000 | [diff] [blame] | 249 | if (count($str) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 251 | $str[] = $seconds.' '.$CI->lang->line($seconds > 1 ? 'date_seconds' : 'date_second'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 253 | |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 254 | return implode(', ', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 257 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | // ------------------------------------------------------------------------ |
| 259 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | if ( ! function_exists('days_in_month')) |
| 261 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 262 | /** |
| 263 | * Number of days in a month |
| 264 | * |
| 265 | * Takes a month/year as input and returns the number of days |
| 266 | * for the given month/year. Takes leap years into consideration. |
| 267 | * |
| 268 | * @param int a numeric month |
| 269 | * @param int a numeric year |
| 270 | * @return int |
| 271 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | function days_in_month($month = 0, $year = '') |
| 273 | { |
| 274 | if ($month < 1 OR $month > 12) |
| 275 | { |
| 276 | return 0; |
| 277 | } |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 278 | elseif ( ! is_numeric($year) OR strlen($year) !== 4) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | { |
| 280 | $year = date('Y'); |
| 281 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 283 | if ($year >= 1970) |
| 284 | { |
| 285 | return (int) date('t', mktime(12, 0, 0, $month, 1, $year)); |
| 286 | } |
| 287 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | if ($month == 2) |
| 289 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 290 | if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | { |
| 292 | return 29; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
| 297 | return $days_in_month[$month - 1]; |
| 298 | } |
| 299 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 300 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | // ------------------------------------------------------------------------ |
| 302 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | if ( ! function_exists('local_to_gmt')) |
| 304 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 305 | /** |
| 306 | * Converts a local Unix timestamp to GMT |
| 307 | * |
| 308 | * @param int Unix timestamp |
| 309 | * @return int |
| 310 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | function local_to_gmt($time = '') |
| 312 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 313 | if ($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 | $time = time(); |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 316 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 317 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 318 | return mktime( |
Andrey Andreev | b089e15 | 2012-06-16 19:11:40 +0300 | [diff] [blame] | 319 | gmdate('G', $time), |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 320 | gmdate('i', $time), |
| 321 | gmdate('s', $time), |
Andrey Andreev | b089e15 | 2012-06-16 19:11:40 +0300 | [diff] [blame] | 322 | gmdate('n', $time), |
| 323 | gmdate('j', $time), |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 324 | gmdate('Y', $time) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 325 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 328 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | // ------------------------------------------------------------------------ |
| 330 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | if ( ! function_exists('gmt_to_local')) |
| 332 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 333 | /** |
| 334 | * Converts GMT time to a localized value |
| 335 | * |
| 336 | * Takes a Unix timestamp (in GMT) as input, and returns |
| 337 | * at the local value based on the timezone and DST setting |
| 338 | * submitted |
| 339 | * |
| 340 | * @param int Unix timestamp |
| 341 | * @param string timezone |
| 342 | * @param bool whether DST is active |
| 343 | * @return int |
| 344 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 346 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 347 | if ($time === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | { |
| 349 | return now(); |
| 350 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 351 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | $time += timezones($timezone) * 3600; |
| 353 | |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 354 | return ($dst === TRUE) ? $time + 3600 : $time; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 357 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | // ------------------------------------------------------------------------ |
| 359 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | if ( ! function_exists('mysql_to_unix')) |
| 361 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 362 | /** |
| 363 | * Converts a MySQL Timestamp to Unix |
| 364 | * |
| 365 | * @param int Unix timestamp |
| 366 | * @return int |
| 367 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | function mysql_to_unix($time = '') |
| 369 | { |
| 370 | // We'll remove certain characters for backward compatibility |
| 371 | // since the formatting changed with MySQL 4.1 |
| 372 | // YYYY-MM-DD HH:MM:SS |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 373 | |
Andrey Andreev | 3bbbd26 | 2012-06-14 13:35:32 +0300 | [diff] [blame] | 374 | $time = str_replace(array('-', ':', ' '), '', $time); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 375 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | // YYYYMMDDHHMMSS |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 377 | return mktime( |
| 378 | substr($time, 8, 2), |
| 379 | substr($time, 10, 2), |
| 380 | substr($time, 12, 2), |
| 381 | substr($time, 4, 2), |
| 382 | substr($time, 6, 2), |
| 383 | substr($time, 0, 4) |
| 384 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 387 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | // ------------------------------------------------------------------------ |
| 389 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | if ( ! function_exists('unix_to_human')) |
| 391 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 392 | /** |
| 393 | * Unix to "Human" |
| 394 | * |
| 395 | * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM |
| 396 | * |
| 397 | * @param int Unix timestamp |
| 398 | * @param bool whether to show seconds |
| 399 | * @param string format: us or euro |
| 400 | * @return string |
| 401 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us') |
| 403 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 404 | $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 405 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 406 | if ($fmt === 'us') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | { |
| 408 | $r .= date('h', $time).':'.date('i', $time); |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | $r .= date('H', $time).':'.date('i', $time); |
| 413 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 414 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | if ($seconds) |
| 416 | { |
| 417 | $r .= ':'.date('s', $time); |
| 418 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 419 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 420 | if ($fmt === 'us') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | { |
Andrey Andreev | eef2406 | 2012-06-14 16:17:48 +0300 | [diff] [blame] | 422 | return $r.' '.date('A', $time); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | return $r; |
| 426 | } |
| 427 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | // ------------------------------------------------------------------------ |
| 430 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | if ( ! function_exists('human_to_unix')) |
| 432 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 433 | /** |
| 434 | * Convert "human" date to GMT |
| 435 | * |
| 436 | * Reverses the above process |
| 437 | * |
| 438 | * @param string format: us or euro |
| 439 | * @return int |
| 440 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | function human_to_unix($datestr = '') |
| 442 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 443 | if ($datestr === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | { |
| 445 | return FALSE; |
| 446 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 447 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 448 | $datestr = preg_replace('/\040+/', ' ', trim($datestr)); |
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 | 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] | 451 | { |
| 452 | return FALSE; |
| 453 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 454 | |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 455 | $split = explode(' ', $datestr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 457 | list($year, $month, $day) = explode('-', $split[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 459 | $ex = explode(':', $split['1']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 460 | |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 461 | $hour = (int) $ex[0]; |
| 462 | $min = (int) $ex[1]; |
| 463 | $sec = ( ! empty($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2])) |
| 464 | ? (int) $ex[2] : 0; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | |
Andrey Andreev | 5036c9c | 2012-06-04 15:34:56 +0300 | [diff] [blame] | 466 | if (isset($split[2])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 468 | $ampm = strtolower($split[2]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 469 | |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 470 | if ($ampm[0] === 'p' && $hour < 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 471 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 472 | $hour += 12; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 473 | } |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 474 | elseif ($ampm[0] === 'a' && $hour === 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 475 | { |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 476 | $hour = 0; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 477 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 479 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | return mktime($hour, $min, $sec, $month, $day, $year); |
| 481 | } |
| 482 | } |
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 | // ------------------------------------------------------------------------ |
| 485 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 486 | if ( ! function_exists('nice_date')) |
| 487 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 488 | /** |
| 489 | * Turns many "reasonably-date-like" strings into something |
| 490 | * that is actually useful. This only works for dates after unix epoch. |
| 491 | * |
| 492 | * @param string The terribly formatted date-like string |
| 493 | * @param string Date format to return (same as php date function) |
| 494 | * @return string |
| 495 | */ |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 496 | function nice_date($bad_date = '', $format = FALSE) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 497 | { |
| 498 | if (empty($bad_date)) |
| 499 | { |
| 500 | return 'Unknown'; |
| 501 | } |
Andrey Andreev | d9b44be | 2012-06-15 16:07:08 +0300 | [diff] [blame] | 502 | elseif (empty($format)) |
| 503 | { |
| 504 | $format = 'U'; |
| 505 | } |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 506 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 507 | // Date like: YYYYMM |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 508 | if (preg_match('/^\d{6}$/i', $bad_date)) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 509 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 510 | if (in_array(substr($bad_date, 0, 2), array('19', '20'))) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 511 | { |
| 512 | $year = substr($bad_date, 0, 4); |
| 513 | $month = substr($bad_date, 4, 2); |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 514 | } |
| 515 | else |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 516 | { |
| 517 | $month = substr($bad_date, 0, 2); |
| 518 | $year = substr($bad_date, 2, 4); |
| 519 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 520 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 521 | return date($format, strtotime($year.'-'.$month.'-01')); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 522 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 523 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 524 | // Date Like: YYYYMMDD |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 525 | 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] | 526 | { |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 527 | return date($format, strtotime($matches[1].'/01/'.$matches[2])); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 528 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 529 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 530 | // 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] | 531 | 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] | 532 | { |
Andrey Andreev | f11a193 | 2012-06-14 16:35:09 +0300 | [diff] [blame] | 533 | return date($format, strtotime($matches[3].'-'.$matches[1].'-'.$matches[2])); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 534 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 535 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 536 | // Any other kind of string, when converted into UNIX time, |
| 537 | // produces "0 seconds after epoc..." is probably bad... |
| 538 | // return "Invalid Date". |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 539 | if (date('U', strtotime($bad_date)) === '0') |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 540 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 541 | return 'Invalid Date'; |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 542 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 543 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 544 | // It's probably a valid-ish date format already |
| 545 | return date($format, strtotime($bad_date)); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // ------------------------------------------------------------------------ |
| 550 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 551 | if ( ! function_exists('timezone_menu')) |
| 552 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 553 | /** |
| 554 | * Timezone Menu |
| 555 | * |
| 556 | * Generates a drop-down menu of timezones. |
| 557 | * |
| 558 | * @param string timezone |
| 559 | * @param string classname |
| 560 | * @param string menu name |
Mat Whitney | 7540ded | 2012-06-22 12:02:10 -0700 | [diff] [blame] | 561 | * @param mixed attributes |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 562 | * @return string |
| 563 | */ |
Mat Whitney | 7540ded | 2012-06-22 12:02:10 -0700 | [diff] [blame] | 564 | function timezone_menu($default = 'UTC', $class = '', $name = 'timezones', $attributes = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 565 | { |
| 566 | $CI =& get_instance(); |
| 567 | $CI->lang->load('date'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 568 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 569 | $default = ($default === 'GMT') ? 'UTC' : $default; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 570 | |
| 571 | $menu = '<select name="'.$name.'"'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 572 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 573 | if ($class !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 574 | { |
| 575 | $menu .= ' class="'.$class.'"'; |
| 576 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 577 | |
Eric Barnes | acedd2b | 2012-07-29 00:15:40 -0400 | [diff] [blame] | 578 | $menu .= _stringify_attributes($attributes).">\n"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 579 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 580 | foreach (timezones() as $key => $val) |
| 581 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 582 | $selected = ($default === $key) ? ' selected="selected"' : ''; |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 583 | $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 586 | return $menu.'</select>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 589 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 590 | // ------------------------------------------------------------------------ |
| 591 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 592 | if ( ! function_exists('timezones')) |
| 593 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 594 | /** |
| 595 | * Timezones |
| 596 | * |
| 597 | * Returns an array of timezones. This is a helper function |
| 598 | * for various other ones in this library |
| 599 | * |
| 600 | * @param string timezone |
| 601 | * @return string |
| 602 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | function timezones($tz = '') |
| 604 | { |
| 605 | // Note: Don't change the order of these even though |
| 606 | // some items appear to be in the wrong order |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 607 | |
| 608 | $zones = array( |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 609 | 'UM12' => -12, |
| 610 | 'UM11' => -11, |
| 611 | 'UM10' => -10, |
| 612 | 'UM95' => -9.5, |
| 613 | 'UM9' => -9, |
| 614 | 'UM8' => -8, |
| 615 | 'UM7' => -7, |
| 616 | 'UM6' => -6, |
| 617 | 'UM5' => -5, |
| 618 | 'UM45' => -4.5, |
| 619 | 'UM4' => -4, |
| 620 | 'UM35' => -3.5, |
| 621 | 'UM3' => -3, |
| 622 | 'UM2' => -2, |
| 623 | 'UM1' => -1, |
| 624 | 'UTC' => 0, |
| 625 | 'UP1' => +1, |
| 626 | 'UP2' => +2, |
| 627 | 'UP3' => +3, |
| 628 | 'UP35' => +3.5, |
| 629 | 'UP4' => +4, |
| 630 | 'UP45' => +4.5, |
| 631 | 'UP5' => +5, |
| 632 | 'UP55' => +5.5, |
| 633 | 'UP575' => +5.75, |
| 634 | 'UP6' => +6, |
| 635 | 'UP65' => +6.5, |
| 636 | 'UP7' => +7, |
| 637 | 'UP8' => +8, |
| 638 | 'UP875' => +8.75, |
| 639 | 'UP9' => +9, |
| 640 | 'UP95' => +9.5, |
| 641 | 'UP10' => +10, |
| 642 | 'UP105' => +10.5, |
| 643 | 'UP11' => +11, |
| 644 | 'UP115' => +11.5, |
| 645 | 'UP12' => +12, |
| 646 | 'UP1275' => +12.75, |
| 647 | 'UP13' => +13, |
| 648 | 'UP14' => +14 |
| 649 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 650 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 651 | if ($tz === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 652 | { |
| 653 | return $zones; |
| 654 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 655 | |
Andrey Andreev | e92df33 | 2012-03-26 22:44:20 +0300 | [diff] [blame] | 656 | return isset($zones[$tz]) ? $zones[$tz] : 0; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 660 | // ------------------------------------------------------------------------ |
| 661 | |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 662 | if ( ! function_exists('date_range')) |
| 663 | { |
Andrey Andreev | 14aa317 | 2012-05-02 13:27:30 +0300 | [diff] [blame] | 664 | /** |
| 665 | * Date range |
| 666 | * |
| 667 | * Returns a list of dates within a specified period. |
| 668 | * |
| 669 | * @param int unix_start UNIX timestamp of period start date |
| 670 | * @param int unix_end|days UNIX timestamp of period end date |
| 671 | * or interval in days. |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame^] | 672 | * @param mixed is_unix Specifies whether the second parameter |
Andrey Andreev | 14aa317 | 2012-05-02 13:27:30 +0300 | [diff] [blame] | 673 | * is a UNIX timestamp or a day interval |
| 674 | * - TRUE or 'unix' for a timestamp |
| 675 | * - FALSE or 'days' for an interval |
| 676 | * @param string date_format Output date format, same as in date() |
| 677 | * @return array |
| 678 | */ |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 679 | function date_range($unix_start = '', $mixed = '', $is_unix = TRUE, $format = 'Y-m-d') |
| 680 | { |
| 681 | if ($unix_start == '' OR $mixed == '' OR $format == '') |
| 682 | { |
| 683 | return FALSE; |
| 684 | } |
| 685 | |
| 686 | $is_unix = ! ( ! $is_unix OR $is_unix === 'days'); |
| 687 | |
| 688 | // Validate input and try strtotime() on invalid timestamps/intervals, just in case |
| 689 | if ( ( ! preg_match('/^[0-9]+$/', $unix_start) && ($unix_start = @strtotime($unix_time)) === FALSE) |
| 690 | OR ( ! preg_match('/^[0-9]+$/', $mixed) && ($is_unix === FALSE OR ($mixed = @strtotime($mixed)) === FALSE)) |
| 691 | OR ($is_unix === TRUE && $mixed < $unix_start)) |
| 692 | { |
| 693 | return FALSE; |
| 694 | } |
| 695 | |
| 696 | if ($is_unix && ($unix_start == $mixed OR date($format, $unix_start) === date($format, $mixed))) |
| 697 | { |
| 698 | return array($start_date); |
| 699 | } |
| 700 | |
| 701 | $range = array(); |
| 702 | |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 703 | /* NOTE: Even though the DateTime object has many useful features, it appears that |
| 704 | * it doesn't always handle properly timezones, when timestamps are passed |
| 705 | * directly to its constructor. Neither of the following gave proper results: |
| 706 | * |
| 707 | * new DateTime('<timestamp>') |
| 708 | * new DateTime('<timestamp>', '<timezone>') |
| 709 | * |
| 710 | * --- available in PHP 5.3: |
| 711 | * |
| 712 | * DateTime::createFromFormat('<format>', '<timestamp>') |
| 713 | * DateTime::createFromFormat('<format>', '<timestamp>', '<timezone') |
| 714 | * |
| 715 | * ... so we'll have to set the timestamp after the object is instantiated. |
| 716 | * Furthermore, in PHP 5.3 we can use DateTime::setTimestamp() to do that and |
| 717 | * given that we have UNIX timestamps - we should use it. |
| 718 | */ |
| 719 | $from = new DateTime(); |
| 720 | |
| 721 | if (is_php('5.3')) |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 722 | { |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 723 | $from->setTimestamp($unix_start); |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 724 | if ($is_unix) |
| 725 | { |
| 726 | $arg = new DateTime(); |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 727 | $arg->setTimestamp($mixed); |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 728 | } |
| 729 | else |
| 730 | { |
| 731 | $arg = (int) $mixed; |
| 732 | } |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 733 | |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 734 | $period = new DatePeriod($from, new DateInterval('P1D'), $arg); |
| 735 | foreach ($period as $date) |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 736 | { |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 737 | $range[] = $date->format($format); |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 738 | } |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 739 | |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 740 | /* If a period end date was passed to the DatePeriod constructor, it might not |
| 741 | * be in our results. Not sure if this is a bug or it's just possible because |
| 742 | * the end date might actually be less than 24 hours away from the previously |
| 743 | * generated DateTime object, but either way - we have to append it manually. |
| 744 | */ |
| 745 | if ( ! is_int($arg) && $range[count($range) - 1] !== $arg->format($format)) |
| 746 | { |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 747 | $range[] = $arg->format($format); |
| 748 | } |
| 749 | |
| 750 | return $range; |
| 751 | } |
| 752 | |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 753 | $from->setDate(date('Y', $unix_start), date('n', $unix_start), date('j', $unix_start)); |
| 754 | $from->setTime(date('G', $unix_start), date('i', $unix_start), date('s', $unix_start)); |
| 755 | if ($is_unix) |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 756 | { |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 757 | $arg = new DateTime(); |
| 758 | $arg->setDate(date('Y', $mixed), date('n', $mixed), date('j', $mixed)); |
| 759 | $arg->setTime(date('G', $mixed), date('i', $mixed), date('s', $mixed)); |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 760 | } |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 761 | else |
| 762 | { |
| 763 | $arg = (int) $mixed; |
| 764 | } |
| 765 | $range[] = $from->format($format); |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 766 | |
Andrey Andreev | 30da39b | 2012-03-10 15:49:17 +0200 | [diff] [blame] | 767 | if (is_int($arg)) // Day intervals |
| 768 | { |
| 769 | do |
| 770 | { |
| 771 | $from->modify('+1 day'); |
| 772 | $range[] = $from->format($format); |
| 773 | } |
| 774 | while (--$arg > 0); |
| 775 | } |
| 776 | else // end date UNIX timestamp |
| 777 | { |
| 778 | for ($from->modify('+1 day'), $end_check = $arg->format('Ymd'); $from->format('Ymd') < $end_check; $from->modify('+1 day')) |
| 779 | { |
| 780 | $range[] = $from->format($format); |
| 781 | } |
| 782 | |
| 783 | // Our loop only appended dates prior to our end date |
| 784 | $range[] = $arg->format($format); |
| 785 | } |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 786 | |
| 787 | return $range; |
| 788 | } |
| 789 | } |
| 790 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 791 | /* End of file date_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 792 | /* Location: ./system/helpers/date_helper.php */ |