-1

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
badsyntax
  • 311
  • 4
  • 14
  • Cannot reproduce ~ https://eval.in/949309. How are you verifying the result? – Phil Feb 05 '18 at 03:14
  • Instead of returning true false I echo true false. – badsyntax Feb 05 '18 at 03:26
  • What **exactly** do you mean by that? If the code in your question differs to your actual code, please update it. It would be very helpful if you could show a code example of how you're verifying the issue – Phil Feb 05 '18 at 03:29
  • Can you now add the full output from your script. I don't see any `true` or `false` in what you've posted but there should be at least one or the other – Phil Feb 05 '18 at 03:34
  • updated the code. so when the function runs it echos false for every user even when it shouldnt. – badsyntax Feb 05 '18 at 03:35
  • Still not seeing that output with the "true" or "false". It should be right after the `var_dump` output – Phil Feb 05 '18 at 03:40
  • Am I missing something? It is right after the var dump in an if statement. – badsyntax Feb 05 '18 at 03:42
  • Still cannot reproduce ~ https://eval.in/949317. Yes, you are missing something in your question. The part from the output that says "true" or "false". You are currently **only** showing the two `var_dump` results. Where is the rest? See in my `eval.in` links how the third line of each set is either "true" or "false", what does yours say? You aren't showing it at all – Phil Feb 05 '18 at 03:45
  • You dont see the if, else statement after the var dumps? Says if echo false else echo true? – badsyntax Feb 05 '18 at 03:46
  • I CAN SEE YOUR CODE. WHAT DOES YOUR OUTPUT LOOK LIKE? – Phil Feb 05 '18 at 03:47
  • The output is a page that says false for each user in the db including my own which it shouldnt based on the code above. – badsyntax Feb 05 '18 at 03:48
  • OMG, in your question you have two snippets that are the results of your `var_dump` calls showing two date strings and two timestamps. Your code should be outputting "true" or "false" after that yet you have not shown that in your question **AT ALL**. Run your code then copy / paste the **exact output** into your question. There should be **THREE** lines of output per call to `checkOnline` – Phil Feb 05 '18 at 03:51
  • 1
    Voted to close this question as Off-topic because this question was caused by a problem that can no longer be reproduced or a simple typographical error. – Aniket Sahrawat Feb 05 '18 at 03:52
  • 1
    added the exact output as described – badsyntax Feb 05 '18 at 04:01
  • Thank you. Unfortunately this doesn't shed any more light on the problem. The calculation in your code should be exactly the same as in the eval.in demos. Unless your actual code is somehow different to the code in your question, I cannot see how the result is possible. – Phil Feb 05 '18 at 04:06
  • The code is exactly the same as it appears in my script. Could my timezone or the way the record is saved in the db have an effect? – badsyntax Feb 05 '18 at 04:07

1 Answers1

1

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

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Youre right. Just wrote a gigantic function to check how much time had passed since datetime and it reported 12 hours ago. Thanks for sticking with me. – badsyntax Feb 05 '18 at 04:33