Im trying to make a query where time() is between date_start and date_end
i tried:
date_start > CURRENT_TIMESTAMP AND date_end < CURRENT_TIMESTAMP
and
WHERE CURRENT_TIMESTAMP BETWEEN date_start AND date_end
Still it does not workout for me. Tried CURRENT_TIMESTAMP() too.
Mysql
id item_id percentage rabatcode date_start date_end
9 6 50 XXY-GGS-82 1323817200 1324422000
Compare NOW() against the values via FROM_UNIXTIME():
WHERE NOW() BETWEEN FROM_UNIXTIME(date_start) AND FROM_UNIXTIME(date_end)
Ideally, if it is possible for you to modify your database structure, it is usually much better to store dates as MySQL's native DATETIME type, owing to the fact that MySQL's date processing functions work with the date types without conversion.
You mention in the title of your question that you want to check when the current time is between to UNIX timestamps, so you need to use the UNIX_TIMESTAMP() function:
WHERE UNIX_TIMESTAMP() BETWEEN date_start AND date_end
NOW() returns a string (from the manual: '2007-12-15 23:50:26') so it won't work for your purposes.
The problem is you cannot use CURRENT_TIMESTAMP twice. You should look into converting your dates to a different format and comparing them to NOW() (if they cannot be compared to NOW() already.
Related
What is the difference between NOW() and DATE(NOW()) in terms of performance, results and precision? How will MySQL understand those 2 functions?
I was wondering what is the best between
WHERE the_date < NOW()
and
WHERE the_date < DATE(NOW())
NOW() returns current date/time. DATE(NOW()) returns the date part. If you want to know only the date, use CURRDATE().
You are looking at two different comparisons.
Since the_date contains a time of day, the expression the_date < NOW() will take into account the time of day at the moment you execute the SQL statement, which could make a difference if the YYYY-MM-DD in the_date is the same as the current datetime.
In the case of the_date < DATE(NOW()), you are comparing the_date to YYYY-MM-DD 00:00:00000, so the current time of day from NOW() is ignored and set to midnight.
What is more important here is what you need to do and what results you are expecting.
The DateTime.Now property returns the current date and time, for example 2011-07-01 10:09.45310.
The DateTime.Today property returns the current date with the time components set to zero, for example 2011-07-01 00:00.00000.
The DateTime.Today property actually returns DateTime.Now.Date:
I have a table 't' with date(yyyy-mm-dd), hour(1-12), minute(00-59), ampm(a/p), and timezone(pst/est) fields.
How can I select the rows that are <= now()? (ie. already happened)
Thank you for your suggestions!
edit: this does it without attention to the hour/minute/ap/tz fields:
SELECT * FROM t.date WHERE date <= now()
Here's one way to do it - combine all your seconds, minutes, etc into a date and compare to NOW(), making sure you do the comparison in the same time-zone. (Untested):
SELECT *
FROM t
LEFT JOIN y ON t.constant=y.constant
WHERE CONVERT_TZ(STR_TO_DATE(CONCAT(date,' ',hour,':',minute,' 'ampm),
'%Y-%m-%d %l:%i %p' ),
timezone,"SYSTEM") < NOW();
If your hour is 01 - 12 not 1-12 then use %h instead of %l in the STR_TO_DATE.
The STR_TO_DATE tries to stick your date and time columns together and convert them into a date.
The CONVERT_TZ(...,timezone,"SYSTEM") converts this date from whatever timezone is specified in the timezone column to system time.
This is then compared to NOW(), which is always in system time.
As an aside, perhaps you should make a single column date using MySQL's date datatype, as it's a lot easier to do arithmetic on that!
For reference, here is a summary of very useful mysql date functions where you can read up on those featuring in this answer.
Good luck!
SELECT * FROM t
WHERE `date`<=DATE_SUB(curdate(), INTERVAL 1 DAY)
OR (
`date`<=DATE_ADD(curdate(), INTERVAL 1 DAY)
AND
CONVERT_TZ(CAST(CONCAT(`date`,' ',IF(`hour`=12 AND ampm='a',0,if(ampm='a',`hour`,`hour`+12)),':',`minute`,':00') AS DATETIME),'GMT',`timezone`)<=NOW()
)
Rationale for date<=DATE_[ADD|SUB](curdate(), INTERVAL 1 DAY):
The fancy conversion is quite an expensive operation, so we don't want it to run on the complete table. This is why we pre-select against an UNCHANGED date field (possibly using an index). In no timezone can an event being more than a day in current timezone's past be in the future, and in no timezone can an event more than a day in the curent timezone's future be in the past.
What is difference between MySQL Curdate() and Now()?
For questions like this, it is always worth taking a look in the manual first. Date and time functions in the mySQL manual
CURDATE() returns the DATE part of the current time. Manual on CURDATE()
NOW() returns the date and time portions as a timestamp in various formats, depending on how it was requested. Manual on NOW().
Just for the fun of it:
CURDATE() = DATE(NOW())
Or
NOW() = CONCAT(CURDATE(), ' ', CURTIME())
CURDATE() will give current date while NOW() will give full date time.
Run the queries, and you will find out whats the difference between them.
SELECT NOW(); -- You will get 2010-12-09 17:10:18
SELECT CURDATE(); -- You will get 2010-12-09
Actually MySQL provide a lot of easy to use function in daily life without more effort from user side-
NOW() it produce date and time both in current scenario whereas
CURDATE() produce date only, CURTIME() display time only, we can use one of them according to our need with CAST or merge other calculation it, MySQL rich in these type of function.
NOTE:- You can see the difference using query select NOW() as NOWDATETIME, CURDATE() as NOWDATE, CURTIME() as NOWTIME ;
There's a query I'm working on that has
... WHERE start_date >= DATE(NOW()) AND end_date >= DATE(NOW())
start_date/end_date are in the date format: yyyy-mm-dd
What kinds of results should be expected from this kind of query?
Both start_date and end_date is greater than or equal to today's date.
Instead of using DATE(now()) you can use CURRENT_DATE().
What kinds of results should be expected from this kind of query?
If start_date and end_date are proper mySQL DATE fields, it should work just fine and return all entries where start_date AND end_date are today's date, or a date in the future.
If they are varchar fields, you may want to consider converting the columns into DATE fields - I assume the values are going to remain but make a backup of course - or casting them as DATE fields (slow!):
WHERE CONVERT(start_date, DATE) => DATE(NOW()) AND CONVERT(end_date, DATE) => DATE(NOW())
you will be comparing dates at the date (i.e. time not considered) level of granularity. i.e. all rows where the day is on or later than today (NOW()), assuming that start_date and end_date are date columns.
This should work fine, as you can use NOW() within a date comparision.
However, you don't need to wrap NOW() within DATE - it'll work as-is as long as you're using one of the time related (DATE, DATETIME, TIMESTAMP, etc.) field types.
As such, you should just be able to use...
WHERE start_date >= NOW() AND end_date >= NOW()
That would work, provided that you surrounded start_date and end_date with single quotes. It would give you all the records where the start date and end date are in the future.
I'm working with a database that has date information stored as a Unix timestamp ( int(11) ) and what I want to do is only return entries from the past X days, the past 90 days for example.
What I've come up with is:
SELECT * FROM mytable WHERE category=1 AND
FROM_UNIXTIME( time ) > DATE_SUB(now(), INTERVAL 91 DAY)
Where 'time' is the int(11) in the db. This seems to be working fine, but just wondering what others think of this.
SELECT * FROM mytable WHERE category=1 AND
time > (UNIX_TIMESTAMP() - ((60*60*24)*90))
or simply
SELECT * FROM mytable WHERE category=1 AND
time > (UNIX_TIMESTAMP() - (86400*90))
this is just comparing a number (seconds in this case)
This query is bound to cause you headaches down the way as MySQL needs to do the conversion of dates for every row making use of indexes impossible. Unix timestamps are numbers, so instead of converting a timestamp to another date format, convert your lookup dates to unix timestamps.
What is the reason for storing the timestamp as an int ? I would use the mysql DATETIME data type because you can use the many Date Time functions mysql has.
If you do not have control over the data type of this field I would convert your date to the unix timestamp int before you do your query and compare it that way.
Just thinking aloud... wouldn't doing it the other way around cause less work for the DB?
time > UNIX_TIMESTAMP( DATE_SUB(NOW(), INTERVAL 91 DAY) )