数据库
首页 > 数据库> > mysql 时间戳转换 今天、昨天、7天、本月、上月

mysql 时间戳转换 今天、昨天、7天、本月、上月

作者:互联网

下面的时间范围查询都是前闭后开,如 $time_start <= time < $time_end

if $day_index == 1 then #今天数据

set $time_start = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 0 DAY),INTERVAL 0 HOUR));
set $time_end = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 0 DAY),INTERVAL 24 HOUR));

elseif $day_index == 2 then #昨天数据

set $time_start = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 1 DAY),INTERVAL 0 HOUR));
set $time_end = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 1 DAY),INTERVAL 24 HOUR));

elseif $day_index == 3 then #7天数据

set $time_start = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 6 DAY),INTERVAL 0 HOUR)); #6天前
set $time_end = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 0 DAY),INTERVAL 24 HOUR)); #今天结束

elseif $day_index == 4 then #本月数据

set $time_start = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(curdate(), INTERVAL 0 MONTH),INTERVAL 0 DAY)); #本月开始
set $time_end = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 0 DAY),INTERVAL 24 HOUR)); #今天结束

elseif $day_index == 5 then #上月数据

set $time_start = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(curdate(), INTERVAL 1 MONTH),INTERVAL 0 DAY)); #上月开始
set $time_end = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(curdate(), INTERVAL 0 MONTH),INTERVAL 0 DAY)); #本月开始

end if;

标签:set,转换,SUB,mysql,INTERVAL,本月,time,DATE,DAY
来源: https://blog.csdn.net/oYuLinZuo/article/details/110442359