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

How to write date in mysql

下次还敢
Release: 2024-04-22 19:57:38
Original
443 people have browsed it

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>
Copy after login

For example:

<code class="sql">2023-03-08  -- 表示 2023 年 3 月 8 日</code>
Copy after login

Date time format:

<code class="sql">YYYY-MM-DD HH:MM:SS</code>
Copy after login

For example:

<code class="sql">2023-03-08 12:30:15  -- 表示 2023 年 3 月 8 日 12:30:15</code>
Copy after login

UNIX timestamp:

MySQL also supports storing UNIX timestamps, which represent the elapsed time since Epoch seconds.

<code class="sql">FROM_UNIXTIME(unix_timestamp)</code>
Copy after login

For example:

<code class="sql">FROM_UNIXTIME(1678339215)  -- 表示 2023 年 3 月 8 日 12:30:15</code>
Copy after login

String format:

<code class="sql">STR_TO_DATE(string_value, format)</code>
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template