dsxsou8465 2018-02-05 02:56
浏览 27
已采纳

PHP检查1分钟是否过去总是返回false

I am having trouble checking time. I loop through users in my database using this function to check each one and if time has passed 1 minute. As I loop through the top user (me) has the same time as current time (1 minute has not passed). Other users are fake and their time is from earlier today (1 minute has def passed). However my function returns false every time.

public function checkOnline($username) 
{
    $user = $this->load->model('user')->getUser('username', $username);
    $last_active = strtotime($user['last_active']);

    var_dump(date('Y-m-d g:i:s', time()) .  ' ' . $user['last_active']);
    var_dump(time() .  ' ' . $last_active);

    if (time() - $last_active > 1 * 60) {
        echo 'false';
        return false;
    } else {
        echo 'true';
        return true;
    }
}

The var dumps return this for the first user (me) return this.

'2018-02-04 4:49:54 2018-02-04 4:49:54'
1517798994 1517755794

The var for other users return this.

'2018-02-04 4:49:54 2018-02-04 3:09:03'
'1517798994 1517749743'

Why is my function always returning false?

EDIT:

Here is the loop where I check

foreach ($users as $user) {
    if ($this->checkOnline($user['username']) === true) {
        $is_online = '<span class="text-green">Online</span>';
    } else {
        $is_online = '<span class="text-red">Offline</span>';
    }
}

Output:

C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04   5:53:57' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517759637' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
C:\path\UsersController.php:124:string '2018-02-04 5:53:57 2018-02-04 3:09:03' (length=37)
C:\path\UsersController.php:125:string '1517802837 1517749743' (length=21)
false
  • 写回答

1条回答 默认 最新

  • douzhong4222 2018-02-05 04:22
    关注

    Your issue stems from some bad reporting and / or date storage.

    Where you use

    date('Y-m-d g:i:s', time())
    

    The "g" refers to

    12-hour format of an hour without leading zeros

    Now, your current time of "2018-02-04 5:53:57" seems to be around 12 hours ahead of what you assume to be the same time from your DB. If I had to guess, it's actually 5:53pm on your server.

    When PHP parses the date string "2018-02-04 5:53:57" from your DB, it assumes 24 hour time and interprets it as 5:53am.

    I suggest you stick to ISO 8601 format if you must use strings (for example date('c')) or better would be to store the dates in your DB as proper date/time data types.

    The DB layer in your code should then ideally convert those to DateTime instances which are quite easy to use with diffs. See PHP find difference between two datetimes

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题