日期foreach数组

时间:2016-02-12 05:54:30

标签: php

为什么这不起作用,它假设从当前日期之间的排列日期中选择。例如,如果日期为16-01-30,则所选日期为16-02-14谢谢您帮助我。

    <?
    $date = array('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>

1 个答案:

答案 0 :(得分: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>";
    }