Skip to content
This repository

Improve the date helper #870

Closed
wants to merge 16 commits into from

2 participants

Andrey Andreev Phil Sturgeon
Andrey Andreev
Collaborator

Just some minor speed optimizations and removed/fixed spaces.

system/helpers/date_helper.php
((6 lines not shown))
598 562
 	{
599 563
 		$CI =& get_instance();
600 564
 		$CI->lang->load('date');
601 565
 
602  
-		$default = ($default == 'GMT') ? 'UTC' : $default;
5

For this I normally go with:

$default === 'GMT' OR $default = 'UTC';

Ternary is for one condition and two possible answers (three parts), this is essentially the same but for one condition and a default answer (two parts).

What do people think?

Andrey Andreev Collaborator
narfbg added a note

Um, that's why I've replaced the ternary with an if() - it's the same thing. I would indeed like to write 1 line of code instead of 4, but didn't want to start the readability discussion again.

Andrey Andreev Collaborator
narfbg added a note

Well, $var === 'value' OR $var = 'othervalue'; is basically an if(), only without the 'if' word itself and all the brackets.

I vote for that to be a standard in such cases - yes. :)

Andrey Andreev Collaborator
narfbg added a note

Used the proposed <comparison> OR <assignment> format in the last commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
system/helpers/date_helper.php
@@ -688,12 +653,14 @@ function timezones($tz = '')
688 653
 		{
689 654
 			return $zones;
690 655
 		}
691  
-
692  
-		$tz = ($tz == 'GMT') ? 'UTC' : $tz;
  656
+		elseif ($tz === 'GMT')
2

See above.

Andrey Andreev Collaborator
narfbg added a note

This actually turned out to be unnecessary, as if $zones[$tz] is not set (and there's no element with the 'GMT' key in it) - 0 will be returned anyway, so deleted that line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Andrey Andreev narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 65 additions and 119 deletions. Show diff stats Hide diff stats

  1. 184  system/helpers/date_helper.php
184  system/helpers/date_helper.php
@@ -49,22 +49,9 @@
49 49
 	function now()
50 50
 	{
51 51
 		$CI =& get_instance();
52  
-
53  
-		if (strtolower($CI->config->item('time_reference')) == 'gmt')
54  
-		{
55  
-			$now = time();
56  
-			$system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
57  
-
58  
-			if (strlen($system_time) < 10)
59  
-			{
60  
-				$system_time = time();
61  
-				log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
62  
-			}
63  
-
64  
-			return $system_time;
65  
-		}
66  
-
67  
-		return time();
  52
+		return (strtolower($CI->config->item('time_reference')) === 'gmt')
  53
+			? mktime(gmdate('G'), gmdate('i'), gmdate('s'), gmdate('n'), gmdate('j'), gmdate('Y'))
  54
+			: time();
68 55
 	}
69 56
 }
70 57
 
@@ -94,8 +81,10 @@ function mdate($datestr = '', $time = '')
94 81
 		{
95 82
 			return '';
96 83
 		}
97  
-
98  
-		$time = ($time == '') ? now() : $time;
  84
+		elseif ($time == '')
  85
+		{
  86
+			$time = now();
  87
+		}
99 88
 
