Detailed explanation of MySQL date string timestamp conversion

Detailed explanation of MySQL date string timestamp conversion

The conversion between time, string and timestamp is commonly used in daily life. Although it is commonly used, I like to search for the usage almost every time I use it. This article will serve as a note to organize the conversion between the three (i.e.: date to string, date to timestamp, string to date, string to timestamp, timestamp to date, timestamp to string) for easy reference in the future;

Functions involved

date_format(date, format) function, MySQL date formatting function date_format()

unix_timestamp() Function

str_to_date(str, format) function

from_unixtime(unix_timestamp, format) function, MySQL timestamp formatting function from_unixtime

Time to string

select date_format(now(), '%Y-%m-%d'); 
#Result: 2016-01-05

Time to timestamp

select unix_timestamp(now());  
# Result: 1452001082

String to time

select str_to_date('2016-01-02', '%Y-%m-%d %H'); 
#Result: 2016-01-02 00:00:00

Convert string to timestamp

select unix_timestamp('2016-01-02');  
# Result: 1451664000

Timestamp to time

select from_unixtime(1451997924); 
#Result: 2016-01-05 20:45:24

Timestamp to string

select from_unixtime(1451997924,'%Y-%d');  
//Result: 2016-01-05 20:45:24

Schedule

MySQL date format value range.

value meaning
Second %S, %s Two-digit second (00,01, ..., 59)
point %I, %i Minute as a two-digit number (00,01, ..., 59)
Hour %H 24-hour format, two-digit hour (00,01, ...,23)
%h 12-hour format, two-digit hour (00,01, ...,12)
%k 24-hour clock, hour in numerical form (0,1, ...,23)
%l 12-hour clock, hour in numerical form (0,1, ...,12)
%T 24-hour system, time format (HH:mm:ss)
%r 12-hour clock, time format (hh:mm:ss AM or PM)
%p AM or PM
week %W The name of each day of the week (Sunday, Monday, ..., Saturday)
%a Abbreviation of the day of the week (Sun, Mon, ..., Sat)
%w Weekday as a number (0=Sunday, 1=Monday, ..., 6=Saturday)
%U The number represents the week number, with Sunday being the first day of the week
%u Numbers represent the week number, with Monday being the first day of the week
sky %d Two digits representing the day of the month (01,02, ...,31)
%e Number representing the day of the month (1,2, ...,31)
%D English suffixes indicate the day of the month (1st, 2nd, 3rd ...)
%j Three-digit day of the year (001, 002, ..., 366)
moon %M English month names (January, February, ..., December)
%b English abbreviation of the month name (Jan, Feb, ..., Dec)
%m Two digits representing the month (01,02, ...,12)
%c Numbers representing the month (1,2, ...,12)
Year %Y Four-digit year (2015, 2016...)
%y Two-digit year (15,16...)
Text Output %text Directly output text content

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Summary of MySQL date data type and time type usage
  • MySQL date and time format conversion implementation statement
  • Mysql date time DATE_FORMAT (date, format)
  • Functions and methods for converting dates and timestamps in MySQL
  • mysql gets yesterday's date, today's date, tomorrow's date, and the time of the previous hour and the next hour
  • Get the current system time and date in MySQL to facilitate query and judgment code
  • Summary of MySQL functions for getting the current date and time
  • Detailed explanation of commonly used date and time/numeric functions in MySQL (must read)
  • MySQL gets the current date and time function
  • An article to deal with Mysql date and time functions

<<:  In-depth understanding of uid and gid in docker containers

>>:  Tutorial on installing mysql5.7.17 via yum on redhat7

Recommend

Uninstalling MySQL database under Linux

How to uninstall MySQL database under Linux? The ...

Teach you how to insert 1 million records into MySQL in 6 seconds

1. Idea It only took 6 seconds to insert 1,000,00...

Implementation of vue+drf+third-party sliding verification code access

Table of contents 1. Background 2. Verification p...

An article teaches you how to use Vue's watch listener

Table of contents Listener watch Format Set up th...

Several ways to implement CSS height changing with width ratio

[Solution 1: padding implementation] principle: I...

Docker installation and deployment example on Linux

After reading the following article, you can depl...

Markup Languages ​​- Lists Again

Click here to return to the 123WORDPRESS.COM HTML ...

How to quickly build an FTP file service using FileZilla

In order to facilitate the storage and access of ...

Lambda expression principles and examples

Lambda Expressions Lambda expressions, also known...

Summary of MySQL5 green version installation under Windows (recommended)

1 Download MySQL Download address: http://downloa...

Detailed tutorial on deploying Jenkins based on docker

0. When I made this document, it was around Decem...