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