CoderZone.org

Category: >> PHP Code >> Automatically Create Date Select Boxes Bookmark and Share

<< lastnext >>

Snippet Name: Automatically Create Date Select Boxes

Description: This function builds dropdown boxes with the month, day, and year. It defaults to a 'mmddyyyy' format, the current date, or specified date depending on the settings.

Also see:
» Calculate days between any two dat...
» Add a hidden field to a form dynam...
» Add more fields to a form dynamica...
» Validate file extensions in upload...
» Easy date manipulation
» Date Picker Form in ASP
» Getting Values Submitted From A Fo...
» Format money as millions, billions...
» Convert UK Dates To mySQL Format D...
» Calculate the difference between t...
» DHTML Combo Box Dropdown
» Delete files older than 'X'
» RMAN: Format Directives
» USER - connection information
» USER - User Information 2
» USER Information
» SQL*Plus formatting
» Create string by formatting an amo...
» List security related profile info...
» Currency display formatting functi...
» Nicely Format File Size
» String to Date
» Month, Day, Year dropdown boxes
» Select (dropdown) list generator
» Calculate date of Easter Sunday
» GET and POST variables in hidden f...
» Define a schedule of holidays
» Calendar for any month any year
» Add and Subtract dates
» Basic PHP Calendar

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

<?php
 
function buildDate($name, $m, $d, $y) {
        $date = date("m-d-Y",strtotime("now"));
        $date_array = explode("-",$date);
        $now_m = "";
        $now_d = "";
        $now_y = "";
        $month = '<select class="small" name="'.$name.'_month">';
        $month .= '<option value="" selected disabled>mm</option>';
        for ($i=1;$i<=12;$i++) {
                if ( $m != "" ) {
                        if ( $m == $i ) {
                                $now_m = "selected";
                        }
                }elseif ( $m == "0" ) {
                        $now_m = "";
                }else{
                        if ( $date_array[0] == $i ) {
                                $now_m = "selected";
                        }
                }
                $month .= '<option value="'.$i.'" '.$now_m.'>'.$i.'</option>';
                $now_m = "";
        }//END month for loop
        $month .= '</select>';
        $day = '<select class="small" name="'.$name.'_day">';
        $day .= '<option value="" selected disabled>dd</option>';
        for ($i=1;$i<=31;$i++) {
                if ( $d != "" ) {
                        if ( $d == $i ) {
                                $now_d = "selected";
                        }
                }elseif ( $d == "0" ) {
                        $now_d = "";
                }elseif ( $d == $i ) {
                        $now_d = "selected";
                }else{
                        if ( $date_array[1] == $i ) {
                                $now_d = "selected";
                        }
                }
                $day .= '<option value="'.$i.'" '.$now_d.'>'.$i.'</option>';
                $now_d = "";
        }//END day for loop
        $day .= '</select>';
        $year = '<select class="small" name="'.$name.'_year">';
        $year .= '<option value="" selected disabled>yyyy</option>';
        for ($i=gmDate("Y"); $i <=substr(get330Date(),0,4); $i++) {
                if ( $y != "" ) {
                        if ( $y == $i ) {
                                $now_y = "selected";
                        }
                }elseif ( $y == "0" ) {
                        $now_y = "";
                }elseif ( $y == $i ) {
                        $now_y = "selected";
                }else{
                        if ( $date_array[2] == $i ) {
                                $now_y = "selected";
                        }
                }
                $year .= '<option value="'.$i.'" '.$now_y.'>'.$i.'</option>';
                $now_y = "";
        }//END year for loop
        $year .= '</select>';
        //-------------------------------------------
        echo $month." ".$day." ".$year;
}//END buildDate function
// ------------------------------------
function get330Date() {
        return date("Y-m-d", mktime(0, 0, 0, substr(gmDate("Y m d"),5,2), 
        substr(gmDate("Y m d"),8,2) + 330, 
        substr(gmDate("Y m d"),0,4)))."<br />";
}
 
?>
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 11