检查数据库中的日期是现在还是更早
I have a php script that looks like this (shortened/edited for clarity of course):
$result = mysqli_query($link, 'SELECT date FROM table');
while ($row = mysqli_fetch_assoc($result)) {
if ($row["date"] < time())
{
do some stuff
}
else
do something else
The date is stored in a DATE field in MySQL. I have no interest in storing time and only the date is stored in that field in XXXX-XX-XX format.
How can I check if the retrieved date is today or earlier? By using time() there, which was the last thing I ended up trying, my script is acting as if all retrieved dates are today or earlier, even when they are not.
Thank you for any help with this.
我有一个看起来像这样的PHP脚本(当然为了清晰起见缩短/编辑): p> \ n
$ result = mysqli_query($ link,'SELECT date FROM table');
while($ row = mysqli_fetch_assoc($ result)){
if($ row [“date”]&lt; time())
{
do some stuff
}
else
do something else
code> pre>
日期存储在MySQL的DATE字段中。 我没有兴趣存储时间,只有日期以XXXX-XX-XX格式存储在该字段中。 p>
如何检查检索的日期是今天还是更早? 通过使用time(),这是我最后尝试的最后一件事,我的脚本就好像所有检索的日期都是今天或更早,即使它们不是。 p>
谢谢 对此有任何帮助。 p>
div>
You should perform the comparison in the SQL query.
Have a look at: mysql date comparison with date_format
You can check if date is today (or earlier) in the SQL statement:
SELECT `date`, `date` <= CURDATE() AS isTodayOrEarlier
FROM table
or
SELECT `date`, IF(`date` <= CURDATE(), 1, 0) AS isTodayOrEarlier
FROM table