简体   繁体   中英

how to select data between 2 dates

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    
SELECT * FROM tasks WHERE targetDate BETWEEN '2015-10-13' AND '2015-10-27' 

要么

SELECT * FROM tasks WHERE DATE_FORMAT(targetDate,'%Y-%m-%d') BETWEEN '2015-10-13' AND '2015-10-27'

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..

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

use mysql CURDATE() or INTERVAL

SELECT * FROM tasks 
WHERE
targetDate BETWEEN (CURDATE() - INTERVAL 2 DAY ) and CURDATE()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM