dougeqiang1619 2014-11-21 06:09
浏览 116
已采纳

如何使用php日期从mysql数据库获取数据?

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

  • 写回答

2条回答 默认 最新

  • doutenggu4070 2014-11-21 10:15
    关注

    $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))."' "; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了