Completed
Push — v3.0 ( e28df5...5be1a9 )
by Samir
13:00
created

date_helper.php ➔ timezones()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 59
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 47
nc 5
nop 1
dl 0
loc 59
rs 8.9846
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php  if ( ! defined('BASEPATH')) {
2
	exit('No direct script access allowed');
3
}
4
/**
5
 * CodeIgniter
6
 *
7
 * An open source application development framework for PHP 5.1.6 or newer
8
 *
9
 * @package		CodeIgniter
10
 * @author		ExpressionEngine Dev Team
11
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
12
 * @license		http://codeigniter.com/user_guide/license.html
13
 * @link		http://codeigniter.com
14
 * @since		Version 1.0
15
 * @filesource
16
 */
17
18
// ------------------------------------------------------------------------
19
20
/**
21
 * CodeIgniter Date Helpers
22
 *
23
 * @package		CodeIgniter
24
 * @subpackage	Helpers
25
 * @category	Helpers
26
 * @author		ExpressionEngine Dev Team
27
 * @link		http://codeigniter.com/user_guide/helpers/date_helper.html
28
 */
29
30
// ------------------------------------------------------------------------
31
32
/**
33
 * Get "now" time
34
 *
35
 * Returns time() or its GMT equivalent based on the config file preference
36
 *
37
 * @access	public
38
 * @return	integer
39
 */
40
if ( ! function_exists('now'))
41
{
42
	function now()
43
	{
44
		$CI = & get_instance();
45
46
		if (strtolower($CI->config->item('time_reference')) == 'gmt')
0 ignored issues
show
Bug introduced by
The property config does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47
		{
48
			$now = time();
49
			$system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
50
51
			if (strlen($system_time) < 10)
52
			{
53
				$system_time = time();
54
				log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
55
			}
56
57
			return $system_time;
58
		} else
59
		{
60
			return time();
61
		}
62
	}
63
}
64
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
73
 * with a percent sign:  %Y %m %d etc...
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
 *
79
 * @access	public
80
 * @param	string
81
 * @param	integer
82
 * @return	integer
83
 */
84
if ( ! function_exists('mdate'))
85
{
86
	function mdate($datestr = '', $time = '')
87
	{
88
		if ($datestr == '') {
89
					return '';
90
		}
91
92
		if ($time == '') {
93
					$time = now();
94
		}
95
96
		$datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));
97
		return date($datestr, $time);
98
	}
99
}
100
101
// ------------------------------------------------------------------------
102
103
/**
104
 * Standard Date
105
 *
106
 * Returns a date formatted according to the submitted standard.
107
 *
108
 * @access	public
109
 * @param	string	the chosen format
110
 * @param	integer	Unix timestamp
111
 * @return	string
112
 */
113
if ( ! function_exists('standard_date'))
114
{
115
	function standard_date($fmt = 'DATE_RFC822', $time = '')
116
	{
117
		$formats = array(
118
						'DATE_ATOM'		=>	'%Y-%m-%dT%H:%i:%s%Q',
119
						'DATE_COOKIE'	=>	'%l, %d-%M-%y %H:%i:%s UTC',
120
						'DATE_ISO8601'	=>	'%Y-%m-%dT%H:%i:%s%Q',
121
						'DATE_RFC822'	=>	'%D, %d %M %y %H:%i:%s %O',
122
						'DATE_RFC850'	=>	'%l, %d-%M-%y %H:%i:%s UTC',
123
						'DATE_RFC1036'	=>	'%D, %d %M %y %H:%i:%s %O',
124
						'DATE_RFC1123'	=>	'%D, %d %M %Y %H:%i:%s %O',
125
						'DATE_RSS'		=>	'%D, %d %M %Y %H:%i:%s %O',
126
						'DATE_W3C'		=>	'%Y-%m-%dT%H:%i:%s%Q'
127
						);
128
129
		if ( ! isset($formats[$fmt]))
130
		{
131
			return FALSE;
132
		}
133
134
		return mdate($formats[$fmt], $time);
135
	}
136
}
137
138
// ------------------------------------------------------------------------
139
140
/**
141
 * Timespan
142
 *
143
 * Returns a span of seconds in this format:
144
 *	10 days 14 hours 36 minutes 47 seconds
145
 *
146
 * @access	public
147
 * @param	integer	a number of seconds
148
 * @param	integer	Unix timestamp
149
 * @return	integer
150
 */
