《MysqL教程MysqL日期时间函数大全 MysqL函数大全》要点:
本文介绍了MysqL教程MysqL日期时间函数大全 MysqL函数大全,希望对您有用。如果有疑问,可以联系我们。
MysqL必读本节内容:
MysqL日期时间函数,MysqL函数大全
MysqL必读dayofweek(date)
返回日期date是星期几(1=星期天,odbc标准)
MysqL> select dayofweek('1998-02-03');
-> 3
weekday(date)
返回日期date是星期几(0=星期一,1=星期二,……6= 星期天).
MysqL> select weekday('1997-10-04 22:23:00');
-> 5
MysqL> select weekday('1997-11-05');
-> 2
dayofmonth(date)
返回date是一月中的第几日(在1到31范围内)
MysqL> select dayofmonth('1998-02-03');
-> 3
dayofyear(date)
返回date是一年中的第几日(在1到366范围内)
MysqL> select dayofyear('1998-02-03');
-> 34
month(date)
返回date中的月份数值
MysqL> select month('1998-02-03');
-> 2
dayname(date)
返回date是星期几(按英文名返回)
MysqL> select dayname("1998-02-05");
-> 'thursday'
monthname(date)
返回date是几月(按英文名返回)
MysqL> select monthname("1998-02-05");
-> 'february'
quarter(date)
返回date是一年的第几个季度
MysqL> select quarter('98-04-01');
-> 2
week(date,first)
返回date是一年的第几周(first默认值0,first取值1表示周一是周的开始,0从周日开始)
MysqL> select week('1998-02-20');
-> 7
MysqL> select week('1998-02-20',0);
-> 7
MysqL> select week('1998-02-20',1);
-> 8
year(date)
返回date的年份(范围在1000到9999)
MysqL> select year('98-02-03');
-> 1998
hour(time)
返回time的小时数(范围是0到23)
MysqL> select hour('10:05:03');
-> 10
minute(time)
返回time的分钟数(范围是0到59)
MysqL> select minute('98-02-03 10:05:03');
-> 5
second(time)
返回time的秒数(范围是0到59)
MysqL> select second('10:05:03');
-> 3
period_add(p,n)
增加n个月到时期p并返回(p的格式yymm或yyyymm)
MysqL> select period_add(9801,2);
-> 199803
period_diff(p1,p2)
返回在时期p1和p2之间月数(p1和p2的格式yymm或yyyymm)
MysqL> select period_diff(9802,199703);
-> 11
date_add(date,interval expr type)
date_sub(date,interval expr type)
adddate(date,interval expr type)
subdate(date,interval expr type)
对日期时间进行加减法运算
(adddate()和subdate()是date_add()和date_sub()的同义词,也可以用运算符 和-而不是函数
date是一个datetime或date值,expr对date进行加减法的一个表达式字符串type指明表达式expr应该如何被解释
[type值 含义 期望的expr格式]:
second 秒 seconds
minute 分钟 minutes
hour 时间 hours
day 天 days
month 月 months
year 年 years
minute_second 分钟和秒 "minutes:seconds"
hour_minute 小时和分钟 "hours:minutes"
day_hour 天和小时 "days hours"
year_month 年和月 "years-months"
hour_second 小时,分钟,"hours:minutes:seconds"
day_minute 天,小时,分钟 "days hours:minutes"
day_second 天,秒 "days hours:minutes:seconds"
expr中允许任何标点做分隔符,如果所有是date值时结果是一个date值,否则结果是一个datetime值)
如果type关键词不完整,则MysqL从右端取值,day_second因为缺少小时分钟等于minute_second)
如果增加month、year_month或year,天数大于结果月份的最大天数则使用最大天数)
MysqL> select "1997-12-31 23:59:59" interval 1 second;
-> 1998-01-01 00:00:00
MysqL> select interval 1 day "1997-12-31";
-> 1998-01-01
MysqL> select "1998-01-01" - interval 1 second;
-> 1997-12-31 23:59:59
MysqL> select date_add("1997-12-31 23:59:59",interval 1 second);
-> 1998-01-01 00:00:00
MysqL> select date_add("1997-12-31 23:59:59",interval 1 day);
-> 1998-01-01 23:59:59
MysqL> select date_add("1997-12-31 23:59:59",interval "1:1" minute_second);
-> 1998-01-01 00:01:00
MysqL> select date_sub("1998-01-01 00:00:00",interval "1 1:1:1" day_second);
-> 1997-12-30 22:58:59
MysqL> select date_add("1998-01-01 00:00:00",interval "-1 10" day_hour);
-> 1997-12-30 14:00:00
MysqL> select date_sub("1998-01-02",interval 31 day);
-> 1997-12-02
MysqL> select extract(year from "1999-07-02");
-> 1999
MysqL> select extract(year_month from "1999-07-02 01:02:03");
-> 199907
MysqL> select extract(day_minute from "1999-07-02 01:02:03");
-> 20102
to_days(date)
返回日期date是西元0年至今多少天(不计算1582年以前)
MysqL> select to_days(950501);
-> 728779
MysqL> select to_days('1997-10-07');
-> 729669
from_days(n)
给出西元0年至今多少天返回date值(不计算1582年以前)
MysqL> select from_days(729669);
-> '1997-10-07'
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'
time_format(time,format)
和date_format()类似,但time_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开始的秒数,date默认值为当前时间)
MysqL> select unix_timestamp();
-> 882226357
MysqL> select unix_timestamp('1997-10-04 22:23:00');
-> 875996580
from_unixtime(unix_timestamp)
以'yyyy-mm-dd hh:mm:ss'或yyyymmddhhmmss格式返回时间戳的值(根据返回值所处上下文是字符串或数字)
MysqL> select from_unixtime(875996580);
-> '1997-10-04 22:23:00'
MysqL> select from_unixtime(875996580) 0;
-> 19971004222300
from_unixtime(unix_timestamp,format)
以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)
以'hh:mm:ss'或hhmmss格式返回秒数转成的time值(根据返回值所处上下文是字符串或数字)
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
内存溢出PHP培训学院每天发布《MysqL教程MysqL日期时间函数大全 MysqL函数大全》等实战技能,PHP、MysqL、liNUX、APP、Js,CSS全面培养人才。
总结以上是内存溢出为你收集整理的MYSQL教程mysql日期时间函数大全 mysql函数大全全部内容,希望文章能够帮你解决MYSQL教程mysql日期时间函数大全 mysql函数大全所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)