PHP日期和时间选择框

I am populating three select boxes to give me 3 select boxes, containing day, month, time. I am using this current code to populate each option:

<select name="<?php echo $select2; ?>">
<?php 
$months = array(Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
foreach ($months as &$value)
{
echo '<option value="'.$value.'">'.$value.'</option>';
}
?>
</select>

I am then converting the selected results into a single variable. which is stored in a DB as a DD-MM-YY format. How can I pull this info out of the database, and make the select boxes default to what ever has been pulled out from the DB?

Thanks guys

Andy Jones

You can explode the DD-MM-YY string using the explode method. For example:

$pieces = explode("-", $date);

This should give you an array with 3 elements as follows:

Array(
  0 => "DD", // Days
  1 => "MM", // Months
  2 => "YY"  // Years
)

Now maybe go ahead and loop through the dates as you had been. If they match, put a checked attribute for that option.

foreach ($months as $value)
{
  if($value == $pieces[1]) // $pieces[1] is the months element as you'll notice above
    echo '<option value="'.$value.'" checked>'.$value.'</option>';
  else
    echo '<option value="'.$value.'" checked>'.$value.'</option>';
}

Your question was kind of vague, but something like this could work.

<select name="<?php echo $select2; ?>">
<?php 
$months = array(Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
foreach ($months as $i => $value){
    if($i+1==abs($db_value)){
        echo '<option selected="selected" value="'.$value.'">'.$value.'</option>';
    }else{
        echo '<option value="'.$value.'">'.$value.'</option>';
    }
}
?>
</select>