151
if ( ! function_exists('timespan'))
152
{
153
	function timespan($seconds = 1, $time = '')
154
	{
155
		$CI = & get_instance();
156
		$CI->lang->load('date');
0 ignored issues
show
Bug introduced by
The property lang does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
157
158
		if ( ! is_numeric($seconds))
159
		{
160
			$seconds = 1;
161
		}
162
163
		if ( ! is_numeric($time))
164
		{
165
			$time = time();
166
		}
167
168
		if ($time <= $seconds)
169
		{
170
			$seconds = 1;
171
		} else
172
		{
173
			$seconds = $time - $seconds;
174
		}
175
176
		$str = '';
177
		$years = floor($seconds / 31536000);
178
179
		if ($years > 0)
180
		{
181
			$str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
182
		}
183
184
		$seconds -= $years * 31536000;
185
		$months = floor($seconds / 2628000);
186
187 View Code Duplication
		if ($years > 0 OR $months > 0)
188
		{
189
			if ($months > 0)
190
			{
191
				$str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
192
			}
193
194
			$seconds -= $months * 2628000;
195
		}
196
197
		$weeks = floor($seconds / 604800);
198
199 View Code Duplication
		if ($years > 0 OR $months > 0 OR $weeks > 0)
200
		{
201
			if ($weeks > 0)
202
			{
203
				$str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
204
			}
205
206
			$seconds -= $weeks * 604800;
207
		}
208
209
		$days = floor($seconds / 86400);
210
211 View Code Duplication
		if ($months > 0 OR $weeks > 0 OR $days > 0)
212
		{
213
			if ($days > 0)
214
			{
215
				$str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
216
			}
217
218
			$seconds -= $days * 86400;
219
		}
220
221
		$hours = floor($seconds / 3600);
222
223 View Code Duplication
		if ($days > 0 OR $hours > 0)
224
		{
225
			if ($hours > 0)
226
			{
227
				$str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
228
			}
229
230
			$seconds -= $hours * 3600;
231
		}
232
233
		$minutes = floor($seconds / 60);
234
235 View Code Duplication
		if ($days > 0 OR $hours > 0 OR $minutes > 0)
236
		{
237
			if ($minutes > 0)
238
			{
239
				$str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
240
			}
241
242
			$seconds -= $minutes * 60;
243
		}
244
245
		if ($str == '')
246
		{
247
			$str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
248
		}
249
250
		return substr(trim($str), 0, -1);
251
	}
252
}
253
254
// ------------------------------------------------------------------------
255
256
/**
257
 * Number of days in a month
258
 *
259
 * Takes a month/year as input and returns the number of days
260
 * for the given month/year. Takes leap years into consideration.
261
 *
262
 * @access	public
263
 * @param	integer a numeric month
264
 * @param	integer	a numeric year
265
 * @return	integer
266
 */
267
if ( ! function_exists('days_in_month'))
268
{
269
	function days_in_month($month = 0, $year = '')
270
	{
271
		if ($month < 1 OR $month > 12)
272
		{
273
			return 0;
274
		}
275
276
		if ( ! is_numeric($year) OR strlen($year) != 4)
277
		{
278
			$year = date('Y');
279
		}
280
281 View Code Duplication
		if ($month == 2)
282
		{
283
			if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
284
			{
285
				return 29;
286
			}
287
		}
288
289
		$days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
290
		return $days_in_month[$month - 1];
291
	}
292
}
293
294
// ------------------------------------------------------------------------
295
296
/**
297
 * Converts a local Unix timestamp to GMT
298
 *
299
 * @access	public
300
 * @param	integer Unix timestamp
301
 * @return	integer
302
 */