100 89
 		$datestr = str_replace(
101 90
 			'%\\',
@@ -123,24 +112,18 @@ function mdate($datestr = '', $time = '')
123 112
 	function standard_date($fmt = 'DATE_RFC822', $time = '')
124 113
 	{
125 114
 		$formats = array(
126  
-						'DATE_ATOM'		=>	'%Y-%m-%dT%H:%i:%s%O',
127  
-						'DATE_COOKIE'	=>	'%l, %d-%M-%y %H:%i:%s UTC',
128  
-						'DATE_ISO8601'	=>	'%Y-%m-%dT%H:%i:%s%O',
129  
-						'DATE_RFC822'	=>	'%D, %d %M %y %H:%i:%s %O',
130  
-						'DATE_RFC850'	=>	'%l, %d-%M-%y %H:%i:%s UTC',
131  
-						'DATE_RFC1036'	=>	'%D, %d %M %y %H:%i:%s %O',
132  
-						'DATE_RFC1123'	=>	'%D, %d %M %Y %H:%i:%s %O',
133  
-						'DATE_RFC2822'	=>	'%D, %d %M %Y %H:%i:%s %O',
134  
-						'DATE_RSS'		=>	'%D, %d %M %Y %H:%i:%s %O',
135  
-						'DATE_W3C'		=>	'%Y-%m-%dT%H:%i:%s%O'
136  
-						);
137  
-
138  
-		if ( ! isset($formats[$fmt]))
139  
-		{
140  
-			return FALSE;
141  
-		}
142  
-
143  
-		return mdate($formats[$fmt], $time);
  115
+					'DATE_ATOM'	=>	'%Y-%m-%dT%H:%i:%s%Q',
  116
+					'DATE_COOKIE'	=>	'%l, %d-%M-%y %H:%i:%s UTC',
  117
+					'DATE_ISO8601'	=>	'%Y-%m-%dT%H:%i:%s%Q',
  118
+					'DATE_RFC822'	=>	'%D, %d %M %y %H:%i:%s %O',
  119
+					'DATE_RFC850'	=>	'%l, %d-%M-%y %H:%i:%s UTC',
  120
+					'DATE_RFC1036'	=>	'%D, %d %M %y %H:%i:%s %O',
  121
+					'DATE_RFC1123'	=>	'%D, %d %M %Y %H:%i:%s %O',
  122
+					'DATE_RSS'	=>	'%D, %d %M %Y %H:%i:%s %O',
  123
+					'DATE_W3C'	=>	'%Y-%m-%dT%H:%i:%s%Q'
  124
+				);
  125
+
  126
+		return isset($formats[$fmt]) ? mdate($formats[$fmt], $time) : FALSE;
144 127
 	}
145 128
 }
146 129
 
@@ -164,12 +147,12 @@ function timespan($seconds = 1, $time = '', $units = 7)
164 147
 		$CI =& get_instance();
165 148
 		$CI->lang->load('date');
166 149
 
167  
-		if ( ! is_numeric($seconds))
  150
+		if ( ! $seconds OR ! preg_match('/^[0-9]+$/', $seconds))
168 151
 		{
169 152
 			$seconds = 1;
170 153
 		}
171 154
 
172  
-		if ( ! is_numeric($time))
  155
+		if ( ! preg_match('/^[0-9]+$/', $time))
173 156
 		{
174 157
 			$time = time();
175 158
 		}
@@ -179,14 +162,13 @@ function timespan($seconds = 1, $time = '', $units = 7)
179 162
 			$units = 7;
180 163
 		}
181 164
 
182  
-		$seconds = ($time <= $seconds) ? 1 : $time - $seconds;
183  
-
184 165
 		$str = array();
  166
+		$seconds = ($time <= $seconds) ? 1 : $time - $seconds;
185 167
 		$years = floor($seconds / 31557600);
186 168
 
187 169
 		if ($years > 0)
188 170
 		{
189  
-			$str[] = $years.' '.$CI->lang->line((($years	> 1) ? 'date_years' : 'date_year'));
  171
+			$str[] = $years.' '.$CI->lang->line(($years > 1 ? 'date_years' : 'date_year'));
190 172
 		}
191 173
 
192 174
 		$seconds -= $years * 31557600;
