简体   繁体   中英

How to get data form mysql database using php date?

In mysql contact_details table I've a column called created_date . I'm inserting date and time to this created_date column when I insert any new contact to that Table. Date and time is somethiing like this :

6/19/2013 18:25
6/19/2013 18:25
10/10/2014 17:25

Well, I've a Search form where 2 fields exist.

1) Date From.
2) Date To.

This 2 filed is pickup only Date (m/d/y) not TIME . Now I want to search data based on this 2 date. It's could be search Only Date From or Date To or Both .

1) Date From will be search Date From To Today date .
2) Date To will be search All data till Date To .
3) If both date exist then it's should return data between 2 date

So, what I'm doing is following : But it's showing me wrong result. How do I fix this ?

if(!empty($ad_datefrom)){    
    $getSearch .= "AND cd.created_date BETWEEN '$ad_datefrom' AND '$date' ";    
}

if(!empty($ad_dateto)){    
    $getSearch .= "AND cd.created_date BETWEEN'$ad_dateto' AND '$date' ";   
}

if(!empty($ad_datefrom) && !empty($ad_dateto)){
    $getSearch .= "AND cd.created_date BETWEEN '$ad_datefrom' AND '$ad_dateto' ";   
}

Note :

cd =  contact_details Table.
created_date = created_date Column of contact_details Table.  
$ad_datefrom =  Date From field.
$ad_dateto = Date To Field.
$date = current date (m/d/y)

Thank You :)

MySQL想要像'2014-12-24 23:59:59'这样的格式的日期时间,使其具有date()函数,执行date("Ymd H:i:s",$timestamp)

$ad_datefrom and $ad_dateto are in string format hence wont work with the between clause. Convert them using php date something like this:

 $getSearch .= "AND cd.created_date BETWEEN '" . date("Y-m-d H:i:s", strtotime($ad_datefrom)) . "' AND '".date("Y-m-d H:i:s",strtotime($date))."' ";
 $getSearch .= "AND cd.created_date BETWEEN '" . date("Y-m-d H:i:s", strtotime($ad_dateto))."' AND '".date("Y-m-d H:i:s",strtotime($date))."' "; 

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