【问题标题】:line by line date dropdown select menu逐行日期下拉选择菜单
【发布时间】:2015-10-26 04:00:31
【问题描述】:

我正在尝试为我的 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 日,星期二

等等。

我尝试了几个小时:(

【问题讨论】:

    标签: php css wordpress


    【解决方案1】:

    我认为您需要添加日期作为选择框的选项。为此,您可以使用以下内容:

    <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);
    }
    

    【讨论】:

    • 我尝试将其添加到我网站的 functions.php 中,但出现错误。我做错了什么?
    • 我收到错误“解析错误:语法错误,第 13 行 /home/dilmarc2/public_html/philmart/wp-content/themes/shopkeeper-child/functions.php 中的意外 '
    • 我完整的functions.php文件是这样的:
    猜你喜欢
    • 1970-01-01
    • 2023-03-02
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    相关资源
    最近更新 更多