@@ -196,7 +178,7 @@ function timespan($seconds = 1, $time = '', $units = 7)
196 178
 		{
197 179
 			if ($months > 0)
198 180
 			{
199  
-				$str[] = $months.' '.$CI->lang->line((($months	> 1) ? 'date_months' : 'date_month'));
  181
+				$str[] = $months.' '.$CI->lang->line(($months > 1 ? 'date_months' : 'date_month'));
200 182
 			}
201 183
 
202 184
 			$seconds -= $months * 2629743;
@@ -208,7 +190,7 @@ function timespan($seconds = 1, $time = '', $units = 7)
208 190
 		{
209 191
 			if ($weeks > 0)
210 192
 			{
211  
-				$str[] = $weeks.' '.$CI->lang->line((($weeks	> 1) ? 'date_weeks' : 'date_week'));
  193
+				$str[] = $weeks.' '.$CI->lang->line(($weeks > 1 ? 'date_weeks' : 'date_week'));
212 194
 			}
213 195
 
214 196
 			$seconds -= $weeks * 604800;
@@ -220,7 +202,7 @@ function timespan($seconds = 1, $time = '', $units = 7)
220 202
 		{
221 203
 			if ($days > 0)
222 204
 			{
223  
-				$str[] = $days.' '.$CI->lang->line((($days	> 1) ? 'date_days' : 'date_day'));
  205
+				$str[] = $days.' '.$CI->lang->line(($days > 1 ? 'date_days' : 'date_day'));
224 206
 			}
225 207
 
226 208
 			$seconds -= $days * 86400;
@@ -232,7 +214,7 @@ function timespan($seconds = 1, $time = '', $units = 7)
232 214
 		{
233 215
 			if ($hours > 0)
234 216
 			{
235  
-				$str[] = $hours.' '.$CI->lang->line((($hours	> 1) ? 'date_hours' : 'date_hour'));
  217
+				$str[] = $hours.' '.$CI->lang->line(($hours > 1 ? 'date_hours' : 'date_hour'));
236 218
 			}
237 219
 
238 220
 			$seconds -= $hours * 3600;
@@ -244,7 +226,7 @@ function timespan($seconds = 1, $time = '', $units = 7)
244 226
 		{
245 227
 			if ($minutes > 0)
246 228
 			{
247  
-				$str[] = $minutes.' '.$CI->lang->line((($minutes	> 1) ? 'date_minutes' : 'date_minute'));
  229
+				$str[] = $minutes.' '.$CI->lang->line(($minutes > 1 ? 'date_minutes' : 'date_minute'));
248 230
 			}
249 231
 
250 232
 			$seconds -= $minutes * 60;
@@ -252,7 +234,7 @@ function timespan($seconds = 1, $time = '', $units = 7)
252 234
 
253 235
 		if (count($str) === 0)
254 236
 		{
255  
-			$str[] = $seconds.' '.$CI->lang->line((($seconds	> 1) ? 'date_seconds' : 'date_second'));
  237
+			$str[] = $seconds.' '.$CI->lang->line(($seconds > 1 ? 'date_seconds' : 'date_second'));
256 238
 		}
257 239
 
258 240
 		return implode(', ', $str);
@@ -280,20 +262,21 @@ function days_in_month($month = 0, $year = '')
280 262
 			return 0;
281 263
 		}
282 264
 
283  
-		if ( ! is_numeric($year) OR strlen($year) != 4)
  265
+		if ( ! $year OR ! preg_match('/^[1-9]{1}[0-9]{3}$/', $year))
  266
+		{
  267
+			return (int) date('t', mktime(12, 0, 0, $month, 1, date('Y')));
  268
+		}
  269
+		elseif ($year >= 1970)
284 270
 		{
285  
-			$year = date('Y');
  271
+			return (int) date('t', mktime(12, 0, 0, $month, 1, $year));
286 272
 		}
287 273
 
288  
-		if ($month == 2)
  274
+		if ($month == 2 && ($year % 400 == 0 OR ($year % 4 == 0 && $year % 100 != 0)))
289 275
 		{
290  
-			if ($year % 400 == 0 OR ($year % 4 == 0 && $year % 100 != 0))
291  
-			{
292  
-				return 29;
293  
-			}
  276
+			return 29;
294 277
 		}
295 278
 
296  
-		$days_in_month	= array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  279
+		$days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
297 280
 		return $days_in_month[$month - 1];
298 281
 	}
299 282
 }
@@ -316,11 +299,11 @@ function local_to_gmt($time = '')
316 299
 		}
317 300
 
318 301
 		return mktime(
319  
-			gmdate('H', $time),
  302
+			gmdate('G', $time),
320 303
 			gmdate('i', $time),
321 304
 			gmdate('s', $time),
322  
-			gmdate('m', $time),
323  
-			gmdate('d', $time),
  305
+			gmdate('n', $time),
  306
+			gmdate('j', $time),
324 307
 			gmdate('Y', $time)
325 308
 		);
326 309
 	}
