PageRenderTime 439ms CodeModel.GetById 192ms app.highlight 84ms RepoModel.GetById 157ms app.codeStats 0ms

/halogy/helpers/date_helper.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 611 lines | 457 code | 51 blank | 103 comment | 48 complexity | cc6b26e2d512461aaf48ed8c3c356fde MD5 | raw file
  1<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2/**
  3 * CodeIgniter
  4 *
  5 * An open source application development framework for PHP 4.3.2 or newer
  6 *
  7 * @package		CodeIgniter
  8 * @author		ExpressionEngine Dev Team
  9 * @copyright	Copyright (c) 2008 - 2009, EllisLab, Inc.
 10 * @license		http://codeigniter.com/user_guide/license.html
 11 * @link		http://codeigniter.com
 12 * @since		Version 1.0
 13 * @filesource
 14 */
 15
 16// ------------------------------------------------------------------------
 17
 18/**
 19 * CodeIgniter Date Helpers
 20 *
 21 * @package		CodeIgniter
 22 * @subpackage	Helpers
 23 * @category	Helpers
 24 * @author		ExpressionEngine Dev Team
 25 * @link		http://codeigniter.com/user_guide/helpers/date_helper.html
 26 */
 27
 28// ------------------------------------------------------------------------
 29
 30/**
 31 * Get "now" time
 32 *
 33 * Returns time() or its GMT equivalent based on the config file preference
 34 *
 35 * @access	public
 36 * @return	integer
 37 */	
 38if ( ! function_exists('now'))
 39{
 40	function now()
 41	{
 42		$CI =& get_instance();
 43	
 44		if (strtolower($CI->config->item('time_reference')) == 'gmt')
 45		{
 46			$now = time();
 47			$system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
 48	
 49			if (strlen($system_time) < 10)
 50			{
 51				$system_time = time();
 52				log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
 53			}
 54	
 55			return $system_time;
 56		}
 57		else
 58		{
 59			return time();
 60		}
 61	}
 62}
 63	
 64// ------------------------------------------------------------------------
 65
 66/**
 67 * Convert MySQL Style Datecodes
 68 *
 69 * This function is identical to PHPs date() function,
 70 * except that it allows date codes to be formatted using
 71 * the MySQL style, where each code letter is preceded
 72 * with a percent sign:  %Y %m %d etc...
 73 *
 74 * The benefit of doing dates this way is that you don't
 75 * have to worry about escaping your text letters that
 76 * match the date codes.
 77 *
 78 * @access	public
 79 * @param	string
 80 * @param	integer
 81 * @return	integer
 82 */	
 83if ( ! function_exists('mdate'))
 84{
 85	function mdate($datestr = '', $time = '')
 86	{
 87		if ($datestr == '')
 88			return '';
 89	
 90		if ($time == '')
 91			$time = now();
 92		
 93		$datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));
 94		return date($datestr, $time);
 95	}
 96}
 97	
 98// ------------------------------------------------------------------------
 99
