Jump to content

Archived

This topic is now archived and is closed to further replies.

collin84

Date input

Recommended Posts

Hi

I'm new to php and have just started using it for an assignment at university

I'm wanting to create a date input section on a website using 3 combo boxes for day, month and year

I then want to store this date in a mysql table

$month = array(1=>"January", "February", "March"........);

$day = $_POST['day'];
$year = $_POST['year'];

$query = "INSERT INTO table (date) VALUES ('$year-???-$day')";

The only question i have got is how to get the numerical value of the selected month and put it in place of "???" ?

Thanks
Col

Share this post


Link to post
Share on other sites
Display month names in the combo but use month numbers for the values

[code]<?php
if (isset ($_POST['submit'])) {
    $day = $_POST['day'];
    $month = $_POST['month'];
    $year = $_POST['year'];

    $date = "$year-$month-$day";

    $query = "INSERT INTO table (date) VALUES ('$date')";

    echo $query;
}
?>

<FORM method='POST'>
Day
<SELECT name='day'>
        <?php
        for ($d=1; $d <= 31; $d++) {
             printf ("<OPTION value='%1\$02d'>%1\$d</OPTION>", $d);
        }
        ?>
</SELECT>

Month
<SELECT name='month'>
        <?php
        for ($m=1; $m <= 12; $m++) {
             $mname = date('F', mktime(0,0,0,$m,1,2006));
             printf ("<OPTION value='%02d'>%s</OPTION>", $m, $mname);
        }
        ?>
</SELECT>

Year
<SELECT name='year'>
        <?php
        for ($y=2001; $y <= 2006; $y++) {
             echo "<OPTION value='$y'>$y</OPTION>\n";
        }
        ?>
</SELECT>

<INPUT TYPE='SUBMIT'  name='submit' value='Submit'>
</FORM>[/code]

Share this post


Link to post
Share on other sites
Thanks for the help, its working fine now. Thought it must have been something simple but couldn't figure it out and was spending far too long trying.

Col

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.