I have a last_notified column on my MySQL table, data type DATETIME
I want to either only select the rows where last_notified is more than n seconds old, or compare the last_notified value from the db in php.
I have tried messing around with the date object.
$d = date('Y-m-d H:i:s'); // current date and time
$dd = date('2011-09-08 10:21:34'); // this is the format used in mysql.
I know I cannot just compare them, and i'm unaware of how to add time to the date object. I have seen examples of people using something along the lines of
$t = explode(" ",$dd);
date($dd, strtotime('+30 minutes', strtotime($t[1])));
but that doesn't work . I'm just not seeing it.
You can use sql like this:
SELECT * FROM your_table WHERE last_notified < DATE_SUB(NOW(), INTERVAL n SECOND)
Take a look at your first snippet and date mask in first line
Y-m-d H:m:s
you use m mask twice, so it means there will be month instead of minutes.
you should use i mask to specify the minutes
SELECT * FROM your_table WHERE last_notified < DATE_SUB(NOW(), INTERVAL n SECOND)
Related
I have a table called reports in MySQL(MariaDB) . There is a one(out of 5) column named logdate which is is of type datetime .columns stores the the date and time (in 24hr format) .
for ex here is sample value from that column
2021-04-10 09:35:00
I have to find all reports between a given date and time .
I get 4 variables from form data in PHP
$fromdate= $_POST['fromdate'];
$todate= $_POST['todate'];
$fromtime= $_POST['fromtime'];
$totime= $_POST['totime'];
$fromtime and $totime are just integers with value from 0-23 for hours.
For example the condition may be like get all data between 4th April 2021
from 5 o'clock To 8 April 2021 18 o'clock
i.e. From 2021-04-04 03:00:00 to 2021-04-08 18:00:00. There will be never condition on minutes and seconds .
My question is how to construct a datetime in PHP compatible with MySQL types so I can have good(efficient, there are millions of records in table ) search speed?
for ex
$select = "select * from reports where logdate between ? and ? ";
P.S: I tried saving date and time as integer as unixtime stamp. But when i convert from and to date received using strttotime() I facing time format issue due to bug in my code which so can use datetime only.
If you have any suggestion to improve efficiency of DB please suggest.Thanks
Hi this link may be of help in optimizing date comparison
MySQL SELECT WHERE datetime matches day (and not necessarily time)
This one below, will help you in formatting your strtotime() by using strptime()
https://www.php.net/manual/en/function.strptime.php
Also check your spelling or typo; you wrote "strttotime()" instead of "strtotime()" yours has an extra 't' in str"tto"time, it should be str"to"time, though without the double qoutes
Though I can't say for sure this is the most effective way but you can use hour(logdate) to compare with $fromdate and $todate
$select = "select * from reports where hour(logdate) between ? and ? ";
But it will only compare hour part. Please mention how you are getting date part to compare?
It is not a good idea to make a calculation on a field in the WHERE CLAUSE. In this case MySQL / MariaDB must calculate the value from this field to comapare it to see
if this ROW has this condition. So MySQL must read the whole table FULL TABLE SCAN and CANT use any INDEX.
A better way to do this is to store the calculation on fix site. Then MySQL calculated it only one time and can use a Index ( if there one) .
you can easy use a query like this:
$select = "SELECT * FROM reports where logdate between date(?) + INTERVAL ? HOUR AND date(?) + INTERVAL ? HOUR ";
to test see:
SELECT date('2021-04-05') + INTERVAL 16 HOUR;
result:
2021-04-05 16:00:00
Here is what is working for me after using Bernds solution .
I constructing datetime string in php
$fromstr ="$fromdate"." "."$fromtime".":00:00";
$tostr="$todate"." "."$totime".":00:00";
here is my query looks like for date of 7th April to 10th April
$ select = "SELECT * FROM reports where logdate >= '$fromstr' and logdate <= '$tostr' order by logdate";
after echoing it
"SELECT * FROM reports where logdate >= '2021-04-07 3:00:00' and logdate <= '2021-04-10 5:00:00' order by logdate";```
However I am not sure if can use index for logdate column and utilize it with above query.
How to write a sql query to find out that there are 2 days left before the current date.
In php, this can be done via:
$res['end_date'] - time () < 86400 * 3;
How can I do the same after 1 sql query, well or better, only 2 days, if less so that it does not work out, well, if it works out, it's okay.
UPD:
It is necessary to compose a sql query that will select only those records that have 2 days left before the end_date expires
The type is int for the field end_date and is stored via the time () function in php.
Can't compose a WHERE clause.
You can use the FROM_UNIXTIME function to convert it to a DateTime you can then use the NOW() plus 2 days to check if the date is under 2 days. You then have to check that the date is before the current time otherwise you'll get dates that have already gone.
SELECT
end_date
FROM
table
WHERE
FROM_UNIXTIME(end_date) <= NOW() + INTERVAL 2 DAY
AND
FROM_UNIXTIME(end_date) > NOW()
Assuming that you are storing an epoch timestamp (the number of seconds since January 1st, 1970), I would recommend:
select *
from mytable
where end_date >= unix_timestamp() and end_date < unix_timestamp() + 2 * 24 * 60 * 60
unix_timestamp() gives you the current epoch. You can use simple math to add two days to that.
The upside of this approach is that this does direct filtering against the store value, so this can take advantagae of an index on end_date - as opposed to converting the timestamp to a date, which requires converting the whole column before the filtering can happen. So this is much more efficient.
You can ajust the inequalities as you prefer. I used a half-open interval (inclusive on the lower bound and exclusive on the upper bound), which is a widely used approach.
I ended up doing this:
$time = time();
$params = $db->query("SELECT * FROM `params` WHERE (`end_date` - {$time}) < 86400 * 3");
And it worked.
I always do
select *
from mytable
where FROM_UNIXTIME(end_date) < NOW() + INTERVAL 2 DAY
This will get results where two days in the future is ahead of the end date ie, anything that will end within 2 days (or has already ended as I didn't add a check for that)
Edit: I see you can't use where
If you cannot use where clause
select FROM_UNIXTIME(end_date) - INTERVAL 2 DAY as end_date
from mytable
And then check in php if the result is before or after. This will show all results however
I would like to be able to get all the results from a MySql database that have the same day as a certain DateTime.
For example if the DateTime's date is "2/5/2018 10:15pm" all MySql results who's DateTime day is the 5th and in the same month and year (Ignoring the time) are returned.
I tried using
SELECT * FROM posts WHERE timestamp=:timestamp, Array(':timestamp' => $targetTime)
But that only gives results with the exact same DateTime without ignoring the time.
Use >= and < so it will preserve the index if you have.
WHERE timestamp >= DATE(:timestamp)
AND timestamp < DATE(:timestamp) + INTERVAL 1 DAY
DATE() will strip the time.
Here's a Demo.
Could you try ?
SELECT * FROM posts WHERE DATE(timestamp) = DATE($targetTime);
I want to limit my users to only (be able to) post something every 15 minutes.
So in my SQL query I select NOW() to get the current date and time, and also the user's last post date date_added. I want to compare the two dates and if the difference between now() and date_added is less than 15 minutes, then the user cannot yet post. If it's greater then he can. If less than 15 minutes then I'd like a message like 'Please wait x minutes and y seconds.' So I need some kind of date manipulation/comparison.
How should I approach this. In MySQL or PHP?
You could simply convert the mysql timestamp into a php-date and compare from there
$time = date ("Y-m-d H:i:s", $mysqltime);
You will find lots of useful snippets how to compare dates on the functions documentation: http://php.net/manual/de/function.date.php
Edit: pozs anwser nails it ...
In MySQL: DATE_ADD() or DATE_SUB(), then compare.
In PHP: DateTime->diff().
Something like
SELECT count(*) FROM posts WHERE last_post > DATE_SUB(NOW(), INTERVAL 15 minute);
Im using timestamps with my sql database.
I currently am making a voting system, can anyone make a if statement where if $stamp is older than two days it will run some code? i have been trying for hours and can't seem to get it to work. Thanks!
You don't need PHP to do this for you, let the database do the work as it's usually more efficient:
SELECT * FROM votes WHERE vote_timestamp <= DATE_SUB(NOW(), INTERVAL 2 DAY);
I believe this also work with UNIX_TIMESTAMP, TIMESTAMP, DATETIME and DATE columns as MySQL will perform the conversion automatically and apply it to the result set.
Something like this if you're using a unix timestamp:
$two_days_ago = strtotime('-2 days');
$sql = 'SELECT * FROM table WHERE timestamp_column < ' . $two_days_ago;
If your column is a MySQL timestamp (as others have mentioned) it would look more like this:
$two_days_ago = date('Y-m-d H:i:s', strtotime('-2 days'));
$sql = "SELECT * FROM table WHERE timestamp_column < '" . $two_days_ago . "'";
time() will give you the current time stamp. this number is in seconds.
So, everything with a timestamp which is less than time()-(2*24*60*60) is what you look for.
b.t.w usually people are reluctant to help if you do not show what you tried (or that u even tried).