303
if ( ! function_exists('local_to_gmt'))
304
{
305
	function local_to_gmt($time = '')
306
	{
307
		if ($time == '') {
308
					$time = time();
309
		}
310
311
		return mktime(gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));
312
	}
313
}
314
315
// ------------------------------------------------------------------------
316
317
/**
318
 * Converts GMT time to a localized value
319
 *
320
 * Takes a Unix timestamp (in GMT) as input, and returns
321
 * at the local value based on the timezone and DST setting
322
 * submitted
323
 *
324
 * @access	public
325
 * @param	integer Unix timestamp
326
 * @param	string	timezone
327
 * @param	bool	whether DST is active
328
 * @return	integer
329
 */
330
if ( ! function_exists('gmt_to_local'))
331
{
332
	function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
333
	{
334
		if ($time == '')
335
		{
336
			return now();
337
		}
338
339
		$time += timezones($timezone) * 3600;
340
341
		if ($dst == TRUE)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
342
		{
343
			$time += 3600;
344
		}
345
346
		return $time;
347
	}
348
}
349
350
// ------------------------------------------------------------------------
351
352
/**
353
 * Converts a MySQL Timestamp to Unix
354
 *
355
 * @access	public
356
 * @param	integer Unix timestamp
357
 * @return	integer
358
 */
359
if ( ! function_exists('mysql_to_unix'))
360
{
361
	function mysql_to_unix($time = '')
362
	{
363
		// We'll remove certain characters for backward compatibility
364
		// since the formatting changed with MySQL 4.1
365
		// YYYY-MM-DD HH:MM:SS
366
367
		$time = str_replace('-', '', $time);
368
		$time = str_replace(':', '', $time);
369
		$time = str_replace(' ', '', $time);
370
371
		// YYYYMMDDHHMMSS
372
		return  mktime(
373
						substr($time, 8, 2),
374
						substr($time, 10, 2),
375
						substr($time, 12, 2),
376
						substr($time, 4, 2),
377
						substr($time, 6, 2),
378
						substr($time, 0, 4)
379
						);
380
	}
381
}
382
383
// ------------------------------------------------------------------------
384
385
/**
386
 * Unix to "Human"
387
 *
388
 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
389
 *
390
 * @access	public
391
 * @param	integer Unix timestamp
392
 * @param	bool	whether to show seconds
393
 * @param	string	format: us or euro
394
 * @return	string
395
 */
396
if ( ! function_exists('unix_to_human'))
397
{
398
	function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
399
	{
400
		$r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
401
402
		if ($fmt == 'us')
403
		{
404
			$r .= date('h', $time).':'.date('i', $time);
405
		} else
406
		{
407
			$r .= date('H', $time).':'.date('i', $time);
408
		}
409
410
		if ($seconds)
411
		{
412
			$r .= ':'.date('s', $time);
413
		}
414
415
		if ($fmt == 'us')
416
		{
417
			$r .= ' '.date('A', $time);
418
		}
419
420
		return $r;
421
	}
422
}
423
424
// ------------------------------------------------------------------------
425
426
/**
427
 * Convert "human" date to GMT
428
 *
429
 * Reverses the above process
430
 *
431
 * @access	public
432
 * @param	string	format: us or euro
433
 * @return	integer
434
 */
435
if ( ! function_exists('human_to_unix'))
436
{
437
	function human_to_unix($datestr = '')
438
	{
439
		if ($datestr == '')
440
		{
441
			return FALSE;
442
		}
443
444
		$datestr = trim($datestr);
445
		$datestr = preg_replace("/\040+/", ' ', $datestr);
446
447
		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))
448
		{
449
			return FALSE;
450
		}
451
452
		$split = explode(' ', $datestr);
453
454
		$ex = explode("-", $split['0']);
455
456
		$year  = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
457
		$month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
458
		$day   = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
459
460
		$ex = explode(":", $split['1']);
461
462
		$hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
463
		$min  = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
464
465
		if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
466
		{
467
			$sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
468
		}
469
		else
470
		{
471
			// Unless specified, seconds get set to zero.
472
			$sec = '00';
473
		}
