Location: PHPKode > projects > ConPortal > conportal/inc/datetime.php
<?php
/*
 *  ConPortal - Pomona College ITS scheduling appplication
 *  Copyright (C) 2005-2008  Pomona College, Bucknell Unversity
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of version 2 of the GNU General Public License
 *  as published by the Free Software Foundation.
 *
 *  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 should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * Retrieves date part of a timestamp, setting time to midnight
 */
function date_part ($timestamp)
{
	$dateArray = getdate($timestamp);
	return mktime(0, 0, 0, $dateArray['mon'], $dateArray['mday'], $dateArray['year']);
}

/*
 * Retrieves time part of a timestamp, number of seconds since midnight
 */
function time_part ($timestamp)
{
	return $timestamp - date_part($timestamp);
}

function timestamp_to_monthday ($timestamp)
{
	return date('n/j', $timestamp);
}

function timestamp_to_prettydate ($timestamp)
{
    return date("l, F jS, Y", $timestamp);
}

// take timestamp and return (previous sunday, next saturday)
function start_end_of_week($timestamp)
{
    $day_of_week = date("w", $timestamp);
    // Magical abuse of math. It works, trust me.
    return array(strtotime("-$day_of_week days", $timestamp),
                 strtotime("+".(6-$day_of_week)." days", $timestamp));
}

// Print stuff for the last two years and one year ahead of us...
function print_year_options($default = '')
{
    if ($default == '')
        $default = date('Y');

    for ($i = -2; $i <= 1; ++$i)
	{
        $str = date("Y", strtotime("$i year"));
        if ($str == $default)
            echo "<option value='$str' selected='selected'>$str</option>\n";
        else
            echo "<option value='$str'>$str</option>\n";
    }
}

/* Bug fixed here - added the 1 after $i in the date call... otherwise it would list
certain months twice late in the month, because it would use the current date...
So if you want September, and today's the 31st, PHP counts the 31st of Sept. as 
October 1st.  */
function print_month_options($default = '')
{
    if ($default == '')
        $default = date('F');

    for ($i = 1; $i <= 12; ++$i) 
	{
        $str = date('F', mktime(0, 0, 0, $i, 1)); 
        if ($i == $default)
            echo "<option value='$i' selected='selected'>$str</option>\n";
        else
            echo "<option value='$i'>$str</option>\n";
    }
}

function print_day_options($default = '')
{
    if ($default == '')
        $default = date('j');

    for ($i = 1; $i <= 31; ++$i)
	{
        if ((int)$i == (int)$default)
            echo "<option selected='selected'>$i</option>\n";
        else
            echo "<option>$i</option>\n";
    }
}

// Display friendly year-month-day dropdowns.  
// Suffix is the suffix to attach to the names of items so that we can differentiate
// which values go where if we have more than one set of y-m-d values
// Some of this is cribbed from shift_history.php
function display_friendly_ymd_dropdowns($suffix, $timestamp)
{
	//if we're passed a blank timestamp, use current time
	if($timestamp == "0000-00-00 00:00:00")
	{
		$month = date('n');
		$day = date('d');
		$year = date('Y');
	}
	//otherwise, use the value passed in
	else
	{
		$timestamp_array = timestamp_to_array($timestamp);
		$month = $timestamp_array['month'];
		$day = $timestamp_array['day'];
		$year = $timestamp_array['year'];
	}

	echo " <select name='month_" . $suffix ."' style='width: 8em'>";
	print_month_options($month);
    	echo "</select><select name='day_" . $suffix ."' style='width: 3.5em'>";
        print_day_options($day);
	echo "</select><select name='year_" . $suffix . "' style='width: 4.6em'>";
        print_year_options($year);
	echo "</select> ";

}

// Takes in year, month, day, and returns them
// Hooray for built-in PHP date validity checking goodness!
function return_ymd_timestamp_string($year, $month, $day)
{
	if(checkdate($month, $day, $year))
	{
		//pad month and day if necessary
		if($month < 10)
			$month = "0" . strval($month);
		if($day < 10)
			$day = "0" . strval($day);

		$str = $year . "-" . $month . "-" . $day;
		return $str;
	}
	else
	{
		return -1;	
	}

}

// Display friendly 12-hour hour/minute selector.  
// Suffix is the suffix to attach to the names of items so that we can differentiate
// which values go where if we have more than one set of hour/minute values
function display_friendly_hm_box($suffix, $timestamp = "")
{
	//if we're passed a blank timestamp, use current time
	if($timestamp == "0000-00-00 00:00:00")
	{
		$hour = date('h');
		$min = date('i');
		$AMPM = date('A');
	}
	//otherwise, use the value passed in
	else
	{
		$timestamp_array = timestamp_to_array($timestamp);
		$hour = $timestamp_array['12hour'];
		$min = $timestamp_array['min'];
		$AMPM = $timestamp_array['AMPM'];
	}

	echo "at <input type='text' name='hour_" . $suffix . "' style='width: 1.5em' value='" . $hour . "' />:";
	echo "<input type='text' name='min_" . $suffix . "' style='width: 1.5em' value='" . $min . "' /> ";
	if($AMPM == "AM")
	{
		echo "<select name='AMPM_" . $suffix . "'> <option value='AM' selected='selected'>AM</option><option value='PM'>PM</option></select>";
	}
	else
	{
		echo "<select name='AMPM_" . $suffix . "'> <option value='AM'>AM</option><option value='PM' selected='selected'>PM</option></select>";
	}
}

