Write in search field date in dd/mm/YYYY format and search in the database in YYYY-mm-dd format

Asked

Viewed 111 times

1

I want to type in the form the date in the format dd/mm/YYYY but search in the database in the format YYYY-mm-dd, please already tried everything, from a HELP Ai

  • Use the class DateTime, has static function createFromFormat and the function format to extract.

2 answers

4


In Mysql there is the function STR_TO_DATE, you receive as a parameter your string date and converts to a valid date in the query SQL:

SELECT STR_TO_DATE("01/12/2018", '%d/%m/%Y')

Returns: 2018-12-01

2

$data = substr($obj->getData(), 8, 2);
$data .= "/";
$data .= substr($obj->getData(), 5, 2);
$data .= "/";
$data .= substr($obj->getData(), 0, 4);
  • you can use "substring" where in the above example "obj->getData()" is your date coming from the Database!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.