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