douti0467 2017-05-12 17:11
浏览 31

在php中比较两个日期的问题

For some reason I am having difficulties comparing a previous date and current date. I have tried many different things, and tried to google my way to an answer but with no luck.

This is how my code is..

$phpdate = date("Y-m-d");

$sql = "SELECT lastDailyCollect FROM users WHERE steamid='".$_POST['steamid']."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $lastDailyCollect = $row['lastDailyCollect'];
    }
} 

if ($lastDailyCollect == $phpdate) {
//give user error message
}elseif ($lastDailyCollect != $phpdate) {
//let user know it suceeded
}else {
//comparison error
}

I want to check if the user is able to collect a daily bonus. The last collection date of each user is stored in a mysql database, in a table called users. It always goes to the comparison error.

Hope somebody can help.

  • 写回答

1条回答 默认 最新

  • dongtuo5611 2017-05-12 17:59
    关注

    What you can do to check if lastDailyCollect date was previous day by subtracting one day from the current date and storing it in $yesterday then matching if previous date is equal to lastDailyCollect date.

    <?php
    
    $date = date("Y-m-d");  //2017-05-12
    
    $lastDailyCollect = "2017-05-11";
    
    $yesterday = date('Y-m-d',strtotime($date . "-1 days")); //2017-05-11
    
    if($lastDailyCollect == $yesterday) {
    //give user error message
        echo 'lastDailyCollect is equal to Previous day';
    }
    else
    {
    //let user know it suceeded
    echo 'lastDailyCollect is not equal to previous day';
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题