0

I have table for tasks list.

I usually use linux time to set the dates but this time I tried DATE type.

how can I select all the task that their targetDate is in 2 days or less?

I know how to do it in the SQL query:

SELECT * FROM tasks 
WHERE `targetDate` >= CURDATE() 
  AND `targetDate`  < DATE_ADD(CURDATE(), INTERVAL 2 DAY)

I would like it know how to do it in the PHP code

id  int(10) 
title   varchar(250)    utf8_general_ci
text    text    utf8_general_ci
catID   tinyint(3)  
createUserID    int(4)  
createDate  date    
targetDate  date    
Roi
  • 181
  • 1
  • 1
  • 8
  • You need to connect to MySQL database and use 'mysqli_query'. – Pupil Oct 26 '15 at 09:45
  • Your data is in a database... like Pupil said... you have to connect to and query the database for your data using either `mysqli_` or more preferably `pdo_mysql`. – Kuya Oct 26 '15 at 09:51

4 Answers4

1
SELECT * FROM tasks WHERE targetDate BETWEEN '2015-10-13' AND '2015-10-27' 

OR

SELECT * FROM tasks WHERE DATE_FORMAT(targetDate,'%Y-%m-%d') BETWEEN '2015-10-13' AND '2015-10-27'
Krishna Gupta
  • 695
  • 4
  • 15
0

your Date type in SQL is in "YYYY-MM-DD" format. So in your query, you have to give the same format to grab the data you wanted.

Hope that helps..

0

You can use BETWEEN CLAUSE in combination with date_sub( now( ) , INTERVAL 2 DAY ) AND NOW( )

SELECT * FROM tasks 
WHERE
targetDate BETWEEN date_sub( now( ) , INTERVAL 2 DAY ) AND NOW( )
Ninju
  • 2,522
  • 2
  • 15
  • 21
0

use mysql CURDATE() or INTERVAL

SELECT * FROM tasks 
WHERE
targetDate BETWEEN (CURDATE() - INTERVAL 2 DAY ) and CURDATE()
Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20