3

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 . "'";
6
  • you have a closing bracket in your query that is never opened? What is the error message you're getting? Commented Mar 30, 2016 at 7:55
  • DATETIME format 2016-04-02 but you get data as 30/03/2016 Commented Mar 30, 2016 at 7:56
  • yes not getting date as 30/03/2016 Commented Mar 30, 2016 at 7:56
  • I think channasmcs has answered your question, you need to convert 30/03/2016 to 2016-03-30 for the SQL query to work Commented Mar 30, 2016 at 7:59
  • tyied strtodate but not woring Commented Mar 30, 2016 at 8:00

2 Answers 2

2
$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')

is working

Sign up to request clarification or add additional context in comments.

2 Comments

any better answer ? please share
@AseshaGeorge, if evedate is in datetime format, you must concat $from_date with 00:00:00 and $to_date with 23:59:59, otherwhise the query will cut some to_date dates. see my comment above, you had it as suggestion.
0

You can convert date DATE_FORMAT

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.