100/**
101 * Standard Date
102 *
103 * Returns a date formatted according to the submitted standard.
104 *
105 * @access	public
106 * @param	string	the chosen format
107 * @param	integer	Unix timestamp
108 * @return	string
109 */	
110if ( ! function_exists('standard_date'))
111{
112	function standard_date($fmt = 'DATE_RFC822', $time = '')
113	{
114		$formats = array(
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%O',
118						'DATE_RFC822'	=>	'%D, %d %M %y %H:%i:%s %O',
119						'DATE_RFC850'	=>	'%l, %d-%M-%y %H:%m:%i 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		if ( ! isset($formats[$fmt]))
127		{
128			return FALSE;
129		}
130	
131		return mdate($formats[$fmt], $time);
132	}
133}
134	
135// ------------------------------------------------------------------------
136
137/**
138 * Timespan
139 *
140 * Returns a span of seconds in this format:
141 *	10 days 14 hours 36 minutes 47 seconds
142 *
143 * @access	public
144 * @param	integer	a number of seconds
145 * @param	integer	Unix timestamp
146 * @return	integer
147 */	
148if ( ! function_exists('timespan'))
149{
150	function timespan($seconds = 1, $time = '')
151	{
152		$CI =& get_instance();
153		$CI->lang->load('date');
154
155		if ( ! is_numeric($seconds))
156		{
157			$seconds = 1;
158		}
159	
160		if ( ! is_numeric($time))
161		{
162			$time = time();
163		}
164	
165		if ($time <= $seconds)
166		{
167			$seconds = 1;
168		}
169		else
170		{
171			$seconds = $time - $seconds;
172		}
173		
174		$str = '';
175		$years = floor($seconds / 31536000);
176	
177		if ($years > 0)
178		{	
179			$str .= $years.' '.$CI->lang->line((($years	> 1) ? 'date_years' : 'date_year')).', ';
180		}	
181	
182		$seconds -= $years * 31536000;
183		$months = floor($seconds / 2628000);
184	
185		if ($years > 0 OR $months > 0)
186		{
187			if ($months > 0)
188			{	
189				$str .= $months.' '.$CI->lang->line((($months	> 1) ? 'date_months' : 'date_month')).', ';
190			}	
191	
192			$seconds -= $months * 2628000;
193		}
194
195		$weeks = floor($seconds / 604800);
196	
197		if ($years > 0 OR $months > 0 OR $weeks > 0)
198		{
199			if ($weeks > 0)
200			{	
201				$str .= $weeks.' '.$CI->lang->line((($weeks	> 1) ? 'date_weeks' : 'date_week')).', ';
202			}
203		
204			$seconds -= $weeks * 604800;
205		}			
206
207		$days = floor($seconds / 86400);
208	
209		if ($months > 0 OR $weeks > 0 OR $days > 0)
210		{
211			if ($days > 0)
212			{	
213				$str .= $days.' '.$CI->lang->line((($days	> 1) ? 'date_days' : 'date_day')).', ';
214			}
215	
216			$seconds -= $days * 86400;
217		}
218	
219		$hours = floor($seconds / 3600);
220	
221		if ($days > 0 OR $hours > 0)
222		{
223			if ($hours > 0)
224			{
225				$str .= $hours.' '.$CI->lang->line((($hours	> 1) ? 'date_hours' : 'date_hour')).', ';
226			}
227		
228			$seconds -= $hours * 3600;
229		}
230	
231		$minutes = floor($seconds / 60);
232	
233		if ($days > 0 OR $hours > 0 OR $minutes > 0)
234		{
235			if ($minutes > 0)
236			{	
237				$str .= $minutes.' '.$CI->lang->line((($minutes	> 1) ? 'date_minutes' : 'date_minute')).', ';
238			}
239		
240			$seconds -= $minutes * 60;
241		}
242	
243		if ($str == '')
244		{
245			$str .= $seconds.' '.$CI->lang->line((($seconds	> 1) ? 'date_seconds' : 'date_second')).', ';
246		}
247			
248		return substr(trim($str), 0, -1);
249	}
250}
251	
252// ------------------------------------------------------------------------
253
254/**
255 * Number of days in a month
256 *
257 * Takes a month/year as input and returns the number of days
258 * for the given month/year. Takes leap years into consideration.
259 *
260 * @access	public
261 * @param	integer a numeric month
262 * @param	integer	a numeric year
263 * @return	integer
264 */	
265if ( ! function_exists('days_in_month'))
266{
267	function days_in_month($month = 0, $year = '')
268	{
269		if ($month < 1 OR $month > 12)
270		{
271			return 0;
272		}
273	
274		if ( ! is_numeric($year) OR strlen($year) != 4)
275		{
276			$year = date('Y');
277		}
278	
279		if ($month == 2)
280		{
281			if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
282			{
283				return 29;
284			}
285		}
286
287		$days_in_month	= array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
288		return $days_in_month[$month - 1];
289	}
290}
291	
292// ------------------------------------------------------------------------
293
294/**
295 * Converts a local Unix timestamp to GMT
296 *
297 * @access	public
298 * @param	integer Unix timestamp
299 * @return	integer
300 */	
301if ( ! function_exists('local_to_gmt'))
302{
303	function local_to_gmt($time = '')
304	{
305		if ($time == '')
306			$time = time();
307	
308		return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));
309	}
310}
311	
312// ------------------------------------------------------------------------
313
314/**
315 * Converts GMT time to a localized value
316 *
317 * Takes a Unix timestamp (in GMT) as input, and returns
318 * at the local value based on the timezone and DST setting
319 * submitted
320 *
321 * @access	public
322 * @param	integer Unix timestamp
323 * @param	string	timezone
324 * @param	bool	whether DST is active
325 * @return	integer
326 */	
327if ( ! function_exists('gmt_to_local'))
328{
329	function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
330	{			
331		if ($time == '')
332		{
333			return now();
334		}
335	
336		$time += timezones($timezone) * 3600;
337
338		if ($dst == TRUE)
339		{
340			$time += 3600;
341		}
342	
343		return $time;
344	}
345}
346	
347// ------------------------------------------------------------------------
348
349/**
350 * Converts a MySQL Timestamp to Unix
351 *
352 * @access	public
353 * @param	integer Unix timestamp
354 * @return	integer
355 */	
356if ( ! function_exists('mysql_to_unix'))
357{
358	function mysql_to_unix($time = '')
359	{
360		// We'll remove certain characters for backward compatibility
361		// since the formatting changed with MySQL 4.1
362		// YYYY-MM-DD HH:MM:SS
363	
364		$time = str_replace('-', '', $time);
365		$time = str_replace(':', '', $time);
366		$time = str_replace(' ', '', $time);
367	
368		// YYYYMMDDHHMMSS
369		return  mktime(
370						substr($time, 8, 2),
371						substr($time, 10, 2),
372						substr($time, 12, 2),
373						substr($time, 4, 2),
374						substr($time, 6, 2),
375						substr($time, 0, 4)
376						);
377	}
378}
379	
380// ------------------------------------------------------------------------
381
382/**
383 * Unix to "Human"
384 *
385 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
386 *
387 * @access	public
388 * @param	integer Unix timestamp
389 * @param	bool	whether to show seconds
390 * @param	string	format: us or euro
391 * @return	string
392 */	
393if ( ! function_exists('unix_to_human'))
394{
395	function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
396	{
397		$r  = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
398		
399		if ($fmt == 'us')
400		{
401			$r .= date('h', $time).':'.date('i', $time);
402		}
403		else
404		{
405			$r .= date('H', $time).':'.date('i', $time);
406		}
407	
408		if ($seconds)
409		{
410			$r .= ':'.date('s', $time);
411		}
412	
413		if ($fmt == 'us')
414		{
415			$r .= ' '.date('A', $time);
416		}
417		
418		return $r;
419	}
420}
421	
422// ------------------------------------------------------------------------
423
424/**
425 * Convert "human" date to GMT
426 *
427 * Reverses the above process
428 *
429 * @access	public
430 * @param	string	format: us or euro
431 * @return	integer
432 */	
433if ( ! function_exists('human_to_unix'))
434{
435	function human_to_unix($datestr = '')
436	{
437		if ($datestr == '')
438		{
439			return FALSE;
440		}
441	
442		$datestr = trim($datestr);
443		$datestr = preg_replace("/\040+/", "\040", $datestr);
444
445		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))
446		{
447			return FALSE;
448		}
449
450		$split = preg_split("/\040/", $datestr);
451
452		$ex = explode("-", $split['0']);
453	
454		$year  = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
455		$month = (strlen($ex['1']) == 1) ? '0'.$ex['1']  : $ex['1'];
456		$day   = (strlen($ex['2']) == 1) ? '0'.$ex['2']  : $ex['2'];
457
458		$ex = explode(":", $split['1']);
459	
460		$hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
461		$min  = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
462
463		if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
464		{
465			$sec  = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
466		}
467		else
468		{
469			// Unless specified, seconds get set to zero.
470			$sec = '00';
471		}
472	
473		if (isset($split['2']))
474		{
475			$ampm = strtolower($split['2']);
476		
477			if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
478				$hour = $hour + 12;
479			
480			if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
481				$hour =  '00';
482			
483			if (strlen($hour) == 1)
484				$hour = '0'.$hour;
485		}
486			
487		return mktime($hour, $min, $sec, $month, $day, $year);
488	}
489}
490	
491// ------------------------------------------------------------------------
492
493/**
494 * Timezone Menu
495 *
496 * Generates a drop-down menu of timezones.
497 *
498 * @access	public
499 * @param	string	timezone
500 * @param	string	classname
501 * @param	string	menu name
502 * @return	string
503 */	
504if ( ! function_exists('timezone_menu'))
505{
506	function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
507	{
508		$CI =& get_instance();
509		$CI->lang->load('date');
510	
511		if ($default == 'GMT')
512			$default = 'UTC';
513
514		$menu = '<select name="'.$name.'"';
515	
516		if ($class != '')
517		{
518			$menu .= ' class="'.$class.'"';
519		}
520	
521		$menu .= ">\n";
522	
523		foreach (timezones() as $key => $val)
524		{
525			$selected = ($default == $key) ? " selected='selected'" : '';
526			$menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
527		}
528
529		$menu .= "</select>";
530
531		return $menu;
532	}
533}
534	
535// ------------------------------------------------------------------------
536
537/**
538 * Timezones
539 *
540 * Returns an array of timezones.  This is a helper function
541 * for various other ones in this library
542 *
543 * @access	public
544 * @param	string	timezone
545 * @return	string
546 */	
547if ( ! function_exists('timezones'))
548{
549	function timezones($tz = '')
550	{
551		// Note: Don't change the order of these even though
552		// some items appear to be in the wrong order
553		
554		$zones = array( 
555						'UM12'		=> -12,
556						'UM11'		=> -11,
557						'UM10'		=> -10,
558						'UM95'		=> -9.5,
559						'UM9'		=> -9,
560						'UM8'		=> -8,
561						'UM7'		=> -7,
562						'UM6'		=> -6,
563						'UM5'		=> -5,
564						'UM45'		=> -4.5,
565						'UM4'		=> -4,
566						'UM35'		=> -3.5,
567						'UM3'		=> -3,
568						'UM2'		=> -2,
569						'UM1'		=> -1,
570						'UTC'		=> 0,
571						'UP1'		=> +1,
572						'UP2'		=> +2,
573						'UP3'		=> +3,
574						'UP35'		=> +3.5,
575						'UP4'		=> +4,
576						'UP45'		=> +4.5,
577						'UP5'		=> +5,
578						'UP55'		=> +5.5,
579						'UP575'		=> +5.75,
580						'UP6'		=> +6,
581						'UP65'		=> +6.5,
582						'UP7'		=> +7,
583						'UP8'		=> +8,
584						'UP875'		=> +8.75,
585						'UP9'		=> +9,
586						'UP95'		=> +9.5,
587						'UP10'		=> +10,
588						'UP105'		=> +10.5,
589						'UP11'		=> +11,
590						'UP115'		=> +11.5,
591						'UP12'		=> +12,
592						'UP1275'	=> +12.75,
593						'UP13'		=> +13,
594						'UP14'		=> +14
595					);
596				
597		if ($tz == '')
598		{
599			return $zones;
600		}
601	
602		if ($tz == 'GMT')
603			$tz = 'UTC';
604	
605		return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
606	}
607}
608
609
610/* End of file date_helper.php */
611/* Location: ./system/helpers/date_helper.php */