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