dongying9712 2014-05-30 15:57
浏览 30
已采纳

这是一个有效的PHP日期相等检查吗? [关闭]

Is this a valid PHP date equality check?

$zeroDate = date("0000-00-00 00:00:00");

if( date($someOtherDateString)==$zeroDate ) {
// . . . 
}
  • 写回答

2条回答 默认 最新

  • dongshi7433 2014-05-30 16:06
    关注

    I'd be more comfortable testing true date/time values than strings that just look like date times.

    I have two basic approaches for testing date quality. The first one is: are the variables equal (like the track you're on above, but not string tests). The second one is: is there a non-zero delta between the dates?

    Try this:

    // convert strings to time
    $zeroDate = strtotime("0000-00-00 00:00:00");
    $someOtherDate = strtotime("0000-00-00T00:00:00-00:00");
    
    // sanity check on the data
    var_dump($zeroDate);
    var_dump($someOtherDate);
    
    // Approach #1
    if ($someOtherDate === $zeroDate) {
        print "They are equal
    ";
    } else {
        print "They are not equal
    ";
    }
    
    // Approach #2
    if (($someOtherDate - $zeroDate) === 0) {
        print "They are still equal
    ";
    } else {
        print "There are not equal
    ";
    }
    

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址