duandian2725 2019-07-31 12:30
浏览 53
已采纳

如何使用PHP If语句与SQL日期和当前日期进行比较? [关闭]

In my SQL Database i have a date column. I want to use a PHP if statement to show some text if the date from the DB equals todays date. Aswell as that, use an else to show something else if the date does not equal today.

Running PHP 7

I have lost count of what i have tried with this but don't seem to get anywhere.

I am fetching the data here:

    $sql1 = 'SELECT `date` FROM `operations`.`opsroom` ORDER BY `opsroom`.`date` ASC, `opsroom`.`starttime` ASC LIMIT 1';
    $nextsession = mysqli_query($conn, $sql1);

Later on in the file is where i am using this:

    <?php
    while ($row = mysqli_fetch_assoc($nextsession)) 
    {
    if( $row['date'] == DATE(date)){
    echo "BOOKINGS TODAY";
    } else {
    echo "No Bookings";
    }
    }
    ?>

Only error i get at the moment is PHP Warning: Use of undefined constant date - assumed 'date' (this will throw an Error in a future version of PHP)

  • 写回答

3条回答 默认 最新

  • dqxmf02844 2019-07-31 12:38
    关注

    Use proper date function of PHP:

    If you want to compare today date then try below:

    <?php
    while ($row = mysqli_fetch_assoc($nextsession)) 
    {
     // this one you need to change
     if( $row['date'] == date('Y-m-d')){
        echo "BOOKINGS TODAY";
     } else {
        echo "No Bookings";
     }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分