474
475
		if (isset($split['2']))
476
		{
477
			$ampm = strtolower($split['2']);
478
479
			if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
480
				$hour = $hour + 12;
481
482
			if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
483
				$hour = '00';
484
485
			if (strlen($hour) == 1)
486
				$hour = '0'.$hour;
487
		}
488
489
		return mktime($hour, $min, $sec, $month, $day, $year);
490
	}
491
}
492
493
// ------------------------------------------------------------------------
494
495
/**
496
 * Timezone Menu
497
 *
498
 * Generates a drop-down menu of timezones.
499
 *
500
 * @access	public
501
 * @param	string	timezone
502
 * @param	string	classname
503
 * @param	string	menu name
504
 * @return	string
505
 */
506
if ( ! function_exists('timezone_menu'))
507
{
508
	function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
509
	{
510
		$CI = & get_instance();
511
		$CI->lang->load('date');
0 ignored issues
show
Bug introduced by
The property lang does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
512
513
		if ($default == 'GMT') {
514
					$default = 'UTC';
515
		}
516
517
		$menu = '<select name="'.$name.'"';
518
519
		if ($class != '')
520
		{
521
			$menu .= ' class="'.$class.'"';
522
		}
523
524
		$menu .= ">\n";
525
526
		foreach (timezones() as $key => $val)
0 ignored issues
show
Bug introduced by
The expression timezones() of type array<string,integer|dou...teger"}>|integer|double is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
527
		{
528
			$selected = ($default == $key) ? " selected='selected'" : '';
529
			$menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
530
		}
531
532
		$menu .= "</select>";
533
534
		return $menu;
535
	}
536
}
537
538
// ------------------------------------------------------------------------
539
540
/**
541
 * Timezones
542
 *
543
 * Returns an array of timezones.  This is a helper function
544
 * for various other ones in this library
545
 *
546
 * @access	public
547
 * @param	string	timezone
548
 * @return	string
549
 */
550
if ( ! function_exists('timezones'))
551
{
552
	function timezones($tz = '')
553
	{
554
		// Note: Don't change the order of these even though
555
		// some items appear to be in the wrong order
556
557
		$zones = array(
558
						'UM12'		=> -12,
559
						'UM11'		=> -11,
560
						'UM10'		=> -10,
561
						'UM95'		=> -9.5,
562
						'UM9'		=> -9,
563
						'UM8'		=> -8,
564
						'UM7'		=> -7,
565
						'UM6'		=> -6,
566
						'UM5'		=> -5,
567
						'UM45'		=> -4.5,
568
						'UM4'		=> -4,
569
						'UM35'		=> -3.5,
570
						'UM3'		=> -3,
571
						'UM2'		=> -2,
572
						'UM1'		=> -1,
573
						'UTC'		=> 0,
574
						'UP1'		=> +1,
575
						'UP2'		=> +2,
576
						'UP3'		=> +3,
577
						'UP35'		=> +3.5,
578
						'UP4'		=> +4,
579
						'UP45'		=> +4.5,
580
						'UP5'		=> +5,
581
						'UP55'		=> +5.5,
582
						'UP575'		=> +5.75,
583
						'UP6'		=> +6,
584
						'UP65'		=> +6.5,
585
						'UP7'		=> +7,
586
						'UP8'		=> +8,
587
						'UP875'		=> +8.75,
588
						'UP9'		=> +9,
589
						'UP95'		=> +9.5,
590
						'UP10'		=> +10,
591
						'UP105'		=> +10.5,
592
						'UP11'		=> +11,
593
						'UP115'		=> +11.5,
594
						'UP12'		=> +12,
595
						'UP1275'	=> +12.75,
596
						'UP13'		=> +13,
597
						'UP14'		=> +14
598
					);
599
600
		if ($tz == '')
601
		{
602
			return $zones;
603
		}
604
605
		if ($tz == 'GMT') {
606
					$tz = 'UTC';
607
		}
608
609
		return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
610
	}
611
}
612
613
614
/* End of file date_helper.php */
615
/* Location: ./system/helpers/date_helper.php */