// Display friendly 12-hour hour/minute selector using predefined dropdown boxes. 
// Hooray for making drool-proof user interfaces!
// Suffix is the suffix to attach to the names of items so that we can differentiate
// which values go where if we have more than one set of hour/minute values
function display_friendly_12h_15m_AMPM_dropdown($suffix)
{
	$m = array('00', '05', '10', '15', '20', '25', '30', '35', '40', '45', '50', '55');
    $mins = '';
    foreach ($m as $min)
        $mins .= "<option value='$min'>$min</option>\n";
	
    $h = array('00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12');
    $hours = '';
    foreach ($h as $hour)
        $hours .= "<option value='$hour'>$hour</option>\n";
	
	echo "<select name='hour_" . $suffix . "'>" . $hours . "</select>";
	echo ":";
	echo "<select name='min_" . $suffix . "'>" . $mins . "</select>";
	echo " ";
	echo "<select name='AMPM_" . $suffix . "'> <option value='AM' selected='selected'>AM</option><option value='PM'>PM</option></select>";
	
	
	
}

// Display friendly day of the week dropdown boxes. 
// Hooray for making drool-proof user interfaces!
// Suffix is the suffix to attach to the names of items so that we can differentiate
// which values go where if we have more than one set of hour/minute values
function display_friendly_day_of_week_dropdown($suffix)
{
	$d = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
               'Saturday', 'Sunday');
    $days = '';
    foreach ($d as $day)
        $days .= "<option value='$day'>$day</option>\n";
	
	echo "<select name='day_" . $suffix . "'>" . $days . "</select>";
}

// returns HH:MM:SS value, setting seconds to "00" and correcting for AM/PM
// returns -1 if values for hour & minute aren't acceptable
function return_hm_timestamp_string($hour, $min, $AMPM)
{
	//see if the hour value makes sense, and pad if necessary, return -1 if it's out of bounds
	$hour = input_sanity_check($hour, 0, 12);
	if($hour == -1)
		return -1;

	//see if the min value makes sense, and pad if necessary, return -1 if it's out of bounds
	$min = input_sanity_check($min, 0, 59);
	if($min == -1)
		return -1;

	//"correct" times of 00:xy PM or 12:xy AM
	if($hour == "00" && $AMPM == "PM")
		$hour == "12";

	if($hour == "12" && $AMPM == "AM")
		$AMPM == "PM";
		
	//correct for AM/PM - but not at 12PM!
	if( ($AMPM == "PM") && ($hour!=12) )
		$hour = intval($hour) + 12;

	//Slap string together and return.  Let seconds be "00" because, really...
	$returnstring = $hour . ":" . $min . ":00";
	return $returnstring;
}

//added by George 5/15/08
//still used in set_pick_start_time; that should be replaced with friendly dropdowns...
function print_timestamp_rightnow()
{
	echo date("Y-m-d H:i:s");
}

//make sure value is between x and y, pad with leading 0 if necessary, and return the value, or -1 if 
//something is wrong.  Intended for checking/padding month, date, hour, and minute values
function input_sanity_check($value, $min, $max)
{
	if(($value > $max) || ($value < $min))
	{
		return -1;
	}
	//pad, if necessary
	if( ($value < 10) && (substr(strval($value), 0, 1 != "0") ) )
	{
		$str = "0" . strval($value);
		return $str; 
	} 
	//value is in acceptable range and doesn't need padding, so...
	return $value;
}

//takes a timestamp (with date component) in and returns an array broken into it's parts
function timestamp_to_array($timestamp)
{
	$result = array();
	$result['year'] = substr($timestamp,0,4);
	$result['month'] = substr($timestamp,5,2);
	$result['day'] = substr($timestamp,8,2);
	$result['min'] = substr($timestamp,14,2);
	$result['24hour'] = substr($timestamp,11,2);
	//12 hour time
	if($result['24hour'] >= 12)
	{
		if($result['24hour'] > 12)
		{
			$result['12hour'] = $result['24hour'] - 12;
		}
		else
		{
			$result['12hour'] = $result['24hour'];
		}
		$result['AMPM'] = "PM";
	}
	else
	{
		$result['12hour'] = $result['24hour'];
		$result['AMPM'] = "AM";
	}

	return $result;
}

//takes a raw time and breaks it up into pieces.  
function time_to_array($timestamp)
{
	$result = array();
	$result['min'] = substr($timestamp,3,2);
	$result['24hour'] = substr($timestamp,0,2);
	//12 hour time
	if($result['24hour'] >= 12)
	{
		if($result['24hour'] > 12)
		{
			$result['12hour'] = $result['24hour'] - 12;
		}
		else
		{
			$result['12hour'] = $result['24hour'];
		}
		$result['AMPM'] = "PM";
	}
	else
	{
		$result['12hour'] = $result['24hour'];
		$result['AMPM'] = "AM";
	}

	return $result;
}

/* Lazy man's sanity check on timestamp strings - mostly verifies that the string is
structured properly, NOT that the values are themselves are particularly sane!
This will catch a sanity check returning -1 from input_sanity_check or one of the two return_..._string functions above.  
Note that this is not Y2.1K (or Y1.9K!) safe!  :)   */
function is_valid_timestamp_string($timestamp)
{
	if(ereg('20[0-9]{2}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:00', $timestamp))
	{
		return TRUE;
	}
	return FALSE;
}


?>
Return current item: ConPortal