date(now) only working with time 00:00:00 - php

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() )");

Related

PHP/MYSQL get tomorrow records from

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

consulting database when data is today, tomorrow, after tomorrow

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)) ;

MYSQL Last month Query is not returning first 9 days

I am querying records from the last calendar month. As it is February, it should return all the records that were added on January this year.
My Query:
`SELECT * FROM table_name WHERE date >=
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1
DAY) AND date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1
MONTH)), INTERVAL 0 DAY) AND campaign = '$campaign' ORDER BY date
ASC`
It returns some records but skips the first 9 days. It starts showing records from 10th of the previous month. What am I missing here?
check your date field type and make sure you have not mistaken it with varchar.
SELECT * FROM table_name WHERE
(MONTH(date) = (MONTH(NOW()) - 1) AND YEAR(date) = YEAR(NOW()))
OR
(MONTH(date) = 12 AND MONTH(NOW())=1 AND YEAR(date) = (YEAR(NOW()) - 1) AND campaign = '$campaign' ORDER BY date
ASC`
Try To Get Data in step by step like,
First, you should try below query.
SELECT Date, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY) AS StartDate, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY) AS EndDate FROM MyTable
Second, If First Step give right date then get your data by directly writing your date rather than DATE_ADD function in where clause.
Third, If These will Give you write DATA then try to fetch data using DATE_ADD function.
Replay If you will get solution.
SELECT * FROM table_name WHERE date between DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 MONTH) AND DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 DAY) AND campaign = '$campaign' ORDER BY date ASC

Get data from database inserted in latest 7 days

I am in problem in mysql database data retrieving. I have to get latest data inserted within a week or latest 7 days. I just know get data of specific date, but no within a span of days.
Please anyone help me. I am new in mysql.
You are looking for INTERVAL. For example, this will find all users whose created_time is in last 7 days and you have field created_time to tracked date of creation of record
SELECT * from users where created_time > (NOW()-INTERVAL 7 DAY)
You can do it using below query.
SELECT *
FROM `table`
WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND
CURDATE()
OR
SELECT * FROM `table` WHERE `date` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
OR
SELECT * FROM table WHERE DATEDIFF(NOW(),dateField) < 7
we can use DATE_SUB Mysql default function
select * from yourtable where date_of_insertion >= DATE_SUB(NOW(),INTERVAL 7 DAY)
<?php
$date = date("your_date_format", strtotime(- 7 days));
$result = mysqli_query($link, "SELECT * FROM table WHERE `date` > '$date'");
?>
Simple as that.
try :
select * from tablename where add_time >= now() - interval 7 day

Show today orders from table

I do not understand how to get output from my sql table .
the table date is stored in int . for example date output from one row is like 1362157869 .
i want to show today orders in query :
php : $today = date("y-m-d", time());
query : SELECT * FROM test WHERE date = '$today'
but it didn't work . i also try this :
SELECT * FROM test WHERE date LIKE '$today'
Query without PHP var:
SELECT * FROM test WHERE date = DATE(NOW());
You are storing your date as a UNIXTIMESTAMP, so you have to convert it to DATETIME using FROM_UNIXTIME, and then you have to extract only the date part, ignoring the time, using the DATE() function:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
Please see fiddle here. You can then extract yesterday records with something like this:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = CURDATE() - INTERVAL 1 DAY
Or a specific date with this:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = '2013-03-01'
To make use of an index
Assiming that your date column could contain not only the date part but also the time, you could also use this that will return all today records:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP(CURDATE())
and this for yesterday:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP(CURDATE() - INTERVAL 1 DAY)
AND date < UNIX_TIMESTAMP(CURDATE())
or this for a specific date:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP('2013-03-01')
AND date < UNIX_TIMESTAMP('2013-03-01' + INTERVAL 1 DAY)

Categories