Get all results from that day of Datetime - php

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);

Related

Request to find out the date in 2 days

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

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 where date range is exactly 1 month

I am trying to abstract querying a month of data on the MySQL level without having to implement a series of conditions that determine the last day of a given month on a given year. LAST_DAY() seemed to be the answer but it appears to only return the date and not the provided time.
SELECT LAST_DAY('2011-02-05 23:59:59');
returns
2011-02-28
When I try to use it in a query, I lose all the entries from the last day of the month because without time the date value is not accepted.
SELECT * FROM subscriptions
WHERE (`modified_at` BETWEEN '2014-12-01 00:00:01' AND LAST_DAY('2014-12-01 23:59:59'));
How can I modify this query so that the LAST_DAY function either generates the last time of the day or preserves the time given?
How about changing the logic to ignore times?
SELECT *
FROM subscriptions
WHERE `modified_at` >= '2014-12-01' AND
`modified_at` < date_add('2014-12-01', interval 1 month)
And this doesn't need last_day().
Use ADDTIME(expr1,expr2);
Like this:
ADDTIME(LAST_DAY('2011-02-05 23:59:59'), '23:59:59')

Selecting all timestamps for a certain date in PHP

I have a table with a few records and for each of these records I've also added a UNIX_TIMESTAMP. Now, I also have a search engine for those records in which I can choose a date using a jQuery datapicker. My question is how do I make the request so that to select all timestamps from the database for a certain date.
With an index on your timestamp column you will get a faster result with:
SELECT *
FROM table_name
WHERE time_stamp_column_name >= :date_picked
AND time_stamp_column_name < :date_picked + INTERVAL 1 DAY
Where :date_picked is your bound-in picked date as a string in the format 'YYYY-MM-DD'.
You can use from_unixtime to convert it into a date
select *
from
table
where
date(from_unixtime(your_timestamp_col)) = '2014-10-01'
use unix timestamp
you can also see this

php sql date time comparison

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)

Categories