簡體   English   中英

如果事件日期小於現在,則顯示foo。 如果活動日期大於現在,則顯示欄

[英]If event date is less than now, show foo. If event date is greater than now, show bar

我的網站上有一個事件頁面,並且在標題的頂部運行一個事件欄,以顯示最新事件。 我試圖弄清楚如何正確編寫此代碼。

如果事件尚未發生,我想顯示它。 如果事件已經發生,我不想顯示該事件。

碼:

<?php //SELECT QUERY...
    foreach($events->results() as $e):
$now = "now()";
    if($e->event_date <= $now){?>   
        <h1>Show Event</h1>

<?php } 
    elseif($e->event_date > $now){ ?>
        <h1>DON'T Show Event</h1>
<?php
}
    endforeach;

因此,基本上,如果事件的時間戳小於或等於即將發生的事件,我希望它顯示出來。

如果事件的時間戳大於事件的當前日期和時間,則我不希望其顯示。

事件數據庫表。 在此處輸入圖片說明

now()是mysql函數。 在PHP中,我們使用time()來獲取當前時間戳

用作

$now = time();// get current timestamp

使用strtotime()在時間戳中轉換日期,然后用於比較

$now = time();
foreach ($events->results() as $e):
    if (strtotime($e->event_date) >= $now) {// check for greater then equal to
        ?>   
        <h1>Show Event</h1>
        <?php
    } else {
        echo "<h1>DON'T Show Event</h1>";
    }
endforeach;

您的方向正確,請像這樣更改

<?php //SELECT QUERY...
    foreach($events->results() as $e):
       $now = date("Y-m-d H:i:s");
       if(date("Y-m-d H:i:s", strtotime($e->event_date)) >= $now){   
          echo "<h1>Show Event</h1>";
       }else{
          echo "<h1>DON'T Show Event</h1>";
       }
    endforeach;
  ?>

暫無
暫無

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

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