问题
I have to insert dates in MySQL with the format dd-mm-yy
.
I know that if I change the date format to yy-mm-dd
it will work, but my client wants it in the format dd-mm-yy
, and the default format of a MySQL date is 0000-00-00
.
回答1:
As Honeyboy said in the comments, you can use the SELECT_TO_DATE function with the format string "%d-%m-%y"
. You can test it like this:
CREATE DATABASE test;
USE TEST;
CREATE TABLE test (date1 date);
INSERT INTO test (date1) VALUES (STR_TO_DATE("03-04-15","%d-%m-%y"));
then with
SELECT * FROM test;
you obtain:
+------------+
| date1 |
+------------+
| 2015-04-03 |
+------------+
If you use PHP, you can then make a prepared statement such as:
$stmt = $conn->prepare("INSERT INTO test (date1) VALUES (STR_TO_DATE(?,'%d-%m-%y'));");
$stmt->bind_param("s", $client_date);
$stmt->execute();
Same principle with other languages.
来源:https://stackoverflow.com/questions/48947472/how-to-insert-a-date-in-the-format-dd-mm-yy-in-mysql