mysql中時間日期格式化

2021-08-31 14:30:57 字數 2370 閱讀 2452

還可以在年份、月份、日間加上「-」 以便格式化成如下形式:2011-3-9,這種格式還是用的蠻多的!

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中時間日期格式化

還可以在年份 月份 日間加上 以便格式化成如下形式 2011 3 9,這種格式還是用的蠻多的!time format time,format 這象上面的date format 函式一樣使用,但是format字串只能包含處理小時 分鐘和秒的那些格式修飾符。其他修飾符產生乙個null值或0。curdat...

mysql中時間日期格式化

還可以在年份 月份 日間加上 以便格式化成如下形式 2011 3 9,這種格式還是用的蠻多的!time format time,format 這象上面的date format 函式一樣使用,但是format字串只能包含處理小時 分鐘和秒的那些格式修飾符。其他修飾符產生乙個null值或0。curdat...

mysql當前時間,日期轉換,日期格式化

當前時間 select now sysdate current timestamp localtime localtimestamp select curdate curtime 時間格式化 select date format now y c d h i s select date format ...