2

I have a column with name DDateTime and it's type is DateTime.

I want to compare only date from DDateTime with input date.

Example: '14-05-2018 08:00:00' = '14-05-2018'

How can I compare only date from DateTime variable?

$sql = "SELECT * 
        FROM ride 
        WHERE RideFrom = '$RideFrom' 
        and RideTo = '$RideTo' 
        and CONVERT(VARCHAR(10), DDateTime, 103) = '14-05-2018';";

I tried this query but it did not work

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Leet Hudka
  • 281
  • 3
  • 16

2 Answers2

2

You can us the MySQL DATE() function for this and make it really easy

AND DATE(DDateTime) = '14-05-2018'

Although I have to mention that your script is wide open to SQL Injection Attack Even if you are escaping inputs, its not safe! Use prepared parameterized statements in either the MYSQLI_ or PDO API's

RiggsFolly
  • 89,708
  • 20
  • 100
  • 143
0

Here is two methods.
Choose the one you like.

$str ='14-05-2018 08:00:00';

echo substr($str, 0, 10). "\n";
echo date("d-m-Y", strtotime($str));

Substr will always take the first ten characters, whatever they are.
Date will try to parse and make it unixtime then convert to datestring.

Andreas
  • 23,304
  • 5
  • 28
  • 61