@@ -350,10 +333,9 @@ function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
350 333
 		}
351 334
 
352 335
 		$time += timezones($timezone) * 3600;
353  
-
354 336
 		if ($dst == TRUE)
355 337
 		{
356  
-			$time += 3600;
  338
+			return $time + 3600;
357 339
 		}
358 340
 
359 341
 		return $time;
@@ -375,10 +357,7 @@ function mysql_to_unix($time = '')
375 357
 		// We'll remove certain characters for backward compatibility
376 358
 		// since the formatting changed with MySQL 4.1
377 359
 		// YYYY-MM-DD HH:MM:SS
378  
-
379  
-		$time = str_replace('-', '', $time);
380  
-		$time = str_replace(':', '', $time);
381  
-		$time = str_replace(' ', '', $time);
  360
+		$time = str_replace(array('-', ':', ' '), '', $time);
382 361
 
383 362
 		// YYYYMMDDHHMMSS
384 363
 		return mktime(
@@ -408,25 +387,17 @@ function mysql_to_unix($time = '')
408 387
 	 */
409 388
 	function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
410 389
 	{
411  
-		$r  = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
412  
-
413  
-		if ($fmt == 'us')
414  
-		{
415  
-			$r .= date('h', $time).':'.date('i', $time);
416  
-		}
417  
-		else
418  
-		{
419  
-			$r .= date('H', $time).':'.date('i', $time);
420  
-		}
  390
+		$r  = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' '
  391
+			. date(($fmt === 'us' ? 'h' : 'H'), $time).':'.date('i', $time);
421 392
 
422 393
 		if ($seconds)
423 394
 		{
424 395
 			$r .= ':'.date('s', $time);
425 396
 		}
426 397
 
427  
-		if ($fmt == 'us')
  398
+		if ($fmt === 'us')
428 399
 		{
429  
-			$r .= ' '.date('A', $time);
  400
+			return $r.' '.date('A', $time);
430 401
 		}
431 402
 
432 403
 		return $r;
@@ -454,51 +425,29 @@ function human_to_unix($datestr = '')
454 425
 
455 426
 		$datestr = preg_replace('/\040+/', ' ', trim($datestr));
456 427
 
457  
-		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))
  428
+		if ( ! preg_match('/^(\d{2}|\d{4})\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr))
458 429
 		{
459 430
 			return FALSE;
460 431
 		}
461 432
 
462 433
 		$split = explode(' ', $datestr);
  434
+		list($year, $month, $day) = explode('-', $split[0]);
463 435
 
464  
-		$ex = explode('-', $split['0']);
465  
-
466  
-		$year  = (strlen($ex[0]) === 2) ? '20'.$ex[0] : $ex[0];
467  
-		$month = (strlen($ex[1]) === 1) ? '0'.$ex[1]  : $ex[1];
468  
-		$day   = (strlen($ex[2]) === 1) ? '0'.$ex[2]  : $ex[2];
469  
-
470  
-		$ex = explode(':', $split['1']);
  436
+		$ex = explode(':', $split[1]);
  437
+		$hour = (int) $ex[0];
  438
+		$min = (int) $ex[1];
  439
+		$sec = (isset($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2])) ? (int) $ex[2] : 0;
471 440
 
472  
-		$hour = (strlen($ex[0]) === 1) ? '0'.$ex[0] : $ex[0];
473  
-		$min  = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
474  
-
475  
-		if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex[2]))
476  
-		{
477  
-			$sec  = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
478  
-		}
479  
-		else
480  
-		{
481  
-			// Unless specified, seconds get set to zero.
482  
-			$sec = '00';
483  
-		}
484  
-
485  
-		if (isset($split['2']))
  441
