I’m coding to search data by compare dateTime. In web form, user input value 2012-06-04 store in $_REQUEST['dateSearch']. I create dateTime object from that:
$dateObj = date_create_from_format("Y-m-d",$_REQUEST['dateSearch']);
$dateParam = $dateObj->format("Y-m");
Then I use $dateParam in sql query to find data
$sql = "
SELECT *
FROM `temp` as `t`
WHERE `t`.`date` LIKE '$dateParam%'
";
Everything is ok, but I’m not sure MySQL alway use only one dateTime format Y-m-d H:i:s.
At here, $dateParam = $dateObj->format("Y-m"); I set default format is “Y-m”. Is the default format never change? I don’t want hard set format Y-m-d H:i:s in code, instead of, I think get format string from system to use is better. How do I get default dateTime format string from mySQL???
You misunderstand,
Y-m-d H:i:sis the ONLY format for datetime data type.
That’s why is named as
datetimeTo change the return format for datetime in mysql,
you can use date_format function in mysql
Such as,