I need to consult a table using mysql...for today, tomorrow, after tomorrow,..
by now I use the following code where data is the day in format YYYY-MM-DD
$data_query = mysqli_query($conexionbd,'select * from `Meteo` where `data` >= "2017-03-31" ');
what should I insert in where data >= in order to get the data for today, tomorrow...without inserting manually the date?
You can it easy calculate with:
SELECT DATE (NOW() + INTERVAL 1 DAY);
sample
mysql> SELECT DATE (NOW() + INTERVAL 1 DAY) as result;
+------------+
| result |
+------------+
| 2017-03-30 |
+------------+
1 row in set (0,00 sec)
mysql>
To get tomorrow's date:
$tomorrow = date("Y-m-d", strtotime("+ 1 day"))
Now you can add that variable to your query:
$data_query = mysqli_query($conexionbd,'select * from `Meteo` where `data` >= "'.$tomorrow.'" ');
Then you can simply add more days as needed:
$after_tomorrow = date("Y-m-d", strtotime("+ 2 day"))
$DTtoday = new DateTime(); //Current date
$DT2days = clone $DTtoday; //Create new object from $DTtoday
$DT2days->add(new DateInterval("P2D")); //Add 2 days
$data_query = mysqli_query($conexionbd,'select * from `Meteo` where `data` BETWEEN "'.$DTtoday->format("Y-m-d").'" AND "'.$DT2days->format("Y-m-d").'"');
You could use now()
and between interval 2 days
data_query = mysqli_query($conexionbd,'select *
from `Meteo`
where `data` between now() and date_add(date(now() ), interval 2 day)) ;
Related
I have a Table in Database which has records of Logins.
Table name: user_logins
ID | timestamp
1 2019.01.03 (Year, Month, Day)
2 2019.01.04
3 2019.01.05
4 2019.01.05
5 2019.01.07
6 2019.01.07
7 2019.01.09
I want to Show only Count of Records by this Week.
From Monday to Sunday (04-02-2019 ... 10-02-2019)
My PHP and SQL Code is:
$mo = mysql_num_rows(mysql_query('SELECT * FROM user_logins WHERE DAYNAME(DATE(timestamp)) = "monday" and timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFWEEK(CURDATE())-0 DAY)'));
this should show the records of 04-02-2019
Here is my SQL Fiddle link:
SQL Fiddle
This:
DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
gives this week's Monday.
So:
SELECT * FROM user_logins
WHERE
timestamp
BETWEEN DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
and
NOW()
Try following query:
SELECT id FROM `user_logins`
WHERE timestamp >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND timestamp < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Demo
I have a varchar field contain a timestamp value, i want to get the tomorrow records.
This is my code as you can see:
$tomorrow = strtotime("1+ day");
SELECT * FROM tasks WHERE task_accomplish_date > $tomorrow
but the query is incorrect
thanks for the help in advance
Should be like this :
$tomorrow = strtotime("+1 day");
/* this will select all record before tomorrow*/
SELECT * FROM tasks WHERE task_accomplish_date < $tomorrow;
/* this will select all record after tomorrow*/
SELECT * FROM tasks WHERE task_accomplish_date > $tomorrow;
http://php.net/manual/en/function.strtotime.php
SELECT * FROM tasks WHERE task_accomplish_date > NOW() + INTERVAL 1 DAY
You should be able to do this all in the MySQL query
SELECT *
FROM `tasks`
WHERE DATE(`task_accomplish_date`) = DATE(NOW() + INTERVAL 1 DAY)
That converts your task_accomplish_date into a date value and looks for records where that date is equal to today + 1 day (tomorrow). That does not give you records from 2 days from today or beyond, just tomorrow. If you want tomorrow and all records beyond tomorrow you would use
SELECT *
FROM `tasks`
WHERE DATE(`task_accomplish_date`) >= DATE(NOW() + INTERVAL 1 DAY)
If you are storing a timestamp, these only care about the date part of the timestamp, not about the time part of the timestamp. If you care about the time part as well you can remove DATE() from both sides of the = in the WHERE clause
i have table which has column completed_at type (datetime)
i am inserting date in it by php date('Y-m-d H:i:s') 2016-05-17 00:00:14
I want to select data which has 3 hours ago
<?php
$sql = "Select `id`,`file` from `uploader` where `completed_at` < CURDATE() - INTERVAL 3 HOUR";
$DB = new DB();
mysqli_query($DB->db_connect(), $sql);
?>
but it gives me wrong answer.
Try this:
SELECT FROM table_name
WHERE completed_at < NOW() - INTERVAL 3 HOUR
I'm trying to get the number of rows where the date is today. Its working like this but only if the time is 00:00:00 so if i change the time to for example 2016-04-25 05:30:30 its not counting that row for some reason.
$result16 = $dbhandle->query("SELECT * FROM email WHERE signoff = DATE(NOW() )");
$row_cnt16 = $result16->num_rows;
What should I do to fix this?
Table information:
signoff = varchar(250)
Outputs:
if date is 2016-04-25 00:00:00 the output is 2
if the date is 2016-04-25 05:04:00 output is 1
try this:
SELECT *
FROM email
WHERE signoff BETWEEN DATE(NOW()) AND DATE(NOW() + INTERVAL 1 DAY)
First change your data type of signoff to datetime.
Then use this query
SELECT * FROM email
WHERE date(signoff) = DATE(CURDATE())
If speed is an issue you can use
SELECT * FROM email
WHERE signoff >= curdate()
AND signoff < curdate() + interval 1 day
Try this by adding Date to the signoff
$result16 = $dbhandle->query("SELECT * FROM email WHERE DATE(signoff) = DATE(NOW() )");
Here's my query:
SELECT *
FROM daily_records
AND date = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
I use this to generate a report of everything that happened yesterday. This works great, Tuesday-Friday. However, on Mondays, I want it to be able to search back to Friday (the previous business day, eg INTERVAL 3 DAY).
Is there a way to do this in mySQL? Or do I just need to check the day of the week in PHP before writing the query?
This should do it:
SELECT * FROM daily_records AND date =
DATE_SUB(CURDATE(), INTERVAL IF(DATE_FORMAT(NOW(), '%w') = 1, 3, 1) DAY)
You could do...
SELECT *
FROM daily_records
WHERE date = IF(DAYOFWEEK(CURDATE()) = 2, DATE_SUB(CURDATE(), INTERVAL 3 DAY), DATE_SUB(CURDATE(), INTERVAL 1 DAY))
mysql> create function PREV_BIZ_DATE ()
-> returns DATE
-> return (CURRENT_DATE() - interval if(DAYOFWEEK(CURRENT_DATE()) = 2, 3, 1) day);
mysql> SELECT * FROM daily_records WHERE date = PREV_BIZ_DATE();