簡體   English   中英

如何回顯所選日期

[英]How can I echo out the selected date

有誰知道我該如何將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>

使用strtotime代替list。

....
// 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>

請注意,我添加了“提交”類型輸入(以提交表單),並將表單方法更改為“發布”,最后將SELECT命名為“ selectedDate”。 我還在循環中更改了您的日期比較代碼行。

希望這可以幫助。

在循環內部添加$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'));

我知道這看起來很多,但是這樣可以避免您必須制作日期數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM