mysql日期格式转换函数有哪些

发布时间:2025-11-06 点击:5
mysql日期格式转换函数有:1、【sec_to_time(seconds)】把秒数转化成时间;2、【time_to_sec(time)】把时间转化成秒数;3、【addtime(time,times)】把times加到time上。
【相关学习推荐:mysql教程(视频)】
mysql日期格式转换函数有:
1、获取系统当前日期
select curdate() select current_date()-> 2016-01-16-> 2016-01-162、获取系统当前时间
select curtime() select current_time()-> 17:44:22-> 17:44:223、now(),sysdate(),current_timestamp(),localtime():获取系统当前日期和时间
select now() select sysdate() select current_timestamp() select current_timestamp select localtime() select localtime-> 2016-01-16 17:44:41-> 2016-01-16 17:44:41-> 2016-01-16 17:44:41-> 2016-01-16 17:44:41-> 2016-01-16 17:44:41-> 2016-01-16 17:44:414、unix_timestamp(date):获取时间戳
select unix_timestamp() select unix_timestamp(‘2016-01-16') select unix_timestamp(‘2016-01-16 23:59:59')-> 1452937627-> 1452873600-> 14529599995、from_unixtime(unix_timestamp,format):把时间戳转化成日期时间
select from_unixtime(1452959999) select from_unixtime(1452959999,'%y-%m-%d %h:%i:%s')-> 2016-01-16 23:59:59-> 2016-01-16 23:59:596、sec_to_time(seconds):把秒数转化成时间
select sec_to_time(2378)-> 00:39:387、time_to_sec(time):把时间转化成秒数
select time_to_sec(‘22:23:00')-> 23788、addtime(time,times):把times加到time上
select addtime(“2015-12-31 23:59:59”,'01:01:01')-> 2016-01-01 01:01:009、convert_tz(date,from_tz ,to_tz ):转换时区
select convert_tz(‘2004-01-01 12:00:00',' 00:00',' 10:00')-> 2004-01-01 22:00:0010、str_to_date(date,format ):将字符串转成format格式的日期时间
select str_to_date(‘2015-01-01', ‘%y-%m-%d')-> 2015-01-0111、last_day(date ):获取date当月最后一天的日期
select last_day(sysdate()) select last_day(‘2015-02-02') select last_day(‘2015-02-02 00:22:33')-> 2016-01-31-> 2015-02-28-> 2015-02-2812、makedate(year ,dayofyear ):根据参数(年份,第多少天)获取日期
select makedate(2015 ,32)-> 2015-02-0113、 maketime(hour ,minute ,second ):根据参数(时,分,秒)获取时间
select maketime(12 ,23 ,34 )-> 12:23:3414、yearweek(date):获取日期的年和周
select yearweek(sysdate()) select yearweek(‘2015-01-10') select yearweek(‘2015-01-10',1)-> 201602-> 201501-> 20150215、weekofyear(date):获取当日是当年的第几周
select weekofyear(sysdate()) select weekofyear(‘2015-01-10')-> 2-> 2mysql中常用的几种时间格式转换函数整理如下
1,from_unixtime(timestamp, format):
timestamp为int型时间,如14290450779;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), 这里星期天是星期的第一天
61f期(0……52), 这里星期一是星期的第一
2,unix_timestamp(date):
作用与from_unixtime()刚好相反,前者是把unix时间戳转换为可读的时间,而unix_timestamp()是把可读的时间转换为unix时间戳,这在对datetime存储的时间进行排序时会用到。如unix_timestamp('2009-08-06 10:10:40'),得到1249524739。
如果unix_timestamp()不传参数,则调用now()函数自动取当前时间。
3,date_format(date, format):
date_format()是将date或datetime类型值转换为任意的时间格式。比如常见的应用场景,某表有一个字段是更新时间,存储的是datetime类型,但前台展示时只需要显示年月日(xxxx-xx-xx),这个时候就可以用date_format(date,'%y-%m-%d ')处理,而不需要在结果集中用程序循环处理。
想了解更多编程学习,敬请关注php培训栏目!

APP渗透测试的主要步骤
如何将电脑中的视频添加到iTunes资料库中|电脑视频文件添加到iTunes资料库中的方法
腾讯云服务器怎么用xshell
腾讯云服务器不续费有影响吗
云服务器怎么放多个网站吗
做网站的每个阶段分别都要什么开销
比格云服务器怎么用
能帮我看看打不开很不稳定