Location: PHPKode > projects > ACollab > include/lib/date_functions.inc.php
<?php 
/****************************************************************************************/
/* ACollab                                                                              */
/****************************************************************************************/
/* Copyright (c) 2002-2004  Adaptive Technology Resource Centre / University of Toronto */
/*                                                                                      */
/* http://atutor.ca/acollab                                                             */
/*                                                                                      */
/* This program is free software. You may 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.                                               */
/*                                                                                      */
/* This program is distributed in the hope that it will be useful, but                  */
/* WITHOUT ANY WARRANTY; without even the implied warranty of                           */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                                 */
/* See the GNU General Public License for more details.                                 */
/*                                                                                      */
/* You may access the GNU General Public License at:                                    */
/* http://www.opensource.org/licenses/gpl-license.php                                   */
/*                                                                                      */
/* You may contact the Adaptive Technology Resource Centre at                           */
/* Robarts Library, University of Toronto                                               */
/* 130 St. George Street, Toronto, Ontario, Canada M5S 1A5                              */
/* Further contact information is available at http://www.utoronto.ca/atrc/             */
/****************************************************************************************/
/* Programmer:                                                                          */
/* Joel Kronenberg - ATRC                                                               */
/****************************************************************************************/

if (!defined('AC_INCLUDE_PATH')) { exit; }


/* Uses the same options as date(), but require a % infront of each argument and the
/* textual values are language dependant ( unlike date() ).

	the following options were added as language dependant:

	%D: A textual representation of a week, three letters Mon through Sun
	%F: A full textual representation of a month, such as January or March January through December
	%l (lowercase 'L'): A full textual representation of the day of the week Sunday through Saturday
	%M: A short textual representation of a month, three letters Jan through Dec
	?? %S: English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j
	?? %a: Lowercase Ante meridiem and Post meridiem am or pm 
	?? %A: Uppercase Ante meridiem and Post meridiem AM or PM 

	valid format_types:
	AT_DATE_MYSQL_DATETIME:		YYYY-MM-DD HH:MM:SS
	AT_DATE_MYSQL_TIMESTAMP_14:	YYYYMMDDHHMMSS
	AT_DATE_UNIX_TIMESTAMP:		seconds since epoch
	AT_DATE_INDEX_VALUE:		0-x, index into a date array
*/
function AT_date($format='%Y-%M-%d', $timestamp = '', $format_type=AT_DATE_MYSQL_DATETIME)
{	
	static $day_name_ext, $day_name_con, $month_name_ext, $month_name_con;

	if (!isset($day_name_ext)) {
		$day_name_ext = array(	_AC('date_sunday'), 
								_AC('date_monday'), 
								_AC('date_tuesday'), 
								_AC('date_wednesday'), 
								_AC('date_thursday'), 
								_AC('date_friday'),
								_AC('date_saturday'));

		$day_name_con = array(	_AC('date_sun'), 
								_AC('date_mon'), 
								_AC('date_tue'), 
								_AC('date_wed'),
								_AC('date_thu'), 
								_AC('date_fri'), 
								_AC('date_sat'));

		$month_name_ext = array(_AC('date_january'), 
								_AC('date_february'), 
								_AC('date_march'), 
								_AC('date_april'), 
								_AC('date_may'),
								_AC('date_june'), 
								_AC('date_july'), 
								_AC('date_august'), 
								_AC('date_september'),
								_AC('date_october'),
								_AC('date_november'),
								_AC('date_december'));

		$month_name_con = array(_AC('date_jan'), 
								_AC('date_feb'), 
								_AC('date_mar'), 
								_AC('date_apr'), 
								_AC('date_may_short'),
								_AC('date_jun'), 
								_AC('date_jul'), 
								_AC('date_aug'), 
								_AC('date_sep'), 
								_AC('date_oct'), 
								_AC('date_nov'),
								_AC('date_dec'));
	}

	if ($format_type == AT_DATE_INDEX_VALUE) {
		if ($format == '%D') {
			return $day_name_con[$timestamp-1];
		} else if ($format == '%l') {
			return $day_name_ext[$timestamp-1];
		} else if ($format == '%F') {
			return $month_name_ext[$timestamp-1];
		} else if ($format == '%M') {
			return $month_name_con[$timestamp-1];
		}
	}

	if ($timestamp == '') {
		$timestamp = time();
		$format_type = AT_DATE_UNIX_TIMESTAMP;
	}

	/* 1. convert the date to a Unix timestamp before we do anything with it */
	//$time_zone_offset = SERVER_TIME_ZONE - $_SESSION['time_zone'];

	if ($format_type == AT_DATE_MYSQL_DATETIME) {
		$year	= substr($timestamp,0,4);
		$month	= substr($timestamp,5,2);
		$day	= substr($timestamp,8,2);
		$hour	= substr($timestamp,11,2);
		$min	= substr($timestamp,14,2);
		$sec	= substr($timestamp,17,2);
	    $timestamp	= mktime($hour + $time_zone_offset, $min, $sec, $month, $day, $year);

	} else if ($format_type == AT_DATE_MYSQL_TIMESTAMP_14) {
	    $hour		= substr($timestamp,8,2);
	    $minute		= substr($timestamp,10,2);
	    $second		= substr($timestamp,12,2);
	    $month		= substr($timestamp,4,2);
	    $day		= substr($timestamp,6,2);
	    $year		= substr($timestamp,0,4);
	    $timestamp	= mktime($hour + $time_zone_offset, $minute, $second, $month, $day, $year);  
	}

	/* pull out all the %X items from $format */
	$first_token = strpos($format, '%');
	if ($first_token === false) {
		/* no tokens found */
		return $timestamp;
	} else {
		$tokened_format = substr($format, $first_token);
	}
	$tokens = explode('%', $tokened_format);
	array_shift($tokens);
	$num_tokens = count($tokens);

	$output = $format;
	for ($i=0; $i<$num_tokens; $i++) {
		$tokens[$i] = substr($tokens[$i],0,1);

		if ($tokens[$i] == 'D') {
			$output = str_replace('%D', $day_name_con[date('w', $timestamp)],$output);
		
		} else if ($tokens[$i] == 'l') {
			$output = str_replace('%l', $day_name_ext[date('w', $timestamp)],$output);
		
		} else if ($tokens[$i] == 'F') {
			$output = str_replace('%F', $month_name_ext[date('n', $timestamp)-1],$output);		
		
		} else if ($tokens[$i] == 'M') {
			$output = str_replace('%M', $month_name_con[date('n', $timestamp)-1],$output);

		} else {

			/* this token doesn't need translating */
			$value = date($tokens[$i], $timestamp);
			if ($value != $tokens[$i]) {
				$output = str_replace('%'.$tokens[$i], $value, $output);
			} /* else: this token isn't valid. so don't replace it. Eg. try %q */
		}
	}

	return $output;
}


