MySQL Compare Dates with time difference - php

Is there a way to compare a datetime field in mySQL with the current date and return the time difference? I don't really know where to start so if anyone has an idea, please point me to the right direction.
Here's what I already tried.
SELECT *
FROM `order`
WHERE `dateTimeAdded` - NOW( ) <1;
basically what I am trying to achieve is to get the orders which is saved for more than 8 hours in the database. Thanks!

If you want your query to have a chance of using an index on the datetime column, you should not do manipulation (functions) on the column but only on the other side:
SELECT *
FROM `order`
WHERE dateTimeAdded < NOW( ) - INTERVAL 8 HOUR ;

If u just need the diff of dates use
DATEDIFF(now(), column)
and if you need the time difference use
TIMEDIFF(now(),column)

DATEDIFF(date1,date2);
Taken FROM

Related

Only select records between start and end time and date

I am working on a school project where I have to make a todo web app. now i have a little problem. I need to get the records that are running out of time (think 20% of the whole task left). now i'm looking for a solution in php or a sql statement with which i can retrieve only those records.
I tried many statements but i cant get one to work.
SELECT * FROM tasks
WHERE user_id='$user_id'
AND '$currentDate' BETWEEN start_date AND end_date
The above one is working with the date but not with time.
So now I need to have a statement or function that only retrieves the tasks that are almost finished. I've added a screenshot of the database and the application to clarify it a bit.
i hope someone could help me. (this is my first time using stackoverflow so sorry if i do something wrong)
First, you should not be munging your query with constants, date or otherwise. So, use now().
Second, combine the date/time into a single column
Third, you seem to want and:
WHERE user_id = ? AND
NOW() >= start_datetime AND
NOW() < end_datetime
If you want to store the date/time in separate columns, then you can combine them:
WHERE user_id = ? AND
NOW() >= ADDTIME(CAST(start_date as DATETIME), start_time) AND
NOW() < ADDTIME(CAST(end_date as DATETIME), end_time)

Select * matches where current date bigger than now and current time bigger than now

Working with dates and times has always been a challenge for me, I have done plenty of research on SE regarding mysql date queries but have not found anything concrete enough to help me with my problem.
Im working on app for work which needs to display all matches which has not expired, a match expires when the date has passed AND time has passed.
Example:
A match could still be active even though the match day is today providing that the time has not past...I think you get what I mean?
The above is the simple part the hard part is the implementation.
I could go:
SELECT * FROM matches WHERE match_date >= CURDATE();
MY Question(s)
How would I modify the above query that it includes a time function aswell?
Would you say the above query regarding DATE is the best / most effective query I can use for what I want to achieve?
Any help / advice appreciated
I think this does what you want:
SELECT *
FROM matches
WHERE match_date > CURDATE() OR
( match_date = CURDATE() and match_time > CURTIME() );
Note: You should really store the date and time in a single field. Then you could do:
SELECT m.*
FROM matches m
WHERE match_datetime >= now();
Much simpler. And more likely to use an index.
You can combine DATE and TIME with the TIMESTAMP() function.
SELECT * FROM matches WHERE TIMESTAMP(match_date, match_time) >= NOW()
In order to use an index at least for match_date you can add a redundant condition match_date >= CURDATE().
SELECT *
FROM matches
WHERE match_date >= CURDATE()
AND TIMESTAMP(match_date, match_time) >= NOW()
The first condition will ignore everything before today using an index.
As other already mentioned things get easier if you use a DATETIME or a TIMESTAMP column. If you use MySQL 5.7.6 (or greater), you can also create a computed/generated column and index it.

How to filter MySQL dates?

I'm trying to do a SELECT * FROM but only items that are less than 30 days old. Here is my select code:
SELECT * FROM `{$table_name33}` WHERE `type`='wpst-requiredinfo' ORDER BY `foreignkey` ASC;
However, my problem is that I can't figure out how to add WHERE AND last_updated is less than 30 days.
I'm not exactly sure how to write the query, but the date is showing up like this: 1428412603 in the table column, it doesn't look much like a date to me. I don't know where to start.
Try this where clause:
WHERE `type`='wpst-requiredinfo' and
last_updated >= date_sub(now(), interval 30 day)
EDIT:
Your date seems to be in Unix time format.
WHERE `type`='wpst-requiredinfo' and
last_updated >= unixtime_timestamp() - 30*24*60*60
Note: this puts all the functions on the current time. In particular, it does not use FROM_UNIXTIME(last_updated). This ensures that an index can be used for this part of the query. The best index would be on (type, last_updated).

PHP Simple Time Check (hour)

Although I have been working with PHP for a while, the one part of it I am still trying to get right is time.
I am creating a simple script that will check if the timestamp is greater than or equal to an hour, and if it is, it will be deleted from the database.
2013-01-03 20:30:25
DELETE FROM tablename WHERE timestamp = ?????
I am not sure how to execute the query to delete values with a timestamp of over an hour from the current time. Any help is greatly appreciated.
DELETE FROM tablename WHERE `timestmap` < DATE_SUB(NOW(), INTERVAL 1 HOUR)
Ref:- date_add and date_sub
First of all, 2013-01-03 20:30:25 is not a timestamp, it is a formatted date. The timestamp for that date would look like this: 1357245025. You can convert it to a timestamp using the strtotime function. You can also work out the timestamp of an hour ago by using strtotime("-1 hour") and performing a comparison on the values.
It might be faster just to do all of this within the MySQL query though, MySQL provides queries to do this, using a query similar to the one that Amit Garg provided.

Select Before The Date

I have a date, say its called $date. I want a a mysql_query to search a select number of weeks,days or even months before my $date. Is this possible? My explanation is not the greatest, but I do need a answer for this and do not know how to properly question it.
You could use mysql interval function?
"select * from table where `date` BETWEEN DATE_SUB(".$date.",INTERVAL 15 DAY ) AND CURDATE( )
That'll return the records from the last 15 days, you could use = insted of between if you want the records exactly 15 days old, or modify it for days, months, etc.
edit: if your working with php's time() remeber to use FROM_UNIXTIME($phpdate) inside your query.
i have a solution for this in SQL, Take it, if it would helps you
Day($date) gives you the date in the vaariable
Month($date) gives you the Month in the vaariable
Year($date) gives you the year in the vaariable
using simple where conditions, now you can search for a particulars
You can use the DATE_ADD and DATE_SUB functions to modify a date, and mysql understands a BETWEEN clause using dates. However, you can also use the TIMESTAMPDIFF function like so:
"SELECT foo FROM table WHERE TIMESTAMPDIFF(DAY, dateField, '$date') < '$desired_days'"

Categories