PHPBuilder - date / date-time select list generator



RSS Twitter
Snippets Calendars Dates

date / date-time select list generator

by: Caleb Lloyd
|
June 10, 2008

Version: 1

Type: Function

Category: Calendars/Dates

License: GNU General Public License

Description: generates an HTML select list that defaults to the current date or date/time, or sets the date or date/time to user specified values. $name is the select list base name. each select list will have either y (year) m (month) d (day) h (hour) or i (minute) immediately following the base name. variables $y, $m, $d, $h, $i, can be passed into the function in order for it to default to a certain date or date/time.



<?php

function generatedateselect($name,$y, $m, $d)
	{
		$now=1;
		if ( ($y>0) && ($m>0) && ($d>0) )
			{
				$now=0;
			}
		$months=array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec');
		$r='';
		$r.='<select name="'.$name.'d">';
		for ($x=1;$x<=31;$x++)
			{
				$r.='<option value="'.$x.'"';
				if ($now)
					{
						if ($x==date("d"))
							{
								$r.=' SELECTED ';
							}
					}
				else
					{
						if ($x==$d)
							{
								$r.=' SELECTED ';
							}
					}
				$r.='>'.$x.'</option>';
			}
		$r.='</select> ';
		$r.='<select name="'.$name.'m">';
		for ($x=1;$x<=12;$x++)
			{
				$r.='<option value="'.$x.'"';
				if ($now)
					{
						if ($x==date("m"))
							{
								$r.=' SELECTED ';
							}
					}
				else
					{
						if ($x==$m)
							{
								$r.=' SELECTED ';
							}
					}
				$r.='>'.$months[$x-1].'</option>';
			}
		$r.='</select> ';
		$r.='<select name="'.$name.'y">';
		for ($x=2008;$x<=2018;$x++)
			{
				$r.='<option value="'.$x.'"';
				if ($now)
					{
						if ($x==date("Y"))
							{
								$r.=' SELECTED ';
							}
					}
				else
					{
						if ($x==$y)
							{
								$r.=' SELECTED ';
							}
					}
				$r.='>'.$x.'</option>';
			}
		$r.='</select> ';
		return $r;
	}
	
function generatedatetimeselect($name, $y, $m, $d, $h, $i)
	{
		$now=1;
		if ( ($y>0) && ($m>0) && ($d>0) && ($h>=0) && ($i>=0) )
			{
				$now=0;
			}
		$r=generatedateselect($name,$y,$m,$d);
		$r.='<select name="'.$name.'h">';
		for ($x=0;$x<=23;$x++)
			{
				$r.='<option value="'.$x.'"';
				if ($now)
					{
						if ($x==date("H"))
							{
								$r.=' SELECTED ';
							}
					}
				else
					{
						if ($x==$h)
							{
								$r.=' SELECTED ';
							}
					}
				$r.='>'.$x.'</option>';
			}
		$r.='</select> ';
		$r.='<select name="'.$name.'i">';
		for ($x=0;$x<=59;$x++)
			{
				$r.='<option value="'.$x.'"';
				if ($now)
					{
						if ($x==date("i"))
							{
								$r.=' SELECTED ';
							}
					}
				else
					{
						if ($x==$i)
							{
								$r.=' SELECTED ';
							}
					}
				$r.='>'.$x.'</option>';
			}
		$r.='</select> ';
		return $r;
	}



?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Caleb Lloyd

Comment:



Comment:

(Maximum characters: 1200). You have characters left.