I currentluy use this custom SQL in Contao SQL to display all entries (metamodel) that are in the future.
SELECT * FROM {{table}} WHERE party_date > UNIX_TIMESTAMP();
Now when I have a entry (party) which is scheduled for 2017/03/28 it won't be displayed when its 2017/03/29.
But how can I keep this entry up until 2017/03/29 - 04:00am in the morning?
Visitors of the website should see this partry up until 4am in the morning (event site).
Is it possible with UNIX_TIMESTAMP() ?
Assuming you want to have 4 hours' gap, you can subtract 4 hours from current datetime and compare the DATE part of party_date and NOW(), e.g.:
SELECT *
FROM table
WHERE DATE(party_date) >= DATE(DATE_SUB(NOW(), INTERVAL 4 HOUR))
By this logic, 2017/03/29 - 04:00am would result in 2017/03/29 and as it's same as date part of party_date, it will be displayed.
Here's MySQL's documentation for datetime functions.
Related
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 have two columns(date field, time field) which are storing date and time in string and first step is to combine these two fields so i can compare them with current date(i'm not sure if i'm doing that right). Second step is writing query which should select records only if obtained date from these two columns is close to current date. And that date should be between current date and 4 hours before current date because after that something will happen with all those records if some conditions are not met.
Format of columns date and time:
event_date = 'yyyy/MM/dd'
event_time = 'HH:mm'
So here is my query:
SELECT *
FROM `events`
WHERE DATE_SUB( NOW( ) , INTERVAL 4 HOUR )
< ADDTIME( event_date, event_time )
Maybe i should firstly convert these strings to date and then work with them or something else? All in all if i have some data with these dates for example:
1. 2017-02-26 10:00
2. 2017-02-26 11:00
3. 2017-02-27 10:00
And current datetime is: 2017-02-26 06:00
. In this case i would get only 1 record and when one hour pass or 2017-02-26 07:00 then i will get 2 records
If event_date is the date of the event and event_time is the time, you could combine them using the timestamp mysql function:
select event_date, event_time, timestamp(event_date, event_time)
from events;
but it's usually a good idea to actually store the date and time information in a single field e.g. event_datetime
you can then write your query like this:
select *
from
events
where
event_datetime between now() and now() + interval 4 hour;
this will return all events starting from now and 4 hours in the future. Or if you want the events from the last 4 hour in the past try with:
event_datetime between now() - interval 4 hour and now();
You should convert your date and time to a proper datetime data type. That said, you can do something like the following to accomplish what you want:
SELECT * FROM `events`
WHERE STR_TO_DATE(CONCAT(event_date,' ',event_time), '%Y/%m/%d %H:%i')
BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 4 HOUR)
The query will return all rows between now and 4 hours from now.
UPDATE: I just noticed the answer from #fthiella, using timestamp probably gives better performance than the example I provided.
I have an html table that displays maintenance records. There are 3 columns that have dates in the future.
I want to have a button above the table that checks whether any of these 3 dates are within the next 30 days. If so, the row is displayed and other rows that are not of immediate concern are not displayed.
What is the best approach for achieving a filter like this?
Update: I'm trying to do it in a MySQL query.
I have 3 attributes in the SQL table that are date.
Does anyone know how to query whether the dates are within the next 30 days?
Does anyone know how to query whether the dates are within the next 30 days?
Use this :
WHERE yourdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
uses between, curdate() and date_add()
Update
to check multiple dates you need to do :
WHERE yourdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
AND yournextdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
AND anotherdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
I am trying to write a query to select all records from users table where User_DateCreated (datetime field) is >= 3 months from today.
Any ideas?
SELECT *
FROM users
WHERE user_datecreated >= NOW() - INTERVAL 3 MONTH
If you want to ignore the time of day when a user was created you can use the following. So this will show someone created at 8:00am if you run Quassnoi's example query at 2:00pm.
SELECT *
FROM users
WHERE DATE(user_datecreated) >= DATE(NOW() - INTERVAL 3 MONTH)
Using DATE(user_datecreated) prevents mysql from using any indexes on the column, making the query really slow when the table grows.
You don't have to ignore the time when the user was created if you remove the time from the "3 months ago" date, as all the users created that day will match the condition.
SELECT *
FROM users
WHERE user_datecreated >= DATE(NOW() - INTERVAL 3 MONTH);
I am currently developing a sports website where one of the pages with be forthcoming fixtures in which the user will be able to what team and where the team are playing their next match.
I have a database with the following fields...
ID
TEAM NUMBER
OPPOSITION
VENUE
DATE
MEET TIME
MATCH TYPE
So a row of data pulled from the DB and print_r'd may look like this
ID=>[1] TEAM NUMBER=>[1] OPPOSITION=>[YORKSHIRE] VENUE=>[HOME] DATE=>[2009/4/25] MEET TIME=>[13.00] MATCH TYPE=>[CUP]
My problem is i cannot work out how to show the next match dependent on what the current date is, so for example for now I want the site to show all the games that will happen over the weeken of the 25th April 2009 and then once that has gone the fixtures for the next weekend.
Hope this makes sense and some one give me an idea of how to tackle this.
select * from my_events where date between now() and date_add(now(), interval 7 day);
Should do it I think.
Instead of relying entirely on MySQL, you can also use PHP's strtotime() function:
$query = "select * from my_events where date between now() and ".
date("Y-m-d", strtotime("+1 week"));
For MySQL check out the Date and Time functions. You can use a combination of CURDATE() and ADDDATE() to achieve what you need.
Your description is very vage but try something like this:
SELECT all_fields_you_need
FROM table_name
WHERE `DATE` > CURDATE() AND `DATE` <= DATE_ADD(CURDATE(), INTERVAL 7 DAY)
ORDER BY `DATE` ASC
(not tested, just written as it came into my mind...)
Load it all into an array and display the data
you can get the system date (in Oracle using sysdate) and then add to it, so look for all records where DATE = sysdate + 7. You may have to play with this a little, formatting the date so that sysdate + 7 returns a date without the time, but that is basically what you need.
EDIT:
If you want the event between now and a week from now (if games are only on the weekend, then this will return next weekend's games) do
DATE > sysdate AND DATE <= sysdate + 7
To get the next match for team xxx
SELECT *
FROM myTable
WHERE TEAM NUMBER = xxx
AND DATE = ( SELECT MIN(DATE)
FROM myTable
WHERE TEAM NUMBER = xxx
AND DATE > NOW() )
I suspect this is what you really want, if matches only take place at weekends (which seems to be an assumption from your question).
Today + 7 days is not the same as next weekend unless today happens to be the same day of the week as the match.