mysql time operation (time difference and conversion of timestamp and time string)

mysql time operation (time difference and conversion of timestamp and time string)

 

Two time differences:

MySQL datediff (date1, date2): subtract date1 - date2 from two dates and return the number of days.
 select  datediff ( ' 2008-08-08 ' , ' 2008-08-01 ' ); -- 7 
 select  datediff ( ' 2008-08-01 ' , ' 2008-08-08 ' ); -- -7

  
MySQL timediff(time1,time2): Subtract time1 - time2 from two dates and return the time difference.
 select timediff( ' 2008-08-08 08:08:08 ' , ' 2008-08-08 00:00:00 ' ); -- 08:08:08 
 select timediff( ' 08:08:08 ' , ' 00 :00:00 ' ); -- 08:08:08

  
Note: The two parameter types of the timediff(time1,time2) function must be the same.

 

 

Functions involved in the conversion of timestamps and time strings

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 format function from_unixtime

 

time to string

select date_format(now(), '%Y-%m-%d');  
  
#Results : 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');  
  
#Results : 2016-01-02 00:00:00 _ _ _ _ _ _ _   

String to Timestamp

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

Timestamp to time

select from_unixtime(1451997924);  
  
#Results : 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 (format) range of values.

  value meaning
Second %S、%s Seconds as two digits ( 00,01, ..., 59)
Minute %I、%i Two-digit minute ( 00,01, ..., 59)
Hour  %H 24-hour clock, two-digit hour (00,01, ...,23)
%h 12-hour clock, two-digit hour (00,01,...,12)
%k 24-hour clock, number hours (0,1,...,23)
%l 12-hour clock, hours in number form (0,1,...,12)
%T 24-hour clock, time format (HH:mm:ss)
%r  12-hour clock, time format (hh:mm:ss AM or PM)
%p  AM AM or PM PM 
  week   %W The name of each day of the week (Sunday,Monday, ...,Saturday)
 %a Abbreviation of the name of each day of the week (Sun,Mon,...,Sat) 
%w  Identifies the week as a number (0=Sunday, 1=Monday, ..., 6=Saturday) 
% U The number indicates the week number, with Sunday being the first day of the week
% u The number indicates the week number, with Monday being the first day of the week
sky %d  Two digits for the day of the month (01,02,...,31)
%e   Numbers indicate the number of days in the month (1,2,...,31)
 %D The English suffix indicates the number of days in the month (1st, 2nd, 3rd...) 
 %j Day of the year as three digits (001,002, ...,366) 
moon %M  English month name (January,February,...,December) 
%b  Abbreviated month name (Jan, Feb, ..., Dec) 
%m  Two digits for month (01,02,...,12)
%c  Numbers represent months (1,2,...,12) 
year %AND  Four-digit year (2015, 2016...)
%and   Two-digit year (15,16...)
text output  %Word  direct output text

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324734675&siteId=291194637