PHP - 将日期与当前日期进行比较 [英] PHP - Compare Date to current

查看:146
本文介绍了PHP - 将日期与当前日期进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有以下内容

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!

推荐答案

条件错误,应该有两个等于符号 ==

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')

上面的代码将 date('jn-Y')的值赋给 $ row [ '

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

您的作业不会执行任何操作,只需返回 true 每次它会进入if条件块

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

其中两个等于符号 == 用于压缩。所以在你的条件double等于,代码将只进入if条件块当你的 $ row ['posted'] 等于 date('jn-Y')

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')

注意 mysql _ * 函数正式被弃用(从PHP 5.5开始,它很可能在下一个主要版本中被删除。)

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

你需要看看这个< a href =http://stackoverflow.com/q/12859942/829533>为什么我不应该在PHP中使用mysql_ *函数?

这篇关于PHP - 将日期与当前日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