MySQL返回特定格式时间日期
MySQL 返回星期和月份名称
select dayname(sysdate()), monthname(now());
返回中文名
set global lc_time_names = 'zh_CN';
SET @@lc_time_names = 'zh_CN';
返回英文
set global lc_time_names = 'en_US';
SET @@lc_time_names = 'en_US';
返回当前时间月份中的最后一天
select last_day(now());
MySQL Extract() 函数
select extract(year from now()); — 2008
select extract(quarter from now()); — 3
select extract(month from now()); — 9
select extract(week from now()); — 36
select extract(day from now()); — 10
select extract(hour from now()); — 7
select extract(minute from now()); — 15
select extract(second from now()); — 30
select extract(microsecond from now()); — 123456
select extract(year_month from now()); — 200809
select extract(day_hour from now()); — 1007
select extract(day_minute from now()); — 100715
select extract(day_second from now()); — 10071530
select extract(day_microsecond from now()); — 10071530123456
select extract(hour_minute from now()); — 715
select extract(hour_second from now()); — 71530
select extract(hour_microsecond from now()); — 71530123456
select extract(minute_second from now()); — 1530
select extract(minute_microsecond from now()); — 1530123456
select extract(second_microsecond from now()); — 30123456
select period_add(200808,2), period_add(20080808,-2);
select period_diff(200808, 200801);
MySQL 日期、时间相减
datediff(now(),sysdate()), timediff(time1,time2);
MySQL 拼凑日期、时间
makdedate(year,dayofyear), maketime(hour,minute,second);
获取时间格式
select get_format(date|time|datetime, 'eur'|'usa'|'jis'|'iso'|'internal') ;
获取当前时间戳
select current_timestamp, current_timestamp();
MySQL (Unix 时间戳、日期)转换函数
select unix_timestamp(),unix_timestamp(now());
select from_unixtime(1218290027);
select from_unixtime(1218169800, ‘%Y %D %M %h:%i:%s %x’);
week(now(),0)
weekofyear(now())
SHOW VARIABLES LIKE 'default_week_format';
模式 一周的第一天 范围
0 星期日 0-53
1 星期一 0-53
2 星期日 1-53
3 星期一 1-53
4 星期日 0-53
5 星期一 0-53
6 星期日 1-53
7 星期一 1-53
查看系统时区
select @@global.time_zone,@@session.time_zone;
show variables like 'system_time_zone';
select now()+0,now()+1;
select now(2);
注意:
SYSDATE( ):返回服务器的当前日期和时间
与now的不同点:(一般使用NOW而不用SYSDATE)
①SYSDATE()返回的是函数执行时的时间
②now()返回的是语句执行时的时间
三个时间函数效果一样
select CURRENT_TIME,CURTIME(),CURRENT_TIME();
select CURRENT_DATE,CURDATE(),CURRENT_DATE();