Question: What are the formats for writing dates in MySQL? Answer: Standard SQL format: YYYY-MM-DD Date and time format: YYYY-MM-DD HH:MM:SSUNIX Timestamp: FROM_UNIXTIME(unix_timestamp) String format: STR_TO_DATE(string_value, format)
MySQL date writing format
In MySQL, the date type is used to store date and time information. When writing dates, you can use the following format:
Standard SQL format:
<code class="sql">YYYY-MM-DD</code>
For example:
<code class="sql">2023-03-08 -- 表示 2023 年 3 月 8 日</code>
Date time format:
<code class="sql">YYYY-MM-DD HH:MM:SS</code>
For example:
<code class="sql">2023-03-08 12:30:15 -- 表示 2023 年 3 月 8 日 12:30:15</code>
UNIX timestamp:
MySQL also supports storing UNIX timestamps, which represent the elapsed time since Epoch seconds.
<code class="sql">FROM_UNIXTIME(unix_timestamp)</code>
For example:
<code class="sql">FROM_UNIXTIME(1678339215) -- 表示 2023 年 3 月 8 日 12:30:15</code>
String format:
<code class="sql">STR_TO_DATE(string_value, format)</code>
where format
specifies the format of the string. For example:
<code class="sql">STR_TO_DATE('03/08/2023', '%m/%d/%Y') -- 表示 2023 年 3 月 8 日</code>
Note:
STR_TO_DATE()
The function must specify the correct format, otherwise an error will occur. The above is the detailed content of How to write date in mysql. For more information, please follow other related articles on the PHP Chinese website!