Location: PHPKode > projects > O - OpenSource GroupWare > osgw/osgw/classes/class.date.php
<?php
/**************************************************************************
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   @Authors:	Ryan Thompson(hide@address.com)
***************************************************************************/
/*$Id: class.date.php,v 1.11 2004/05/04 05:32:43 rthomp Exp $*/
class date
{
	var $weekdays;
	var $med_weekdays;
	var $short_weekdays;
	var $months;
	var $med_months;
	var $short_months;
	var $now;
	var $day_start;
	var $day_end;
	var $long_date;
	var $short_date;
	var $time;
	var $inc = 0;


	function date()
	{

	}
	
	/*!
		@function is_leapyear()
		@author Ryan Thompson
		@abstract Determines whether given year is a leap year.
		@version 0.1
		@params $year - Year to determine if leap year
		@return TRUE/FALSE
		@since 10-11-2003
	*/
	function is_leapyear($year)
	{
		//This isn't foolproof. If should work but I think it's got to check something else.
		//This whole Calendar Math thing is new to me.
		if(($year % 4) == 0)
		{
			if(($year % 100) == 0)
			{
				if(($year % 400) == 0)
				{
					return TRUE;
				} else {
					return FALSE;
				}
			} else {
				return TRUE;
			}
		} else {
			return FALSE;
		}
	}

	/*!
		@function days_in_month()
		@author Ryan Thompson
		@abstract Returns the number of days in given month
		@version 0.1
		@params $month - Month to return number of days for
		@return $num_days
		@since 10-11-2003
	*/
	function days_in_month($month)
	{
			
		$total_days = array(1=>31, 28, 31, 30, 30, 30, 31, 31, 30, 31, 30, 31);
		if($total_days[$month] == 28)
		{
			$num_days = $total_days[$month];
			if($this->is_leapyear(date('Y')))
			{
				$num_days++;
			}
		} else {
			$num_days = $total_days[$month];
		}
			
		return $num_days;
		
		
	}
	
	/*!
		@function next_time()
		@author Ryan Thompson
		@abstract 
		@version 0.1
		@params $month - The month item occurs in - or 0 if special
		@params $day - Day item occurs in - or 0 if special
		@params $occurence - string containing (month:week_num:weekday)
		@return $date
		@since 10-11-2003
	*/
	function next_time($month, $day, $occurrence, $year)
	{
		GLOBAL $O;
		$this->days_in_month($month);
		$leapyear = $this->is_leapyear($year);

		if($day == 0)
		{
			$occ = explode(':', $occurrence);
			$month = $occ[0];
			$week_num = $occ[1];
		
			$weekday = $occ[2];
			$month;
			$year;
			//$f_wkday = date('w', mktime(0,0,0, $month, 1, $year));
			
			$num_days = date('t', mktime(0,0,0, $month, 1, $year));
			$count = 0;
			for($i = 1; $i <= $num_days; $i++)
			{
				if(date('w', mktime(0,0,0, $month, $i, $year)) == $weekday)
				{
					$count++;
				}
				if($count == $week_num)
				{
					break;
				}
				
			}
			$date = date('U', mktime(0,0,0, $month, $i, $year));
			if($date < time())
			{
				$date = $this->next_time($month, $day, $occurrence, $year + 1);	
			}

		} else {
			
			if(date('U', mktime(0,0,0, $month, $day, date('Y'))) < time())
			{
				//GET NEXT YEAR
				$date = date('U', mktime(0,0,0, $month, $day, $year + 1));
			} else {
				//GET THIS YEAR
				
				$date = date('U', mktime(0,0,0, $month, $day, date('Y')));
			}
		}
		
		return $date;
	}
	
	/*!
		@function set_date_format()
		@author Ryan Thompson
		@abstract Sets Time and Date format for lists, etc.
		@version 0.1
		@return TRUE
	*/
	function set_date_format()
	{
		GLOBAL $user;

		$this->set_time($user->user_id);
		$this->set_dates($user->user_id);
		
		return TRUE;
	}
	
	/*!
		@function set_time()
		@author Ryan Thompson
		@abstract Sets format to be used for times
		@version 0.1
		@params $user_id - User ID for time format lookup
		@return $this->time
	*/
	function set_time($user_id)
	{
		GLOBAL $O, $db;
		$sql = "SELECT value FROM o_preferences WHERE user_id='$user_id' AND preference='time'";
		$db->query($sql);
		$db->fetch_results();
		return $this->time = $db->record['value'];
	}
	
	/*!
		@function set_dates()
		@author Ryan Thompson
		@abstract Sets the long and short format for dates
		@version 0.1
		@params $user_id - User ID for date format lookup
		@return TRUE/FALSE
		@since 10-11-2003
	*/
	function set_dates($user_id)
	{
		
		
		GLOBAL $O, $db, $error;
		//$error->debug('date');
		$sql = "SELECT value FROM o_preferences WHERE user_id='$user_id' AND preference='date'";
		$db->query($sql);
		$db->fetch_results();
		$this->long_date = $db->record['value'];
		
		$sql = "SELECT value FROM o_preferences WHERE user_id='$user_id' AND preference='short_date'";
		$db->query($sql);
		$db->fetch_results();
		$this->short_date = $db->record['value'];
		
		return;
	}
	
	/*!
		@function get_start_of_day()
		@author Ryan Thompson
		@abstract Return Unix Timestamp for 00:00:00 on given date
		@version 0.1
		@params $time - Unix Timestamp of time to work from
		@return $this->start_of_day
	*/
	function get_start_of_day($time)
	{
		$m = date('n', $time);
		$d = date('j', $time);
		$y = date('Y', $time);

		$this->start_of_day = date('U', mktime(0,0,0,$m,$d,$y));
		return $this->start_of_day;
	}

	/*!
		@function get_end_of_day()
		@author Ryan Thompson
		@abstract Returns Unix Timestamp for 23:59:59 on given date
		@version 0.1
		@params $time - Unix Timestamp of time to work from
		@return $this->end_of_day
		@since 10-11-2003
	*/
	function get_end_of_day($time)
	{
		$m = date('n', $time);
		$d = date('j', $time);
		$y = date('Y', $time);

		$this->end_of_day = date('U', mktime(23,59,59,$m,$d,$y));
		return $this->end_of_day;
	}
	
	/*!
		@function header_date()
		@author Ryan Thompson
		@abstract Returns date/time for O header
		@version 0.1
		@return $return_date
		@since 10-11-2003
	*/
	function header_date($user_id)
	{

		GLOBAL $O, $db;
		$sql = "SELECT value FROM o_preferences WHERE preference='header_date' AND user_id='$user_id'";
		$db->query($sql);
		$db->fetch_results();

		$header_date = $db->record['value'];

		$sql = "SELECT value FROM o_preferences WHERE preference='header_time' AND user_id='$user_id'";
		$db->query($sql);
		$db->fetch_results();


		$header_time = $db->record['value'];
		$return_date = NULL;
		
		switch($header_date)
		{
			case 'long':
				$return_date .= $this->long_date;
			break;
			case 'short':
				$return_date .= $this->short_date;
			break;
			case 'none':
				$return_date .= NULL;
			break;
		}

		//Only add Time if user preferences are set to include the time.
		if($header_time == 'TRUE')
		{
			$return_date .= " -- ". $this->time;
		}
			
		
		return $return_date;
	}
	
	/*!
		@function get_easter()
		@author Ryan Thompson
		@abstract If possible it returns easter.
		@version 0.1
		@params $year - Year we want to find easter for
		@return $easter_date
		@since 10-11-2003
	*/
	function get_easter($year)
	{
		if(function_exists(easter_date))
		{
			return easter_date($year);
		} else {
			return FALSE;
		}
		
	}
	
	/*!
		@function date_select()
		@author Ryan Thompson
		@abstract Creates a select box for dates (month, day, year)
		@version 0.1
		@params $position - (month, day, year)
		@return $select_box
		@since 06-06-2004 -Where did this come from??
	*/
	
	function date_select($name, $position, $selected)
	{
		GLOBAL $html, $lang;

		switch($position)
		{
			case 'day':
				$select_box = $html->select_box($name, 'count', array('min'=>1,'max'=>31), $selected);

			break;
			case 'month':
				$select_box = $html->select_box($name, 'value_count', explode(':', $lang->msgs['med_months']), $selected);
	
			break;
			case 'year':
				$select_box = $html->select_box($name, 'count', array('min'=>date('Y'), 'max'=>date('Y') + 10), $selected);
			break;
		}
		
		return $select_box;
	}

	/*!
		@function utimestamp()
		@author Ryan Thompson
		@abstract Creates a Unix timestamp from a given date
		@version 0.1
		@params $hour
		@params $min
		@params $day
		@params $month
		@params $year
		@return $timestamp
		@since 03-05-2004
	*/
	
	function utimestamp($hour, $min, $day, $month, $year)
	{
		$timestamp = date('U', mktime($hour, $min, 0, $month, $day, $year));
		return $timestamp;	
	}
	
	
}
?>
Return current item: O - OpenSource GroupWare