+		if (isset($split[2]))
486 442
 		{
487 443
 			$ampm = strtolower($split[2]);
488  
-
489  
-			if (substr($ampm, 0, 1) === 'p' && $hour < 12)
  444
+			if ($ampm[0] === 'p' && $hour < 12)
490 445
 			{
491 446
 				$hour += 12;
492 447
 			}
493  
-
494  
-			if (substr($ampm, 0, 1) === 'a' && $hour == 12)
  448
+			elseif ($ampm[0] === 'a' && $hour == 12)
495 449
 			{
496  
-				$hour =  '00';
497  
-			}
498  
-
499  
-			if (strlen($hour) === 1)
500  
-			{
501  
-				$hour = '0'.$hour;
  450
+				$hour = 0;
502 451
 			}
503 452
 		}
504 453
 
@@ -545,18 +494,17 @@ function nice_date($bad_date = '', $format = FALSE)
545 494
 		// Date Like: YYYYMMDD
546 495
 		if (preg_match('/^\d{8}$/', $bad_date))
547 496
 		{
548  
-			$month = substr($bad_date, 0, 2);
549  
-			$day   = substr($bad_date, 2, 2);
550  
-			$year  = substr($bad_date, 4, 4);
  497
+			$month	= substr($bad_date, 0, 2);
  498
+			$day	= substr($bad_date, 2, 2);
  499
+			$year	= substr($bad_date, 4, 4);
551 500
 
552 501
 			return date($format, strtotime($month.'/01/'.$year));
553 502
 		}
554 503
 
555 504
 		// Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
556  
-		if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/', $bad_date))
  505
+		if (preg_match('/^(\d{1,2})\-|\.(\d{1,2})\-|\.(\d{4})$/', $bad_date, $matches))
557 506
 		{
558  
-			list($m, $d, $y) = explode('-', $bad_date);
559  
-			return date($format, strtotime($y.'-'.$m.'-'.$d));
  507
+			return date($format, strtotime($matches[3].'-'.$matches[2].'-'.$matches[1]));
560 508
 		}
561 509
 
562 510
 		// Any other kind of string, when converted into UNIX time,
@@ -591,7 +539,7 @@ function timezone_menu($default = 'UTC', $class = '', $name = 'timezones')
591 539
 		$CI =& get_instance();
592 540
 		$CI->lang->load('date');
593 541
 
594  
-		$default = ($default == 'GMT') ? 'UTC' : $default;
  542
+		$default !== 'GMT' OR $default = 'UTC';
595 543
 
596 544
 		$menu = '<select name="'.$name.'"';
597 545
 
@@ -605,10 +553,10 @@ function timezone_menu($default = 'UTC', $class = '', $name = 'timezones')
605 553
 		foreach (timezones() as $key => $val)
606 554
 		{
607 555
 			$selected = ($default == $key) ? ' selected="selected"' : '';
608  
-			$menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
  556
+			$menu .= '<option value="'.$key.'"'.($default == $key ? ' selected="selected"' : '').'>'.$CI->lang->line($key)."</option>\n";
609 557
 		}
610 558
 
611  
-		return $menu.'</select>';
  559
+		return $menu."</select>\n";
612 560
 	}
613 561
 }
614 562
 
@@ -678,8 +626,6 @@ function timezones($tz = '')
678 626
 			return $zones;
679 627
 		}
680 628
 
681  
-		$tz = ($tz == 'GMT') ? 'UTC' : $tz;
682  
-
683 629
 		return isset($zones[$tz]) ? $zones[$tz] : 0;
684 630
 	}
685 631
 }
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.