如何回显所选日期

时间:2016-02-14 01:13:25

标签: php

有没有人知道如何将selected日期作为文本回显,日期月份和年份在表单之外分隔?我尝试在表单之外回显$date $month$year但是这并没有给出正确的日期而不是帮助

<?
$date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28','16-04-14','16-04-28',
'16-05-14','16-05-28','16-06-14','16-06-28','16-07-14','16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28',
'16-11-14','16-11-28','16-12-14','16-12-28');
    $currentdate = date('y-m-d');
    echo $currentdate;
    ?>
    <form>
    <select style="width:200px;">
    <?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>" . date("m/d/Y", strtotime($d)) . "</option>";
        echo 'the current billing period is';

    }
?>
</select>
</form>

2 个答案:

答案 0 :(得分:1)

使用strtotime代替列表。

....
// list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
....

编辑:附加信息 - 您的代码需要进行大量修改,并且可能需要进行一些结构更改,但假设这是用于测试方法和“如何做”而不是最终产品。

您需要提交所选日期,在脚本中捕获它并使用所选日期来执行您需要的操作 - 即从数据库中检索数据 - 这应该会让您有所了解。

<?php
    // You need to create these dates by using another method. You cannot hard code these. You can create it with date functions easily.
    $date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28','16-04-14','16-04-28','16-05-14','16-05-28','16-06-14','16-06-28','16-07-14','16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28','16-11-14','16-11-28','16-12-14','16-12-28');

// Checking if we have a posted form, with the button name user clicked 
if (isset($_POST["btnSubmit"])) {
    // This is your selected day - use it  where you need:
    $selectedDate = $_POST["selectedDate"];

    // This is where your model start singing and gets necessary info for this date - just printing here as sample
    print $selectedDate;

    // I need dropDownDate to compare in the SELECT to preselect the appropriate date
    $dropDownDate = strtotime($selectedDate);

} else {
    // First time visit, preselect the nearest date by using current date
    $dropDownDate = time();
}

?>
<form method="post">
<select name="selectedDate" style="width:200px;">
<?php
foreach ($date as $i => $d) {
    if ($dropDownDate >= strtotime($d) && 
            (!isset($date[$i+1]) || ($dropDownDate < strtotime($date[$i+1])))
        ) {
        $selected = 'selected="selected"';
    } else {
        $selected = "";
    }

    list($year, $month, $day) = explode('-', $d);
    echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
}
?>
</select>
<input type="submit" name="btnSubmit" value="Submit">
</form>

请注意,我添加了“提交”类型输入(提交表单)并将表单方法更改为“post”,最后将SELECT命名为“selectedDate”。我还在循环中更改了日期比较代码行。

希望这有帮助。

答案 1 :(得分:1)

在循环内部添加$selected_int变量,如下所示:

foreach ($date as $i => $d) {
    if ($currentdate >= $d && ($i == count($date)-1 || $currentdate < $date[$i+1])) {
        $selected = "selected";
        $selected_int = $i;
    } else {
        $selected = "";
    }
    list($year, $month, $day) = explode('-', $d);
    echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
    echo 'the current billing period is';

}

然后,你可以像:

那样引用它
echo date('Y-m-d', strtotime($date[$selected_int]));

<强>加成

我知道你已经接受了答案,但我现在也想提出一个建议,即我看到你使用的是$date。既然您知道开始日期,并且它是在14天的时间段内,那么将其作为循环的一部分进行编写会很容易。

$start_date = date('Y-m-d', strtotime(date('Y').'-01-01'); //First day of the year, for the sake of argument.

$interval = 14;

for ($i = 0; date('Y') == date('Y', strtotime($start_date.' +'.($i * $interval).' days')); $i++) {//While this year is equal to the start date's year with the added interval [If I knew what your logic here was I could offer a better suggestion]
    if ($currentdate >= date("Y-m-d", strtotime($start_date.' +'.($i * $interval).' days')) && (date('Y') < date("Y", strtotime($start_date.' +'.(($i + 1) * $interval).' days')) || $currentdate < date("m/d/Y", strtotime($start_date.' +'.(($i + 1) * $interval).' days')))) {
        $selected = "selected";
        $selected_int = $i;
    } else {
        $selected = "";
    }
    echo "<option $selected>" . date("m/d/Y", strtotime($start_date.' +'.($i * $interval).' days')) . "</option>";
}

基本上,这需要开始日期,将其显示为第一个日期选项,然后在每次传递时添加14天。您的if/else声明应该仍然相同。它会检查您是否在一年的最后一个时间间隔内,或当前日期是否小于下一个时间间隔,以及当前日期是否大于当前时间间隔。

循环后,您可以通过以下方式获取日期:

echo date("m/d/Y", strtotime($start_date.' +'.($selected_int * $interval).' days'));

我知道它看起来很多,但它可以让你不必开始制作日期数组。