问题
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