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 | 895e98c | 2012-06-08 23:01:31 +0200 | [diff] [blame^] | 45 | * Returns time() based on the timezone parameter or on the "timezone" |
| 46 | * 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 | 8310595 | 2012-03-27 18:18:15 +0200 | [diff] [blame] | 53 | $CI =& get_instance(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 54 | |
Iban Eguia | 8310595 | 2012-03-27 18:18:15 +0200 | [diff] [blame] | 55 | if (is_null($timezone)) |
Iban Eguia | 16760bb | 2012-03-27 18:51:52 +0200 | [diff] [blame] | 56 | { |
Iban Eguia | 8310595 | 2012-03-27 18:18:15 +0200 | [diff] [blame] | 57 | $timezone = $CI->config->item('timezone'); |
Iban Eguia | 16760bb | 2012-03-27 18:51:52 +0200 | [diff] [blame] | 58 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 59 | |
Iban Eguia | 8310595 | 2012-03-27 18:18:15 +0200 | [diff] [blame] | 60 | $timezone = new DateTimeZone($timezone); |
| 61 | $now = new DateTime('now', $timezone); |
| 62 | $offset = $timezone->getOffset($now); |
| 63 | $time = time() + $offset; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 64 | |
Iban Eguia | 8310595 | 2012-03-27 18:18:15 +0200 | [diff] [blame] | 65 | return $time; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 68 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | // ------------------------------------------------------------------------ |
| 70 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | if ( ! function_exists('mdate')) |
| 72 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 73 | /** |
| 74 | * Convert MySQL Style Datecodes |
| 75 | * |
| 76 | * This function is identical to PHPs date() function, |
| 77 | * except that it allows date codes to be formatted using |
| 78 | * the MySQL style, where each code letter is preceded |
| 79 | * with a percent sign: %Y %m %d etc... |
| 80 | * |
| 81 | * The benefit of doing dates this way is that you don't |
| 82 | * have to worry about escaping your text letters that |
| 83 | * match the date codes. |
| 84 | * |
| 85 | * @param string |
| 86 | * @param int |
| 87 | * @return int |
| 88 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | function mdate($datestr = '', $time = '') |
| 90 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 91 | if ($datestr === '') |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 92 | { |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 93 | return ''; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 94 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 96 | $time = ($time === '') ? now() : $time; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 97 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 98 | $datestr = str_replace( |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 99 | '%\\', |
| 100 | '', |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 101 | preg_replace('/([a-z]+?){1}/i', '\\\\\\1', $datestr) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 102 | ); |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 103 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | return date($datestr, $time); |
| 105 | } |
| 106 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 107 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | // ------------------------------------------------------------------------ |
| 109 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | if ( ! function_exists('standard_date')) |
| 111 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 112 | /** |
| 113 | * Standard Date |
| 114 | * |
| 115 | * Returns a date formatted according to the submitted standard. |
| 116 | * |
| 117 | * @param string the chosen format |
| 118 | * @param int Unix timestamp |
| 119 | * @return string |
| 120 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | function standard_date($fmt = 'DATE_RFC822', $time = '') |
| 122 | { |
| 123 | $formats = array( |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 124 | 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC', |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 126 | 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O', |
Syahril Zulkefli | 90658ad | 2011-11-13 23:43:37 +0800 | [diff] [blame] | 128 | 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O', |
| 130 | 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O', |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 131 | 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O', |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 133 | 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | ); |
| 135 | |
| 136 | if ( ! isset($formats[$fmt])) |
| 137 | { |
| 138 | return FALSE; |
| 139 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 140 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | return mdate($formats[$fmt], $time); |
| 142 | } |
| 143 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 144 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | // ------------------------------------------------------------------------ |
| 146 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | if ( ! function_exists('timespan')) |
| 148 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 149 | /** |
| 150 | * Timespan |
| 151 | * |
| 152 | * Returns a span of seconds in this format: |
| 153 | * 10 days 14 hours 36 minutes 47 seconds |
| 154 | * |
| 155 | * @param int a number of seconds |
| 156 | * @param int Unix timestamp |
| 157 | * @param int a number of display units |
| 158 | * @return string |
| 159 | */ |
Roger Herbert | 8d69aa1 | 2012-03-14 08:44:55 +0000 | [diff] [blame] | 160 | function timespan($seconds = 1, $time = '', $units = 7) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | { |
| 162 | $CI =& get_instance(); |
| 163 | $CI->lang->load('date'); |
| 164 | |
| 165 | if ( ! is_numeric($seconds)) |
| 166 | { |
| 167 | $seconds = 1; |
| 168 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | if ( ! is_numeric($time)) |
| 171 | { |
| 172 | $time = time(); |
| 173 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 175 | if ( ! is_numeric($units)) |
| 176 | { |
Roger Herbert | 8d69aa1 | 2012-03-14 08:44:55 +0000 | [diff] [blame] | 177 | $units = 7; |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 180 | $seconds = ($time <= $seconds) ? 1 : $time - $seconds; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 182 | $str = array(); |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 183 | $years = floor($seconds / 31557600); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 184 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | if ($years > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 186 | { |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 187 | $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 190 | $seconds -= $years * 31557600; |
| 191 | $months = floor($seconds / 2629743); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 192 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 193 | if (count($str) < $units && ($years > 0 OR $months > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | { |
| 195 | if ($months > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | { |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 197 | $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 200 | $seconds -= $months * 2629743; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | $weeks = floor($seconds / 604800); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 204 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 205 | if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | { |
| 207 | if ($weeks > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 208 | { |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 209 | $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | $seconds -= $weeks * 604800; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 213 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | |
| 215 | $days = floor($seconds / 86400); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 217 | if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | { |
| 219 | if ($days > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | { |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 221 | $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | $seconds -= $days * 86400; |
| 225 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | $hours = floor($seconds / 3600); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 228 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 229 | if (count($str) < $units && ($days > 0 OR $hours > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | { |
| 231 | if ($hours > 0) |
| 232 | { |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 233 | $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 235 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | $seconds -= $hours * 3600; |
| 237 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 238 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | $minutes = floor($seconds / 60); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 240 | |
Roger Herbert | b8fb66b | 2012-03-11 16:15:15 +0000 | [diff] [blame] | 241 | if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | { |
| 243 | if ($minutes > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | { |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 245 | $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | $seconds -= $minutes * 60; |
| 249 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 250 | |
Roger Herbert | 597eb21 | 2012-03-14 09:06:17 +0000 | [diff] [blame] | 251 | if (count($str) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | { |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 253 | $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 255 | |
Roger Herbert | 04c146d | 2012-03-11 15:31:01 +0000 | [diff] [blame] | 256 | return implode(', ', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 259 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | // ------------------------------------------------------------------------ |
| 261 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | if ( ! function_exists('days_in_month')) |
| 263 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 264 | /** |
| 265 | * Number of days in a month |
| 266 | * |
| 267 | * Takes a month/year as input and returns the number of days |
| 268 | * for the given month/year. Takes leap years into consideration. |
| 269 | * |
| 270 | * @param int a numeric month |
| 271 | * @param int a numeric year |
| 272 | * @return int |
| 273 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | function days_in_month($month = 0, $year = '') |
| 275 | { |
| 276 | if ($month < 1 OR $month > 12) |
| 277 | { |
| 278 | return 0; |
| 279 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 280 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 281 | if ( ! is_numeric($year) OR strlen($year) !== 4) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | { |
| 283 | $year = date('Y'); |
| 284 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 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 | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 317 | gmdate('H', $time), |
| 318 | gmdate('i', $time), |
| 319 | gmdate('s', $time), |
| 320 | gmdate('m', $time), |
| 321 | gmdate('d', $time), |
| 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 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 352 | if ($dst === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | { |
| 354 | $time += 3600; |
| 355 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 356 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | return $time; |
| 358 | } |
| 359 | } |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 360 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | // ------------------------------------------------------------------------ |
| 362 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | if ( ! function_exists('mysql_to_unix')) |
| 364 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 365 | /** |
| 366 | * Converts a MySQL Timestamp to Unix |
| 367 | * |
| 368 | * @param int Unix timestamp |
| 369 | * @return int |
| 370 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | function mysql_to_unix($time = '') |
| 372 | { |
| 373 | // We'll remove certain characters for backward compatibility |
| 374 | // since the formatting changed with MySQL 4.1 |
| 375 | // YYYY-MM-DD HH:MM:SS |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 376 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | $time = str_replace('-', '', $time); |
| 378 | $time = str_replace(':', '', $time); |
| 379 | $time = str_replace(' ', '', $time); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 380 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 381 | // YYYYMMDDHHMMSS |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 382 | return mktime( |
| 383 | substr($time, 8, 2), |
| 384 | substr($time, 10, 2), |
| 385 | substr($time, 12, 2), |
| 386 | substr($time, 4, 2), |
| 387 | substr($time, 6, 2), |
| 388 | substr($time, 0, 4) |
| 389 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 392 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | // ------------------------------------------------------------------------ |
| 394 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | if ( ! function_exists('unix_to_human')) |
| 396 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 397 | /** |
| 398 | * Unix to "Human" |
| 399 | * |
| 400 | * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM |
| 401 | * |
| 402 | * @param int Unix timestamp |
| 403 | * @param bool whether to show seconds |
| 404 | * @param string format: us or euro |
| 405 | * @return string |
| 406 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us') |
| 408 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 409 | $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 410 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 411 | if ($fmt === 'us') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | { |
| 413 | $r .= date('h', $time).':'.date('i', $time); |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | $r .= date('H', $time).':'.date('i', $time); |
| 418 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 419 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | if ($seconds) |
| 421 | { |
| 422 | $r .= ':'.date('s', $time); |
| 423 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 425 | if ($fmt === 'us') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | { |
| 427 | $r .= ' '.date('A', $time); |
| 428 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 429 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | return $r; |
| 431 | } |
| 432 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 433 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | // ------------------------------------------------------------------------ |
| 435 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | if ( ! function_exists('human_to_unix')) |
| 437 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 438 | /** |
| 439 | * Convert "human" date to GMT |
| 440 | * |
| 441 | * Reverses the above process |
| 442 | * |
| 443 | * @param string format: us or euro |
| 444 | * @return int |
| 445 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | function human_to_unix($datestr = '') |
| 447 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 448 | if ($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 | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 453 | $datestr = preg_replace('/\040+/', ' ', trim($datestr)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 454 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | if ( ! preg_match('/^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr)) |
| 456 | { |
| 457 | return FALSE; |
| 458 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 459 | |
Derek Jones | 3659109 | 2010-03-05 10:05:51 -0600 | [diff] [blame] | 460 | $split = explode(' ', $datestr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 462 | $ex = explode('-', $split['0']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 463 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 464 | $year = (strlen($ex[0]) === 2) ? '20'.$ex[0] : $ex[0]; |
| 465 | $month = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1]; |
| 466 | $day = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[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 | $ex = explode(':', $split['1']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 469 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 470 | $hour = (strlen($ex[0]) === 1) ? '0'.$ex[0] : $ex[0]; |
| 471 | $min = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | |
Andrey Andreev | 5036c9c | 2012-06-04 15:34:56 +0300 | [diff] [blame] | 473 | if (isset($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | { |
Andrey Andreev | 5036c9c | 2012-06-04 15:34:56 +0300 | [diff] [blame] | 475 | $sec = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | } |
| 477 | else |
| 478 | { |
| 479 | // Unless specified, seconds get set to zero. |
| 480 | $sec = '00'; |
| 481 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | |
Andrey Andreev | 5036c9c | 2012-06-04 15:34:56 +0300 | [diff] [blame] | 483 | if (isset($split[2])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 485 | $ampm = strtolower($split[2]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 486 | |
Andrey Andreev | e92df33 | 2012-03-26 22:44:20 +0300 | [diff] [blame] | 487 | if (substr($ampm, 0, 1) === 'p' && $hour < 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 488 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 489 | $hour += 12; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 490 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 491 | |
Andrey Andreev | e92df33 | 2012-03-26 22:44:20 +0300 | [diff] [blame] | 492 | if (substr($ampm, 0, 1) === 'a' && $hour == 12) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 493 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 494 | $hour = '00'; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 495 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 496 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 497 | if (strlen($hour) === 1) |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 498 | { |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 499 | $hour = '0'.$hour; |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 500 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 502 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | return mktime($hour, $min, $sec, $month, $day, $year); |
| 504 | } |
| 505 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 506 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 507 | // ------------------------------------------------------------------------ |
| 508 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 509 | if ( ! function_exists('nice_date')) |
| 510 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 511 | /** |
| 512 | * Turns many "reasonably-date-like" strings into something |
| 513 | * that is actually useful. This only works for dates after unix epoch. |
| 514 | * |
| 515 | * @param string The terribly formatted date-like string |
| 516 | * @param string Date format to return (same as php date function) |
| 517 | * @return string |
| 518 | */ |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 519 | function nice_date($bad_date = '', $format = FALSE) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 520 | { |
| 521 | if (empty($bad_date)) |
| 522 | { |
| 523 | return 'Unknown'; |
| 524 | } |
Greg Aker | f916839 | 2011-08-29 19:29:05 -0500 | [diff] [blame] | 525 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 526 | // Date like: YYYYMM |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 527 | if (preg_match('/^\d{6}$/', $bad_date)) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 528 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 529 | if (in_array(substr($bad_date, 0, 2), array('19', '20'))) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 530 | { |
| 531 | $year = substr($bad_date, 0, 4); |
| 532 | $month = substr($bad_date, 4, 2); |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 533 | } |
| 534 | else |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 535 | { |
| 536 | $month = substr($bad_date, 0, 2); |
| 537 | $year = substr($bad_date, 2, 4); |
| 538 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 539 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 540 | return date($format, strtotime($year.'-'.$month.'-01')); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 541 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 542 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 543 | // Date Like: YYYYMMDD |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 544 | if (preg_match('/^\d{8}$/', $bad_date)) |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 545 | { |
| 546 | $month = substr($bad_date, 0, 2); |
| 547 | $day = substr($bad_date, 2, 2); |
| 548 | $year = substr($bad_date, 4, 4); |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 549 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 550 | return date($format, strtotime($month.'/01/'.$year)); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 551 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 552 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 553 | // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between) |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 554 | if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/', $bad_date)) |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 555 | { |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 556 | list($m, $d, $y) = explode('-', $bad_date); |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 557 | return date($format, strtotime($y.'-'.$m.'-'.$d)); |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 558 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 559 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 560 | // Any other kind of string, when converted into UNIX time, |
| 561 | // produces "0 seconds after epoc..." is probably bad... |
| 562 | // return "Invalid Date". |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 563 | if (date('U', strtotime($bad_date)) === '0') |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 564 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 565 | return 'Invalid Date'; |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 566 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 567 | |
Kyle Farris | 896d95a | 2011-08-21 23:03:54 -0300 | [diff] [blame] | 568 | // It's probably a valid-ish date format already |
| 569 | return date($format, strtotime($bad_date)); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | // ------------------------------------------------------------------------ |
| 574 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 575 | if ( ! function_exists('timezone_menu')) |
| 576 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 577 | /** |
| 578 | * Timezone Menu |
| 579 | * |
| 580 | * Generates a drop-down menu of timezones. |
| 581 | * |
| 582 | * @param string timezone |
| 583 | * @param string classname |
| 584 | * @param string menu name |
| 585 | * @return string |
| 586 | */ |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 587 | function timezone_menu($default = 'UTC', $class = '', $name = 'timezones') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | { |
| 589 | $CI =& get_instance(); |
| 590 | $CI->lang->load('date'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 591 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 592 | $default = ($default === 'GMT') ? 'UTC' : $default; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 593 | |
| 594 | $menu = '<select name="'.$name.'"'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 595 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 596 | if ($class !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 597 | { |
| 598 | $menu .= ' class="'.$class.'"'; |
| 599 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 600 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 601 | $menu .= ">\n"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 602 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | foreach (timezones() as $key => $val) |
| 604 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 605 | $selected = ($default === $key) ? ' selected="selected"' : ''; |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 606 | $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 609 | return $menu.'</select>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 612 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 613 | // ------------------------------------------------------------------------ |
| 614 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 615 | if ( ! function_exists('timezones')) |
| 616 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 617 | /** |
| 618 | * Timezones |
| 619 | * |
| 620 | * Returns an array of timezones. This is a helper function |
| 621 | * for various other ones in this library |
| 622 | * |
| 623 | * @param string timezone |
| 624 | * @return string |
| 625 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 626 | function timezones($tz = '') |
| 627 | { |
| 628 | // Note: Don't change the order of these even though |
| 629 | // some items appear to be in the wrong order |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 630 | |
| 631 | $zones = array( |
Greg Aker | c964e72 | 2011-08-29 19:31:29 -0500 | [diff] [blame] | 632 | 'UM12' => -12, |
| 633 | 'UM11' => -11, |
| 634 | 'UM10' => -10, |
| 635 | 'UM95' => -9.5, |
| 636 | 'UM9' => -9, |
| 637 | 'UM8' => -8, |
| 638 | 'UM7' => -7, |
| 639 | 'UM6' => -6, |
| 640 | 'UM5' => -5, |
| 641 | 'UM45' => -4.5, |
| 642 | 'UM4' => -4, |
| 643 | 'UM35' => -3.5, |
| 644 | 'UM3' => -3, |
| 645 | 'UM2' => -2, |
| 646 | 'UM1' => -1, |
| 647 | 'UTC' => 0, |
| 648 | 'UP1' => +1, |
| 649 | 'UP2' => +2, |
| 650 | 'UP3' => +3, |
| 651 | 'UP35' => +3.5, |
| 652 | 'UP4' => +4, |
| 653 | 'UP45' => +4.5, |
| 654 | 'UP5' => +5, |
| 655 | 'UP55' => +5.5, |
| 656 | 'UP575' => +5.75, |
| 657 | 'UP6' => +6, |
| 658 | 'UP65' => +6.5, |
| 659 | 'UP7' => +7, |
| 660 | 'UP8' => +8, |
| 661 | 'UP875' => +8.75, |
| 662 | 'UP9' => +9, |
| 663 | 'UP95' => +9.5, |
| 664 | 'UP10' => +10, |
| 665 | 'UP105' => +10.5, |
| 666 | 'UP11' => +11, |
| 667 | 'UP115' => +11.5, |
| 668 | 'UP12' => +12, |
| 669 | 'UP1275' => +12.75, |
| 670 | 'UP13' => +13, |
| 671 | 'UP14' => +14 |
| 672 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 673 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 674 | if ($tz === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 675 | { |
| 676 | return $zones; |
| 677 | } |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 678 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 679 | $tz = ($tz === 'GMT') ? 'UTC' : $tz; |
Eric Barnes | dc3e4be | 2011-12-19 13:48:29 -0500 | [diff] [blame] | 680 | |
Andrey Andreev | e92df33 | 2012-03-26 22:44:20 +0300 | [diff] [blame] | 681 | return isset($zones[$tz]) ? $zones[$tz] : 0; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 685 | /* End of file date_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 686 | /* Location: ./system/helpers/date_helper.php */ |