Use at own risk. Programs haven't been thoroughly tested.

function: form_selectdate

Easliy generates date select boxes for use in HTML forms Allows pretty flexible configuration because of its many parameters but works perfectly out of the box with default values as well.

Info

@author Kevin van Zonneveld
@version 1.0
@link http://kevin.vanzonneveld.net
@param (string) $whichPart Date part character ("Y"|"m"|"d"|"H"|"i"|"s");
@param (string) $datebox_id optional. How the element should be named on your form. default: dateBox_$whichPart
@param (string/bool) $default optional. What should be the default value of the box. default: Current date
@param (string/bool/int) $Y_range optional. How many years should be displayed. can be an integer or a string: '1995-2005'. default: 6 years in the past + 6 in the future
@param (bool/int) $monthnames optional. Can be boolean:false to only show numeric months. can be an integer to limit the amount of characters in the textual representation of a month

Example

Usage
echo form_selectdate("m");

Outputs

Source Code

download source
<?php
function form_selectdate($whichPart = "Y",$datebox_id=false ,$default=false,$Y_range=12,$monthnames=false)
{
    
 
    // default range definitions
    $ranges["Y"] = array(date("Y")-6,date("Y")+6);
    $ranges["m"] = array(1,12);
    $ranges["d"] = array(1,31);
    $ranges["H"] = array(0,23);
    $ranges["i"] = array(0,59);
    $ranges["s"] = array(0,59);
 
    // default name
    if(!$datebox_id) $datebox_id = 'dateBox_'.$whichPart.'';
 
    // default value
    if($default===false||$default=="")$default = date($whichPart);
 
    // error
    if(!isset($ranges[$whichPart])) {
        return "ERROR: invalid boxpart: '".$whichPart."', you can choose from Y,m,d,H,i,s";
    }
 
    // select the needed range (e.g. 1-31 for when part is day)
    $arrParts[$whichPart] = array();
    switch($whichPart) {
        case "Y":
            list($arrParts[$whichPart]["begin"],$arrParts[$whichPart]["end"]) = explode("-",$Y_range);
 
            if( !$arrParts[$whichPart]["begin"] || !$arrParts[$whichPart]["end"] || !is_numeric($arrParts[$whichPart]["begin"]) || !is_numeric($arrParts[$whichPart]["end"]) ) {
                if(is_numeric($Y_range)) {
                    $arrParts[$whichPart]["begin"] = date($whichPart) - intval($Y_range/2);
                    $arrParts[$whichPart]["end"]  = date($whichPart) + intval($Y_range/2);
                } else {
                    return "ERROR: invalid Y_range: '".$Y_range."'";
                }
            }
        break;
        default:
            list($arrParts[$whichPart]["begin"],$arrParts[$whichPart]["end"]) = $ranges[$whichPart];
        break;
    }
 
    // define the boxlength in pixels
    $lenw = ( strlen(date($whichPart)) * 20 )+  ($monthnames?($monthnames-2)*20:0) ;
    $buf  = '';
    $buf .= '<select style="width:'.$lenw.'px;" name="'.$datebox_id.'" id="'.$datebox_id.'"> ';
 
    for( $i=$arrParts[$whichPart]["begin"]; $i <= $arrParts[$whichPart]["end"]; $i++ ) {
        $buf .= '<option'.( $i == $default ? ' selected ' : ' ' ).' value="'.str_pad($i,2,'0',STR_PAD_LEFT).'">';
        if($whichPart=="m" && $monthnames) {
            $buf .= substr( date("F",mktime(1,1,1,intval($i),1,2000)),0,$monthnames);
        } else {
            $buf .= str_pad($i,2,'0',STR_PAD_LEFT);
        }
        $buf .= '</option> ';
    }
 
    $buf .= '</select>';
 
    return $buf;
 
}
?>

Add comment

» Currently away on vacation. I can reply your message the 24th of July 2008. Please post anyway and check back then. Thank you!

for syntax highlighting

[CODE="Javascript"]
your_code_here();
[/CODE]

Replace "Javascript"
with "php", "text", etc.
code (to make sure you are not a spammer)

Comments

No comments. Be the first!