Jump to content

Date input


collin84

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
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.