avatar

麦兜的小站

MDO.INK

  • 首页
  • 随笔
  • 知识库
  • 归档
  • 动态
  • 标签
  • 关于
Home mysql时间日期函数
文章

mysql时间日期函数

Posted 2025-01-10 Updated 2025-01- 10
By power 已删除用户
14~18 min read

now(), current_timestamp(); -- 当前日期时间 current_date(); -- 当前日期 current_time(); -- 当前时间 date('yyyy-mm-dd hh:ii:ss'); -- 获取日期部分 time('yyyy-mm-dd hh:ii:ss'); -- 获取时间部分 date_format('yyyy-mm-dd hh:ii:ss', '%d %y %a %d %m %b %j'); -- 格式化时间 unix_timestamp(); -- 获得unix时间戳

from_unixtime(); -- 从时间戳获得时间

 select now(),current_date,current_time;

格式:

查询每个月1号

select * from lagouok where right(date_format([字段],'%Y-%m-%d'),2)=1

获取星期几

select dayofweek(now())-1

dayofweek(字段)=1 查询每个月星期二

select year(curdate())-year(字段)-(right(curdate(),5)<right(字段,5)) 年龄 from emp;

查询多少年前select date_sub(sysdate(),interval 10 year)

 

date_format([字段名称],'%Y-%m-%d') = curdate()

 

1、当前日期

select DATE_SUB(curdate(),INTERVAL 0 DAY) ;

2、明天日期
select DATE_SUB(curdate(),INTERVAL -1 DAY) ;

3、昨天日期

select adddate(now(),-1)
select date_sub(now(),interval 1 day)

 

4、前一个小时时间

select date_sub(now(), interval 1 hour);

5、后一个小时时间

select date_sub(now(), interval -1 hour);

6、前30分钟时间

select date_add(now(),interval -30 minute)

7、后30分钟时间

select date_add(now(),interval 30 minute)

8、根据format字符串格式化date值:

%S, %s  两位数字形式的秒( 00,01, …, 59)
%I, %i  两位数字形式的分( 00,01, …, 59)
%H  两位数字形式的小时,24 小时(00,01, …, 23)
%h  两位数字形式的小时,12 小时(01,02, …, 12)
%k  数字形式的小时,24 小时(0,1, …, 23)
%l  数字形式的小时,12 小时(1, 2, …, 12)
%T  24 小时的时间形式(hh:mm:ss)
%r  12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)
%p  AM或PM
%W  一周中每一天的名称(Sunday, Monday, …, Saturday)
%a  一周中每一天名称的缩写(Sun, Mon, …, Sat)
%d  两位数字表示月中的天数(00, 01,…, 31)
%e  数字形式表示月中的天数(1, 2, …, 31)
%D  英文后缀表示月中的天数(1st, 2nd, 3rd,…)
%w  以数字形式表示周中的天数( 0 = Sunday, 1=Monday, …, 6=Saturday)
%j  以三位数字表示年中的天数( 001, 002, …, 366)
%U  周(0, 1, 52),其中Sunday 为周中的第一天
%u  周(0, 1, 52),其中Monday 为周中的第一天
%M  月名(January, February, …, December)
%b  缩写的月名( January, February,…., December)
%m  两位数字表示的月份(01, 02, …, 12)
%c  数字表示的月份(1, 2, …., 12)
%Y  四位数字表示的年份
%y  两位数字表示的年份
%%  直接值“%”

知识库
License:  CC BY 4.0
Share

Further Reading

Jul 31, 2025

如何实现接口幂等性

通俗的说,用户在系统中有操作,不管重复多少次,都应该产生一样的效果或返回一样的结果的。 幂等性的概念 幂等(Idempotent)是一个数学与计算机学的概念,常见于抽象代数中。 f(n)=1^n//无...

Jul 19, 2025

10个npm工具包

有了npm之后,前端人员真的是过上好日子了。我们可以直接把别人写好的工具包拿来用,非常的方便。 1.day.js-轻量日期处理 npminstalldayjs importdayjsfrom'd...

Jul 17, 2025

How to set up PHP7.4 on MacOS.

Thisisallverywellandgood.Apartfromonesmallinsignificantthing… TheversionofPHPinuseiscurrently7.4. Th...

OLDER

开发 vscode 插件却不知从何入手?

NEWER

MySQL误删数据怎么办?

Recently Updated

  • 如何实现接口幂等性
  • 10个npm工具包
  • How to set up PHP7.4 on MacOS.
  • Automa:一键自动化,网页数据采集与工作流程优化专家Automa:解锁自动化
  • Mac 下用 brew 搭建 LNMP

Trending Tags

thinkphp clippings

Contents

©2025 麦兜的小站. Some rights reserved.

Using the Halo theme Chirpy