PHP - Compare Date to current [closed]

余生颓废 提交于 2019-12-11 01:59:32

问题


I have following in the database

$checktime = mysql_query("SELECT * FROM drafts WHERE page='sched'");
while($row = mysql_fetch_array($check)){
    if($row['posted'] = date('j-n-Y'))
    {
        echo "ok";
    }
}

I'd like to compare this date against today's date. Please help me if everyone knows. thank you!


回答1:


Condition is wrong, it should have two equals to signs ==

if($row['posted'] == date('j-n-Y'))

Please Note that one equal to sign is for assignment, it means that

$row['posted'] = date('j-n-Y')

The above code will assign the value of date('j-n-Y') into $row['posted']

And your assignment will do nothing but just return true, so every time it will go into the if condition block

where as the two equals to signs == are use for compression. so in your condition with double equals to, the code will only go into the if condition block when your $row['posted'] is equal to date('j-n-Y')

NOTE: mysql_* functions are Officially deprecated (as of PHP 5.5. It's likely to be removed in the next major release.)

you need to have a look at this Why shouldn't I use mysql_* functions in PHP?



来源:https://stackoverflow.com/questions/19190939/php-compare-date-to-current

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!