Mysql date time string format conversion interconversion

Function involved

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

unix_timestamp () function

str_to_date (str, format) function

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

Time to String

  1. select date_format(now(), '%Y-%m-%d');  
  2.   
  3. # Result: 2017-01-05  

Time to turn the timestamp

  1. select unix_timestamp(now());  
  2.   
  3. # Result: 1452001082  

String transfer time

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

String turn timestamp

  1. select unix_timestamp('2016-01-02');  
  2.   
  3. # Result: 1451664000  

Timestamp turn time

  1. select from_unixtime(1451997924);  
  2.   
  3. # Result: 2017-01-05 20:45:24  

Timestamp to String

  1. select from_unixtime(1451997924,'%Y-%d');  
  2.   
  3. // Results: 2017-01-05 20:45:24  

Schedule

MySQL date format (the format) range.

  value meaning
second %S、%s Sec (00, 01, ..., 59) in the form of two digits
Minute %I、%i Two digital form of points (00, 01, ..., 59)
hour  %H 24-hour, two-digit hour form (00, 01, ..., 23)
%h 12-hour, two-digit hour form (00, 01, ..., 12)
%k 24-hour clock, the number of hours in the form of (0, 1, ..., 23)
%l 12 hour, the number of hours in the form of (0, 1, ..., 12)
%T 24-hour time form (HH: mm: ss)
%r  12-hour time form (hh: mm: ss AM or PM)
%p  Morning or afternoon AM PM 
  week   %W Every day of the week name (Sunday, Monday, ..., Saturday)
 %a Every day of the week abbreviated name (Sun, Mon, ..., Sat) 
%w  Peripheral identity in digital form (0 = Sunday, 1 = Monday, ..., 6 = Saturday) 
% U Number indicates the number of weeks, the first day of the week is Sunday
in% Number indicates the number of weeks, Monday is the first day of the week
day %d  Two digits indicate the number of months of days (01, 02, ..., 31)
%e   Month number indicates the number of days (1, 2, ..., 31)
 %D English suffix indicates the number of days month (1st, 2nd, 3rd ...) 
 %j Three digits indicate the number of years (001, 002, ..., 366) transit 
month %M  English name of the month (January, February, ..., December) 
%b  English abbreviations month name (Jan, Feb, ..., Dec) 
%m  Two digits indicate the month (01, 02, ..., 12)
%c  Digits indicate the month (1, 2, ..., 12) 
year %Y  Year (2015,2016 ...) four digits
%Y   Year (15, 16 ...) of two digits
Text output  Text%  Direct output text


Source:
https://www.cnblogs.com/cnsdhzzl/p/9850412.html

Function involved

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

unix_timestamp () function

str_to_date (str, format) function

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

Time to String

  1. select date_format(now(), '%Y-%m-%d');  
  2.   
  3. # Result: 2017-01-05  

Time to turn the timestamp

  1. select unix_timestamp(now());  
  2.   
  3. # Result: 1452001082  

String transfer time

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

String turn timestamp

  1. select unix_timestamp('2016-01-02');  
  2.   
  3. # Result: 1451664000  

Timestamp turn time

  1. select from_unixtime(1451997924);  
  2.   
  3. # Result: 2017-01-05 20:45:24  

Timestamp to String

  1. select from_unixtime(1451997924,'%Y-%d');  
  2.   
  3. // Results: 2017-01-05 20:45:24  

Schedule

MySQL date format (the format) range.

  value meaning
second %S、%s Sec (00, 01, ..., 59) in the form of two digits
Minute %I、%i Two digital form of points (00, 01, ..., 59)
hour  %H 24-hour, two-digit hour form (00, 01, ..., 23)
%h 12-hour, two-digit hour form (00, 01, ..., 12)
%k 24-hour clock, the number of hours in the form of (0, 1, ..., 23)
%l 12 hour, the number of hours in the form of (0, 1, ..., 12)
%T 24-hour time form (HH: mm: ss)
%r  12-hour time form (hh: mm: ss AM or PM)
%p  Morning or afternoon AM PM 
  week   %W Every day of the week name (Sunday, Monday, ..., Saturday)
 %a Every day of the week abbreviated name (Sun, Mon, ..., Sat) 
%w  Peripheral identity in digital form (0 = Sunday, 1 = Monday, ..., 6 = Saturday) 
% U Number indicates the number of weeks, the first day of the week is Sunday
in% Number indicates the number of weeks, Monday is the first day of the week
day %d  Two digits indicate the number of months of days (01, 02, ..., 31)
%e   Month number indicates the number of days (1, 2, ..., 31)
 %D English suffix indicates the number of days month (1st, 2nd, 3rd ...) 
 %j Three digits indicate the number of years (001, 002, ..., 366) transit 
month %M  English name of the month (January, February, ..., December) 
%b  English abbreviations month name (Jan, Feb, ..., Dec) 
%m  Two digits indicate the month (01, 02, ..., 12)
%c  Digits indicate the month (1, 2, ..., 12) 
year %Y  Year (2015,2016 ...) four digits
%Y   Year (15, 16 ...) of two digits
Text output  Text%  Direct output text


Source:
https://www.cnblogs.com/cnsdhzzl/p/9850412.html

Guess you like

Origin www.cnblogs.com/liyunchuan/p/11285269.html