Month, Day, Year Dropdown Boxes

Description: Creates three SELECT form fields Month Day and Year.

<?PHP
 
    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 */
        ECHO "<select NAME=" . $inName . "Month>\n";
        FOR($currentMonth = 1; $currentMonth <= 12; $currentMonth++)
        {
            ECHO "<option VALUE=\"";
            ECHO INTVAL($currentMonth);
            ECHO "\"";
            IF(INTVAL(DATE( "m", $useDate))==$currentMonth)
            {
                ECHO " SELECTED";
            }
            ECHO ">" . $monthName[$currentMonth] . "\n";
        }
        ECHO "</select>";
 
        /* make day selector */
        ECHO "<select NAME=" . $inName . "Day>\n";
        FOR($currentDay=1; $currentDay <= 31; $currentDay++)
        {
            ECHO "<option VALUE=\"$currentDay\"";
            IF(INTVAL(DATE( "d", $useDate))==$currentDay)
            {
                ECHO " SELECTED";
            }
            ECHO ">$currentDay\n";
        }
        ECHO "</select>";
 
        /* make year selector */
        ECHO "<select NAME=" . $inName . "Year>\n";
        $startYear = DATE( "Y", $useDate);
        FOR($currentYear = $startYear - 5; $currentYear <= $startYear+5;$currentYear++)
        {
            ECHO "<option VALUE=\"$currentYear\"";
            IF(DATE( "Y", $useDate)==$currentYear)
            {
                ECHO " SELECTED";
            }
            ECHO ">$currentYear\n";
        }
        ECHO "</select>";
 
    }
?>
 
<html>
<body>
<form>
Choose a Date: &LT;?PHP DateSelector( "Sample"); ?>
</form>
</body>
</html>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.