2

I need to create something to display an image based on the current date and time. I have an SQL table set up that looks like this:

eventid | startdate  | enddate    | imgfile    | status
6       | 2013-04-29 | 2013-05-03 | finals.jpg | active

What I need to do is compare the start date and end date to the current date, and if the current date falls within the startdate and enddate, then display the imgfile.

I'm not sure how to go about this, but this is what I tried.

//get current date and time
$currentdatetime = strtotime(now);
$formatteddatetime = date("y-m-d", $currentdatetime);

while($row=mysql_fetch_array($getimage))
{
    //get variable for startdate
    $formattedstartdate = date("y-m-d", $row[startdate]);
    $formattedenddate = date("y-m-d", $row[enddate]);
    if($formatteddatetime >= $formattedstartdate AND $formatteddatetime <= $formattedenddate AND $row[status] == 'active')
    {
        echo $row[imgfile];
    }
}
?>

Right now, it doesn't show anything inside of the entire while statement. Am I at least on the right track?

4

2 回答 2

3

CURRENT_DATE 以 'YYYY-MM-DD' 格式或 YYYYMMDD 格式返回当前日期,具体取决于函数中使用的是数字还是字符串。CURDATE() 和 CURRENT_DATE() 是 CURRENT_DATE 的同义词。

于 2017-04-23T06:11:05.217 回答
2

您应该在 SQL 查询而不是 PHP 中执行此操作。例如:

SELECT * FROM table WHERE CURRENT_DATE BETWEEN startdate AND enddate;

CURRENT_DATE()顾名思义,是一个返回当前日期的 MySQL 函数。它是少数具有不带括号的别名的特殊函数之一。

于 2013-04-29T19:26:01.410 回答