Change the Format of Input for a date from Y/M/D to D/M/Y in SQL/PHP from a form

198 Views Asked by At

I need to be able to change the date format from Y/M/D to D/M/Y. The database with all the data that I am querying has the date set as Y/M/D whereas the input form has the input yet as D/M/Y.

Does anyone have any idea how I can change the format?

NB the Date is 'DOB'

Here is the SQL:

$sql="SELECT `country_name`,`gdp`,`population`,Cyclist.name,Cyclist.dob FROM Country JOIN Cyclist ON Country.ISO_id=Cyclist.ISO_id 
WHERE 'dob'
BETWEEN '".$date_1."' AND '".$date_2."'";

Here is the PHP for getting the data from the form submission

$date_1=$_REQUEST['date_1'];
$date_2=$_REQUEST['date_2'];

2

There are 2 best solutions below

6
Raphael On BEST ANSWER

STR_TO_DATE parses a string to a date use a given format.

$sql="SELECT `country_name`,`gdp`,`population`,Cyclist.name,Cyclist.dob FROM Country JOIN Cyclist ON Country.ISO_id=Cyclist.ISO_id 
WHERE 'dob'
BETWEEN STR_TO_DATE('".$date_1.",'%d,%m,%Y') AND STR_TO_DATE('".$date_2.",'%d,%m,%Y')'";

STR_TO_DATE: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date

0
Pradnya Pathare On

You may change the date format in php date() and strtotime() e.g: $date_new = date('d-m-Y', strtotime($date)); and then passing it in sql query.