Date Maker- Drop Down menu

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Date Maker- Drop Down menu

Post by Steveo31 »

Just what it says. Makes dates in the next XX days and stores them in a POST variable as MySQL formatted.

Code: Select all

<?php
/*
* Drop Down Date Maker by Steve Geer
* 
* Purpose:
* 	To make a dropdown menu based on the next XX days.
* 	
* Utility:
* 	Helpful for times when the user needs to schedule something
* 	and a set range of dates needs to be set.  For example, you
* 	have a Tennis Organization, and you would like to allow the 
* 	user to schedule a lesson a week in advance.  Removes the 
* 	need for validation scripts to make sure that the date is not
* 	already taken (kinda) or an invalid format.
* 
* 	This script stores the value of the date chosen in the POST 
* 	variable $apptdate, but feel free to change it as you see fit.
* 	The date is stored as MySQL compatible DATE format YYYY-MM-DD.
* 	You can change this in the "value" attribute of the <option>
* 	element. 
* 
* Copyrights:
* 	No, no.  I looked online and couldn't find much in the way of
* 	a script like this, so I just typed one out myself.  I am proud
* 	of it, but doubt it will be used much.  If you do use it, let me
* 	know, eh?
* 
* Resources:
* 	http://forums.devnetwork.net/
* 	http://www.phpfreaks.com/forums/
* 	http://www.hotscripts.com/
* 	`PHP & MySQL Web Development` by Luke Welling and Laura Thomson
* 
*/
if(isset($_POST['appt'])){
	echo $_POST['appt'];
}
?>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<select name="appt">

<?php
$t = mktime(); //initial timestamp
$td = getdate($t); //formatted date

//Change the limit to the amount of days you need it to "see" into the future.
//In this example it is going 31 days into the future.  Why not 32?  31 is < 32.
for($i=0;$i<32;$i++){ 
	
	//create the initial timestamp:
	$t2 = mktime($td['hours'], $td['minutes'], 0, $td['mon'], ($td['mday']+5)+$i, $td['year']); 
	
	//formats the timestamp created above:
	$td2 = getdate($t2);
	
	//makes the HTML option values:
	echo '<option name="apptdate" value="'.$td2['year'].'-'.$td2['mon'].'-'.$td2['mday'].'">';
	echo $td2['mon'].'/'.$td2['mday'].'/'.$td2['year'];
	echo '</option>';
}

?>
</select>
<input type="submit">
</form>
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Great job. :)
Image Image
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Thanks!! :D
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Or:

Code: Select all

<?php
/*
    param (integer) $days_in_advance
*/
function daysInAdvanceOptions($days_in_advance) 
{        
    $now = mktime();
    $options = '';

    for($i=0; $i<$days_in_advance; $i++)
    {
        $current_timestamp = $now + ($i*60*60*24);
        $options .= '<option value="';
        $options .= date("Y-m-d", $current_timestamp);
        $options .= '">';
        $options .= date("M dS, Y", $current_timestamp);
        $options .= '</option>';
        $options .= "\n";
    }
    return $options;
}
?>
Last edited by McGruff on Tue Aug 09, 2005 2:20 am, edited 1 time in total.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Yea, but I'm still getting the hang of this stuff... :)
Post Reply