Mysql日期和時間函式不求人

2021-04-01 06:08:04 字數 3627 閱讀 1194

date_format(date,format)

根據format字串格式化date值。下列修飾符可以被用在format字串中: %m 月名字(january……december)

%w 星期名字(sunday……saturday)

%d 有英語字首的月份的日期(1st, 2nd, 3rd, 等等。)

%y 年, 數字, 4 位

%y 年, 數字, 2 位

%a 縮寫的星期名字(sun……sat)

%d 月份中的天數, 數字(00……31)

%e 月份中的天數, 數字(0……31)

%m 月, 數字(01……12)

%c 月, 數字(1……12)

%b 縮寫的月份名字(jan……dec)

%j 一年中的天數(001……366)

%h 小時(00……23)

%k 小時(0……23)

%h 小時(01……12)

%i 小時(01……12)

%l 小時(1……12)

%i 分鐘, 數字(00……59)

%r 時間,12 小時(hh:mm:ss [ap]m)

%t 時間,24 小時(hh:mm:ss)

%s 秒(00……59)

%s 秒(00……59)

%p am或pm

%w 乙個星期中的天數(0=sunday ……6=saturday )

%u 星期(0……52), 這裡星期天是星期的第一天

%u 星期(0……52), 這裡星期一是星期的第一天

%% 乙個文字「%」。

所有的其他字元不做解釋被複製到結果中。

mysql> select date_format('1997-10-04 22:23:00', '%w %m %y');

-> 'saturday october 1997'

mysql> select date_format('1997-10-04 22:23:00', '%h:%i:%s');

-> '22:23:00'

mysql> select date_format('1997-10-04 22:23:00',

'%d %y %a %d %m %b %j');

-> '4th 97 sat 04 10 oct 277'

mysql> select date_format('1997-10-04 22:23:00',

'%h %k %i %r %t %s %w');

-> '22 22 10 10:23:00 pm 22:23:00 00 6'

mysql3.23中,在格式修飾符字元前需要%。在mysql更早的版本中,%是可選的。

time_format(time,format)

這象上面的date_format()函式一樣使用,但是format字串只能包含處理小時、分鐘和秒的那些格式修飾符。

其他修飾符產生乙個null值或0。

curdate()

current_date

以'yyyy-mm-dd'或yyyymmdd格式返回今天日期值,取決於函式是在乙個字串還是數字上下文被使用。

mysql> select curdate();

-> '1997-12-15'

mysql> select curdate() + 0;

-> 19971215

curtime()

current_time

以'hh:mm:ss'或hhmmss格式返回當前時間值,取決於函式是在乙個字串還是在數字的上下文被使用。

mysql> select curtime();

-> '23:50:26'

mysql> select curtime() + 0;

-> 235026

now()

sysdate()

current_timestamp

以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回當前的日期和時間,取決於函式是在乙個字串還是在數字的

上下文被使用。

mysql> select now();

-> '1997-12-15 23:50:26'

mysql> select now() + 0;

-> 19971215235026

unix_timestamp()

unix_timestamp(date)

如果沒有引數呼叫,返回乙個unix時間戳記(從'1970-01-01 00:00:00'gmt開始的秒數)。如果unix_timestamp()用乙個date引數被呼叫,它返回從'1970-01-01 00:00:00' gmt開始的秒數值。date可以是乙個date字串、乙個datetime字串、乙個timestamp或以yymmdd或yyyymmdd格式的本地時間的乙個數字。

mysql> select unix_timestamp();

-> 882226357

mysql> select unix_timestamp('1997-10-04 22:23:00');

-> 875996580

當unix_timestamp被用於乙個timestamp列,函式將直接接受值,沒有隱含的「string-to-unix-timestamp」變換。

from_unixtime(unix_timestamp)

以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回unix_timestamp引數所表示的值,取決於函式是在乙個字串還是或數字上下文中被使用。

mysql> select from_unixtime(875996580);

-> '1997-10-04 22:23:00'

mysql> select from_unixtime(875996580) + 0;

-> 19971004222300

from_unixtime(unix_timestamp,format)

返回表示 unix 時間標記的乙個字串,根據format字串格式化。format可以包含與date_format()函式列出的條目同樣的修飾符。

mysql> select from_unixtime(unix_timestamp(),

'%y %d %m %h:%i:%s %x');

-> '1997 23rd december 03:43:30 x'

sec_to_time(seconds)

返回seconds引數,變換成小時、分鐘和秒,值以'hh:mm:ss'或hhmmss格式化,取決於函式是在乙個字串還是在數字上下文中被使用。

mysql> select sec_to_time(2378);

-> '00:39:38'

mysql> select sec_to_time(2378) + 0;

-> 3938

time_to_sec(time)

返回time引數,轉換成秒。

mysql> select time_to_sec('22:23:00');

-> 80580

mysql> select time_to_sec('00:39:38');

-> 2378

Mysql日期和時間函式不求人

date format date,format 根據format字串格式化date值。下列修飾符可以被用在format字串中 m 月名字 january december w 星期名字 sunday saturday d 有英語字首的月份的日期 1st,2nd,3rd,等等。y 年,數字,4 位 y...

Mysql日期和時間函式

date format date,format 根據format字串格式化date值。下列修飾符可以被用在format字串中 m 月名字 january december w 星期名字 sunday saturday d 有英語字首的月份的日期 1st,2nd,3rd,等等。y 年,數字,4 位 y...

MySQL日期和時間函式

1 獲取當前日期的函式和獲取當前時間的函式 curdate 0把時間變成數字 select curdate current date curdate 0,now 結果2020 08 22 2020 08 22 20200822 2020 08 22 19 11 35select current ti...