I imagine this is pretty simple, but can't figure it out. I'm trying to make a few pages - one which will contain results selected from my mysql db's table for today, this week, and this month. The dates are entered when the record is created with date('Y-m-d H:i:s');
. Here's what I have so far:
day where date>(date-(60*60*24))
"SELECT * FROM jokes WHERE date>(date-(60*60*24)) ORDER BY score DESC"
week where date>(date-(60*60*24*7))
"SELECT * FROM jokes WHERE date>(date-(60*60*24*7)) ORDER BY score DESC"
month (30 days) where date>(date-(60*60*24*30))
"SELECT * FROM jokes WHERE date>(date-(60*60*24*30)) ORDER BY score DESC"
Any ideas would be much appreciated. Thanks!
WEEK() function in MySQL is used to find week number for a given date. If the date is NULL, the WEEK() function will return NULL. Otherwise, it returns the value of week which ranges between 0 to 53. The date or datetime from which we want to extract the week.
Use the MONTH() function to retrieve a month from a date/datetime/timestamp column in MySQL. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a date/datetime/timestamp column.
Using MONTH() and GETDATE() function to fetch current month In SQL, we use the GETDATE() method to retrieve the value of the current date which is today's date. Let us try executing a simple query statement using which we will retrieve the value of the current date using GETDATE() function.
MySQL CURDATE() Function The CURDATE() function returns the current date.
Assuming your date column is an actual MySQL date column:
SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 DAY) ORDER BY score DESC; SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 WEEK) ORDER BY score DESC; SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY score DESC;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With