Home >Database >Mysql Tutorial >How to write date in mysql

How to write date in mysql

下次还敢
下次还敢Original
2024-04-22 19:57:38443browse

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)

How to write date in mysql

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:

  • The date part must be in the format of YYYY-MM-DD.
  • The time part can be omitted, if omitted, the default is 00:00:00.
  • UNIX timestamp cannot be negative.
  • 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn