从日期数组中创建 <select> 选项并选择今天的日期或下一个未来日期

问题描述 投票:0回答:1

为什么这个不起作用,它应该标记今天的日期或今天之后最近的日期。例如,如果日期是

16-01-30
,则选择的日期将为
16-02-14

$date = ['16-01-14', '16-01-28', '16-02-14', '16-02-28'];
$currentdate = date('y-m-d');
echo $currentdate;
?>
<form>
    <select>
        <?php
        foreach ($date as $i => $d) {
            if ($currentdate >= $d && ($i == count($date)-1 || $currentdate < $date[$i+1])) {
                $selected = "selected";
            } else {
                $selected = "";
            }
            list($year, $month, $day) = explode('-', $d);
            echo "<option $selected>$month/$day/$year</option>";
        }
        ?>
    </select>
</form>
php html loops drop-down-menu selected
1个回答
1
投票

这就是你的答案

$dates = array('16-01-14','16-01-28','16-02-14','16-02-28');
    $currentdate = date('y-m-d');
    echo $currentdate;
    ?>
    <form>
    <select>
    <?php
    foreach ($dates as $i => $d) {
        if ($currentdate >= $d && ($i == count($dates)-1 || $currentdate < $dates[$i+1])) {
            $selected = "selected";
        } else {
            $selected = "";
        }
        list($year, $month, $day) = explode('-', $d);
        echo "<option $selected>$month/$day/$year</option>";
    }
© www.soinside.com 2019 - 2024. All rights reserved.