Mysql 获取当天、昨天、上周、本周、上月、本月的时间

当天:

SELECT DATE_FORMAT(CURDATE(),'%Y-%m-%d 00:00:00')
SELECT DATE_FORMAT(CURDATE(),'%Y-%m-%d 23:59:59')

昨天:

SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 DAY), '%Y-%m-%d 00:00:00')
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 DAY), '%Y-%m-%d 23:59:59')

上周:

SELECT DATE_FORMAT(DATE_SUB(DATE_SUB(CURDATE(),INTERVAL WEEKDAY(CURDATE()) DAY),INTERVAL 1 WEEK),'%Y-%m-%d 00:00:00')
SELECT DATE_FORMAT(SUBDATE(CURDATE(), WEEKDAY(CURDATE()) + 1), '%Y-%m-%d 23:59:59')

本周:

SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY), '%Y-%m-%d 00:00:00')
SELECT DATE_FORMAT( DATE_ADD(SUBDATE(CURDATE(), WEEKDAY(CURDATE())), INTERVAL 6 DAY), '%Y-%m-%d 23:59:59')

上月:

SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01 00:00:00')
SELECT DATE_FORMAT( LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)), '%Y-%m-%d 23:59:59')

本月:

SELECT DATE_FORMAT(CURDATE(), '%Y-%m-01 00:00:00')
SELECT DATE_FORMAT(LAST_DAY(CURDATE()), '%Y-%m-%d 23:59:59')

某个月有多少天:

SELECT DAY(LAST_DAY('2018-06-01'));

你可能感兴趣的:(Mysql 获取当天、昨天、上周、本周、上月、本月的时间)