逐行日期下拉选择菜单


line by line date dropdown select menu

我想为我的wordpress网站做一个选择下拉框。当我点击,它将有7个选项的日期开始从第二天到7天从现在。

我已经完成了这个代码,以获得当前日期和未来的日子。

<?phpforeach( range(0,6) as $cnt ){
echo strtoupper( date('D d M Y',strtotime( "today + $cnt day") ) ) . PHP_EOL;
}

$days = new DatePeriod(new DateTime, new DateInterval('P1D'), 7, DatePeriod::EXCLUDE_START_DATE);
foreach ($days as $day) {
echo strtoupper($day->format('D d M Y')) . PHP_EOL;
}

如何创建一个选择菜单。当我点击它时,选项如下:

2015年10月26日星期一

2015年10月27日星期二

等等。

我试了几个小时

我认为您需要添加日期作为选择框的选项。你可以这样写:

<select name="select-box-name" id="give-an-id-of-your-choice">
    <option value="">Select a date</option> <!-- this will be the default selected option if nothing is set -->
    <?php foreach( range(0,6) as $cnt ){
        echo "<option value='".strtoupper( date('D d M Y',strtotime( "today + $cnt day") ) )."'>".strtoupper( date('D d M Y',strtotime( "today + $cnt day") ) )."</option>";
    }
    ?>
</select>

如果您确实希望有两种不同格式的日期,即输入时间戳和显示星期一21 Oct 15,您可以更改代码如下:

<select name="select-box-name" id="give-an-id-of-your-choice">
<option value="">Select a date</option> <!-- this will be the default selected option if nothing is set -->
<?php foreach( range(0,6) as $cnt ){
    $date = strtotime( "today + $cnt day");
    echo "<option value='".$date."'>".format_date_option($date)."</option>";
}
?>

<?php
function format_date_option($input_date_ts){
    return date("D d M y",$input_date_ts);
}