当前位置:网站首页>From in MySQL_ Unixtime and UNIX_ Timestamp processing database timestamp conversion - Case

From in MySQL_ Unixtime and UNIX_ Timestamp processing database timestamp conversion - Case

2022-06-22 09:49:00 JackieDYH

MySQL Data sheet design in progress , Time fields are generally designed in timestamp format , It is a little inconvenient for developers to check . have access to FROM_UNIXTIME Convert to date format for viewing

If it is in date format , To convert to timestamp view , Only need to FROM_UNIXTIME Switch to UNIX_TIMESTAMP that will do

Case a  

SELECT *,FROM_UNIXTIME(create_time) AS DATETIME FROM ebk_cls_io_log;

Case 2  

select  from_unixtime(at2.payTime, '%Y-%m-%d'), sum(at2.amount) from alipay_trade at2 group by from_unixtime(at2.payTime, '%Y-%m-%d');

 


Summary  

1、 Time to time stamp
Get the current timestamp ( second )

select unix_timestamp();
 result :1599665897

Get the date timestamp in the specified format

select unix_timestamp('2020-9-9');
 result :1599580800

Get the timestamp of the specified format date and timestamp

select unix_timestamp('2020-9-9 23:49:20');
 result :1599666560

2、 Time stamp to time
grammar :FROM_UNIXTIME(unix_timestamp,format)
Time stamp (unix_timestamp) by 10 position

select from_unixtime(1599666560,'%Y-%m-%d %H:%i:%S');
 result :2020-09-09 23:49:20
Format describe Example Running results
%a English abbreviation week name SELECT FROM_UNIXTIME(1599666560,’%a’);Tue
%b English abbreviation month name SELECT FROM_UNIXTIME( Now( ), ‘%b’ )Sep
%c month , The number SELECT FROM_UNIXTIME( Now( ), ‘%c’ )9
%D The day of the month with English prefix SELECT FROM_UNIXTIME( Now( ), ‘%D’ )9th
%d Day of the month , The number (00-31)SELECT FROM_UNIXTIME( Now( ), '%d ’ )09
%e Day of the month , The number (0-31)SELECT FROM_UNIXTIME( Now( ), '%e ’ )9
%f Microsecond SELECT FROM_UNIXTIME(1599666560 , '%f ’ )000000
%H Hours (00-23)SELECT FROM_UNIXTIME(1599666560 , '%H ’ )23
%h Hours (01-12)SELECT FROM_UNIXTIME(1599666560 , '%h ’ )11
%I Hours (01-12)SELECT FROM_UNIXTIME(1599666560, ‘%I’ )11
%i minute , The number (00-59)SELECT FROM_UNIXTIME(1599666560, ‘%i’ )49
%j Days of (001-366)SELECT FROM_UNIXTIME(1599666560, ‘%j’ )253
%k Hours (0-23)SELECT FROM_UNIXTIME(1599666560, ‘%k’ )23
%l Hours (1-12)SELECT FROM_UNIXTIME(1599666560, ‘%l’ )11
%M month , english SELECT FROM_UNIXTIME(1599666560, ‘%M’ )September
%m month , The number (00-12)SELECT FROM_UNIXTIME(1599666560, ‘%m’ )09
%p In the afternoon SELECT FROM_UNIXTIME(1599666560, ‘%p’ )PM
%r Time ,12- Hours (hh:mm:ss AM or PM)SELECT FROM_UNIXTIME(1599666560, ‘%r’ )11:49:20 PM
%S second (00-59)SELECT FROM_UNIXTIME(1599666560, ‘%S’ )20
%s second (00-59)SELECT FROM_UNIXTIME(1599666560, ‘%s’ )20
%T Time , 24- Hours (hh:mm:ss)SELECT FROM_UNIXTIME(1599666560, ‘%T’ )23:49:20
%U Zhou (00-53) Sunday is the first day of the week SELECT FROM_UNIXTIME(1599666560, ‘%U’ )36
%u Zhou (00-53) Monday is the first day of the week SELECT FROM_UNIXTIME(1599666560, ‘%u’ )37
%V Zhou (01-53) Sunday is the first day of the week , And %X Use SELECT FROM_UNIXTIME(1599666560, ‘%V’ )36
%v Zhou (01-53) Monday is the first day of the week , And %x Use SELECT FROM_UNIXTIME(1599666560, ‘%v’ )37
%W What day of the week , English full name SELECT FROM_UNIXTIME(1599666560, ‘%W’ )Wednesday
%w Days of the week (0= Sunday , 6= Saturday )SELECT FROM_UNIXTIME(1599666560, ‘%w’ )3
%X year ,4 position , Sunday is the first day of the week , And %V Use SELECT FROM_UNIXTIME(1599666560, ‘%X’ )2020
%x year ,4 position , Monday is the first day of the week , And %v Use SELECT FROM_UNIXTIME(1599666560, ‘%x’ )2020
%Y year ,4 position SELECT FROM_UNIXTIME(1599666560, ‘%Y’ )2020
%y year ,2 position SELECT FROM_UNIXTIME(1599666560, ‘%y’ )20

原网站

版权声明
本文为[JackieDYH]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220933263412.html