function date_print_select($date = '', $name, $can_be_empty, $show_what) {
	if (($date == '') && !$can_be_empty) {
		$today_day	= date('d');
		$today_mon	= date('m');
		$today_year	= date('Y');

	} else {
		$today_year = substr($date, 0, 4);
		$today_mon	= substr($date, 5, 2);
		$today_day	= substr($date, 8, 2);
		$today_hour	= substr($date, 11, 2);
		$today_min	= substr($date, 14, 2);
	}


	$start_year = $today_year-1;

	if ($can_be_empty) {
		$start_year = date('Y')-1;
	}

	if (query_bit($show_what, AT_DATE_SHOW_DATES) ) {
		echo '<select name="'.$name.'_day">';
			if ($can_be_empty) {
				echo '<option value="0"></option>';
			}

			for ($i=1; $i <= 31; $i++) {
				echo '<option value="'.$i.'"';
				if ($i == $today_day) {
					echo ' selected="selected"';
				}
				echo '>';
				echo $i.'</option>';
			}
		echo '</select>';

		echo '<select name="'.$name.'_month">';
			if ($can_be_empty) {
				echo '<option value="0"></option>';
			}
			for ($i=1; $i <= 12; $i++) {
				echo '<option value="'.$i.'"';
				if ($i == $today_mon) {
					echo ' selected="selected"';
				}
				echo '>';
				echo AT_date('%F', $i, AT_DATE_INDEX_VALUE);
				echo '</option>';
			}
		echo '</select>';

		echo '<select name="'.$name.'_year">';

			if (($can_be_empty) ) {
				echo '<option value="0"></option>';
			}
			for ($i = $start_year; $i <= $start_year+3; $i++) {
				echo '<option value="'.$i.'"';
				if ($i == $today_year) {
					echo ' selected="selected"';
				}
				echo '>';
				echo $i.'</option>';
			}
		echo '</select>';
	}

	if (query_bit($show_what, AT_DATE_SHOW_DATES) && query_bit($show_what, AT_DATE_SHOW_TIME)) {
		echo _AC('at'). ' ';
	}
	
	if (query_bit($show_what, AT_DATE_SHOW_TIME)) {
		echo '<select name="'.$name.'_hour">';
			if ($can_be_empty) {
				echo '<option value="0"></option>';
			}
			for ($i = 0; $i <= 23; $i++) {
				echo '<option value="'.$i.'"';
				if (($i == $today_hour) && ($today_hour != '')) {
					echo ' selected="selected"';
				}
				echo '>';
				echo $i.'</option>';
			}
		echo '</select>:';
		
		echo '<select name="'.$name.'_min">';
			if ($can_be_empty) {
				echo '<option value="0"></option>';
			}
			for ($i = 0; $i <= 59; $i+=5) {
				echo '<option value="'.$i.'"';
				if (($i == $today_min)  && ($today_min != '')) {
					echo ' selected="selected"';
				}
				echo '>';
				echo $i.'</option>';
			}
		echo '</select><small class="spacer">'._AC('hours_24').'</small>';
	}
}
?>
Return current item: ACollab