Cannot get difference between two timestamps

53
February 12, 2019, at 8:00 PM

I want return the number of dates which have a difference less than 30 minutes. These are the records stored in the database:

id | last_login
0    2019-02-11 17:22:47
1    0000-00-00 00:00:00
2    2019-02-11 17:22:03
3    0000-00-00 00:00:00
4    0000-00-00 00:00:00

the result of the query should be 2, but I get 1:

$time = strtotime('+30 minutes');
return $this->db->select('COUNT(last_login) AS users_online')
    ->from('login')
    ->where('UNIX_TIMESTAMP(last_login) < ' . $time)
    ->get()->num_rows();
Answer 1

There are two problems with your code here. The first, is you are cutting everything down to 1 record with an implicit group thanks to count() - and then getting the number of records returned via num_rows().

The second issue is you are getting every single record in the table where the last login is less than 30 minutes in the future... Which will always be every row.

echo date('H:i') . "\n" . date('H:i', strtotime('+30 minutes'));

Yields:

16:38
17:08
READ ALSO
Parse SOAP web service XML response

Parse SOAP web service XML response

I´m consuming a WS and it gives me data as XMLWhen the response returns to my PHP it came as Object and not as XML

44
How to extract in an array using Simple HTML Dom

How to extract in an array using Simple HTML Dom

I am trying to extract some values parsing html from a websiteWhere are many values with the same tag and when I try to extract them, I got back just "array" word

49
Best way to combine two tables in php [on hold]

Best way to combine two tables in php [on hold]

I'm working on a project where I want to be able to create a weekly calendar and display shifts for that week next to each employee's name

65
WP Acceptance and the composer.json file required

WP Acceptance and the composer.json file required

I am having trouble setting up WP Acceptance and would like some assistance pleaseIn the documentation it states that it is not required to use WP Local Docker so I have the basic required items installed already

69