draj840143 2016-12-23 22:40 采纳率: 100%
浏览 7
已采纳

通过php比较日期[复制]

This question already has an answer here:

I want to compare two dates, the first is my current date and the second is a date from database.

When I see both of them they looks identical, but PHP gives me another result. I tried to compare using functions like strcmp. I simplified my code so you can understand it easier.

<?php
$users_day = "20".date("y-m-d");
$looper = 0 ;
$all_users_selection = mysql_query("SELECT * FROM user");
while($user_rows = mysql_fetch_array($all_users_selection) )
{
    if($user_rows['user_lastseen'] != NULL)
    {
        //Getting user lastseen day and hour
        $db_user_date[$looper] = substr(($user_rows['user_lastseen']) ,0 , 10);
        $db_user_curr[$looper] = $user_rows['username'];
        //Comparing day then hour
        if (strcmp($users_day,$db_user_date[$looper]) != 0)
        {
            //Set offline
            $current_user_update = mysql_query("UPDATE user SET user_status = 'offline' WHERE username = '$db_user_curr[$looper]'");
        }
        $looper++;
  }
  ?>

When I try to test variables, for example echo $users_day;, it gives my the 2016-12-24 (my current date) .

Also when I test the dates from database it gives me a date with the same type for example I added:

echo $db_user_date[$looper]; 

In my while loop and in some case the date was identical 2016-12-24 when i tested echo strcmp($users_day,$db_user_date[$looper]); it gives me -10 which means they are 100% different but they are not!

Any idea ?

</div>
  • 写回答

2条回答 默认 最新

  • dse84825 2016-12-23 22:47
    关注

    Convert your dates to time, which is a numeric value, then compare them.

    $current = strtotime($users_day)
    $db_date = strtotime($db_user_date[$looper])
    
    if($current > $db_date)
    {
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行