MySql select if date is NOT more than 15 min ago - php

I am trying to make a login system that logs you out after 15 minutes of inactivity. Here is my current code for fetching the data:
$itemResult = mysql_query("SELECT * FROM `logins` WHERE token = '$token' AND logged_date xxxxx");
This is where I am stuck. The date is formatted like this: yy/mm/dd h:m
I need to select everything with the correct token that is no more than 15 minutes old. How do I do that?

Explanation:
Because you are using a varchar column type you need to convert them to the same type, either change your database to store the date as a date (typically best practice anyways) or use str_to_date() to convert your string to a date before comparing.
Then we use the NOW() function to get the current datetime, subtract an interval of 15 minutes from the value of NOW() and compare it to the logged_date column
Or you could use Date_format() to convert the now - 15min date to a string
date_format(DATE(NOW() - INTERVAL 15 MINUTE), '%y/%m/%d %h:%i') and then compare your varchar to that
Query:
SELECT *
FROM `logins`
WHERE token = '$token'
AND
str_to_date(logged_date, '%y/%m/%d %h:%i') >= DATE(NOW() - INTERVAL 15 MINUTE)

Related

Get Records 30 minutes before date and time

Been trying to get this to work for 2 days and this is frustrating me.
Trying to get records 30 minutes before a date/time (Format in database is datetime).
This is what I have:
select id
from tbl_events
WHERE DATE_SUB(NOW(), INTERVAL -30 MINUTE) = DATE_FORMAT(start, '%Y-%m-%d %H:%i:%s')
What the heck am I missing?
Thanks
You already use the function DATE_SUB() so within that function you can simply use INTERVAL 30 MINUTE without the minus sign.
You also don't have to format start if it is a datetime or timestamp field.
Finally you shouldn't use = because times are hardly every exactly equal.
This gives this query:
select id
from tbl_events
WHERE start < DATE_SUB(NOW(), INTERVAL 30 MINUTE)
Probably. It's not extremely clear what you're trying to do.

How can I select data from this week using a Unix timestamp in SQL?

I have two columns in my database named dtp_s and dtp_e. Both of these columns hold strtotime() formatted ints which I then use in my PHP application to calculate hours/minutes between time intervals.
I want to display the 5 most recent records in date order, which works fine when I use this:
SELECT id
FROM _records
ORDER BY dtp_s DESC
LIMIT 5
However, I now want to convert the dtp_s back to a DateTime format in my Query and only pull out the data for that week. The issue I have is the records are for a weekly quota, my idea of pulling 5 records out covers Monday-Fri (which is all that is needed and uploaded) however, the following Monday will show the previous weeks Tuesday, Wednesday, Thursday and Friday as well.
I tried to use date_sub for a one week interval but this seems to only work on DateTime datatype columns, not a Unix timestamp:
SELECT id
FROM _records
WHERE dtp_s > DATE_SUB(NOW(), INTERVAL 1 WEEK);
ORDER BY dtp_s DESC
LIMIT 5
How only select the data that is from the current week by converting my formatted DateTime back to DateTime format? I appreciate any help in advance.
An example of my dtp_s and dtp_e is: 1595570400 1595584800
You can convert the filter value to a unix timestamp with date function unixtimestamp(), like so:
where dtp_s > unix_timestamp(now() - interval 1 week)
Actually, you can directly use unix_timestamp() with no conversion:
where dtp_s > unix_timestamp() - 7 * 24 * 60 * 60
Although unix_timestamp() can be very useful, unix_timestamp(now()) is actually redundant. You can just do the whole calculation in the domain of unix timestamps.

MySQL Query - Records between 15 days ago End_date to till End_date

I want to return records from database 15 days old from end _date to till end_date!
I am searching for the query for last 3 days!
However. I want your help to do a query. Its simple but I'm not sure how to do it.
I wrote query something like :
SELECT *
FROM bid_post
WHERE ending_date
BETWEEN DATE_SUB( DATE(`ending_date`) , INTERVAL 15
DAY )
AND ending_date >= CURDATE()
But it is not working !
The data column is a Varchar type. I am storing date as YYYY-mm-dd format
Does somebody now how can I accomplish this?
Thanks.
Please try with this query
SELECT *
FROM bid_post
WHERE ending_date between DATE_SUB( CURDATE() , INTERVAL 15 DAY )
AND CURDATE()
You should never store dates as varchar since these are not real dates and you need to store them using mysql native date data types.
Since the format is Y-m-d you may not need to do a conversion using str_to_date function and can use the query as
select * from bid_post
where
ending_date between date_sub(curdate(),interval 5 day) and curdate()
This will give you data from last 15 days till today.
Using conversion to real date you need str_to_date as
select * from bid_post
where
str_to_date(ending_date,'%Y-%m-%d') between date_sub(curdate(),interval 5 day) and curdate() ;
DEMO

Querying and grouping by date (Y/m/j) when date is in a Y/m/j H:i:s format

I have a table called tablex with the following attributes:
timestamp
eventid
timestamp is of format Y/m/j H:i:s
E.g. 2015/02/20 00:19:16
There are many events per day.
I am generating charts and I need to formulate my SQL query to get date VS count of events for the past 30 days.
SELECT COUNT(eventid)
FROM `tablex`
WHERE timestamp //this is the part where I'm stuck.
GROUP BY DAY(timestamp)
Or is it advisable to create another attribute only with date Y/m/j? Or can it be done via SQL query?
If timestamp is a date/time column -- which it should be. You should not be storing date/times as strings. Then you can do:
SELECT DATE(timestamp), COUNT(eventid)
FROM `tablex`
WHERE timestamp >= date_sub(CURRENT_DATE, interval 30 day)
GROUP BY DATE(timestamp)
Note that this query includes the date in the select.
If your timestamp is stored as a string, it is in a sort-of reasonable format. I would be inclined to translate it using a subquery and just use that.
SELECT thedate, COUNT(eventid)
FROM (select x.*, date(replace(left(timestamp, '/', '-'), 10) as thedate
from `tablex` x
) x
WHERE thedate >= date_sub(CURRENT_DATE, interval 30 day)
GROUP BY thedate;
Note that you can also use str_to_date() to convert the string to a date. I just find it easier in this case to use date() and replace().

PHP MySQL - Compare Datetime

I have a datetime field in my MySql table where the date and time are recorded in the following format:
2010-12-17 18:06:59
I would like to select records where the datetime is in the last 15 minutes. How do I construct my query to do this?
I tried the following but it doesn't work:
// check in last 15 minutes
$time_duration = (15 * 60);
"SELECT COUNT(CounterId) AS Count FROM visitors_counter WHERE TimeStamp > NOW() - $time_duration"
Thanks!
You could just do the entire thing in MySQL:
SELECT COUNT(CounterId) AS Count
FROM visitors_counter
WHERE TimeStamp > (NOW() - INTERVAL 15 MINUTE)
You have to subtract an interval :
SELECT COUNT(*) AS Count
FROM visitors_counter
WHERE TimeStamp > NOW() - INTERVAL 15 MINUTE;
I replaced COUNT(CounterId) with COUNT(*) because the latter is faster if CounterId can't be null. If it can, just replace * with CounterId.
P.S. I don't consider timestamp a good column name because it's a key word. Sure, it works correctly, but it may be "better" to replace it with something else.

Categories