Zend - The PHP Company




Date & Time

Add Code


DateSelector  

Type: code fragment
Added by: leon
Entered: 30/04/2000
Last modified: 08/12/1999
Rating: **** (12 votes)
Views: 17094
DateSelector creates three SELECT form fields for month, day, and year. The current day is selected unless a date is supplied as the second argument.


<?php

     
/*
     ** Function: DateSelector
     ** Version v2.0
     ** Last Updated: 2000-05-01
     ** Author: Leon Atkinson <leon@leonatkinson.com>
     ** Creates three form fields for get month/day/year
     ** Input: Prefix to name of field, default date
     ** Output: HTML to define three date fields
     */
    
function DateSelector($inName$useDate=0)
    {
        
//create array so we can name months
        
$monthName = array(1=> "January",  "February",  "March",
            
"April",  "May",  "June",  "July",  "August",
            
"September",  "October",  "November",  "December");

        
//if date invalid or not supplied, use current time
        
if($useDate == 0)
        {
            
$useDate Time();
        }

        
/*
        ** make month selector
        */
        
print("<select name=" $inName .  "Month>n");
        for(
$currentMonth 1$currentMonth <= 12$currentMonth++)
        {
            print(
"<option value="");
            print(intval(
$currentMonth));
            print("""
);
            if(
intval(date"m"$useDate))==$currentMonth)
            {
                print(
" selected");
            }
            print(
">" $monthName[$currentMonth] .  "n");
        }
        print(
"</select>");


        
/*
        ** make day selector
        */
        
print("<select name=" $inName .  "Day>n");
        for(
$currentDay=1$currentDay <= 31$currentDay++)
        {
            print(
"<option value="$currentDay"");
            if(
intval(date"d"$useDate))==$currentDay)
            {
                print(
" selected");
            }
            print(
">$currentDayn");
        }
        print(
"</select>");


        
/*
        ** make year selector
        */
        
print("<select name=" $inName .  "Year>n");
        
$startYear date"Y"$useDate);
        for(
$currentYear $startYear 5$currentYear <= $startYear+5;$currentYear++)
        {
            print(
"<option value="$currentYear"");
            if(
date"Y"$useDate)==$currentYear)
            {
                print(
" selected");
            }
            print(
">$currentYearn");
        }
        print(
"</select>");
    }
?>

<html>
<head>
<title>DateSelector</title>
</head>
<body>
<form>
Choose a Date:  <?php DateSelector("Sample"); ?>
</form>
</body>
</html>


Usage Example




Rate This Script





Search



This Category All Categories