简体   繁体   中英

get mysql data between two dates using php

I am Trying to fetch Data between two dates, date was stored as DATETIME in mysql and showing as 2016-04-02 04:55:03

to get dates I am using bootstrap datetimepicker with input form like below

<div class="input-group date" id="datetimepicker1">
     <input type="text" class="form-control" id="checkin" name="from_date" data-date-format="dd/mm/yyyy" placeholder="Select Start Date">
      <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
   </div>
   <div class="gap-small"></div>
   <div class="input-group date">
       <input type="text" class="form-control datechange" id="checkout" name="to_date" data-date-format="dd/mm/yyyy" placeholder="Select End Date">
       <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
   </div>

and i am using AJAX to send these values to PHP.

in result page:

var_dump($_POST)

result:

    array(3) { ["from_date"]=> string(10) "30/03/2016" ["to_date"]=> string(10) "05/04/2016" ["search_place"]=> array(1) { [0]=> string(0) "" } }

i am getting values in from_date and to_date

now when i am trying to retrive between from and to dates its not working below is my sql statement

$sql="SELECT * FROM mytable WHERE evedate BETWEEN '" . $from_date . "' AND  '" . $to_date . "'";
$sql="SELECT * FROM mytable WHERE evedate BETWEEN concat(str_to_date('".$from_date."','%d/%m/%Y')) AND concat(str_to_date('".$to_date."','%d/%m/%Y')

正在工作

您可以转换日期DATE_FORMAT

$sql="SELECT * FROM mytable WHERE DATE_FORMAT(evedate, '%m/%d/%Y') BETWEEN '" . $from_date . "' AND  '" . $to_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