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
  • 732,580
  • 175
  • 1,330
  • 1,459
Leet Hudka
  • 226
  • 3
  • 16
  • Are you using SQL Server? – apokryfos May 14 '18 at 14:19
  • Do you want to change your SQL statement to only check the date? Or do you want to do that within your PHP code? –  May 14 '18 at 14:49
  • 1
    Which [DBMS product](https://en.wikipedia.org/wiki/DBMS) are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using `postgresql`, `oracle`, `sql-server`, `db2`, ... –  May 14 '18 at 14:49

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
  • 93,638
  • 21
  • 103
  • 149
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,610
  • 6
  • 30
  • 62