date selection boxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

date selection boxes

Post by hame22 »

Hi I have 3 list menus where a user can select the day month and year.

What I want to be able to do is make the list menus default to todays date automatically.

Is there a script out there that can allow this?

Thanks in advance
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

When writing out the options, compare the value to today's values.

:idea:
visitor-Q
Forum Commoner
Posts: 72
Joined: Mon Feb 05, 2007 1:40 am

Post by visitor-Q »

something like this:

Code: Select all

$today = date("m"."d"."y");
        $today = explode($today);

        (($today[0] >= 1 && $today[0] <= 9) ? ($today[0] = "0". $today[0]) : (""));
        (($today[1] >= 1 && $today[1] <= 9) ? ($today[1] = "0". $today[1]) : (""));
        (($today[2] >= 1 && $today[2] <= 9) ? ($today[2] = "0". $today[2]) : (""));
        $thisMonth = $today[0];
        $thisDay = $today[1];
        $thisyear = $today[2];

        $month_array = array("01" => "January", "02" => "February", "03" => "March",
                             "04" => "April", "05" => "May", "06" => "June",
                             "07" => "July", "08" => "August", "09" => "September",
                             "10" => "October", "11" => "November", "12" => "December");

        echo "<SELECT NAME=\"event_month\">\n".
             "<OPTION VALUE=\"\">select a month\n";

        foreach($month_array as $key => $val){
                echo "<OPTION VALUE=\"". $key . (($key == $thisMonth) ? (" SELECTED") : ("")) ."\">". $val ."\n";
        }
        echo "</SELECT><br />\n";


        $days = range(1, 31);
        echo "<SELECT NAME=\"event_day\">\n".
             "<OPTION VALUE=\"\">select a day\n";

        foreach($days as $day){
                if($day >= 1 && $day <= 9){
                        $day = "0". $day;
                }
                echo "<OPTION VALUE=\"". $day . (($day == $thisDay) ? (" SELECTED") : ("")) ."\">". $day ."\n";
        }
        echo "</SELECT><br />\n";


        $years = range(2005, 2010);
        echo "<SELECT NAME=\"event_year\">\n".
             "<OPTION VALUE=\"\">select a year\n";

        foreach($years as $year){
                echo "<OPTION VALUE=\"". $year . (($year == $thisYear) ? (" SELECTED") : ("")) ."\">". $year ."\n";
        }
        echo "</SELECT>";
untested.
Post Reply