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
https://forums.phpfreaks.com/topic/7507-date-input/
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
https://forums.phpfreaks.com/topic/7507-date-input/#findComment-27342
Share on other sites

Archived

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

×
×
  • 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.