The SELECT DATE_FORMAT (NOW (), '% % Y - m - % d 00:00:00') AS 'today';
The SELECT DATE_FORMAT (NOW (), '% % Y - m - % d 23:59:59') AS 'end today';
Yesterday -
SELECT DATE_FORMAT (DATE_SUB (CURDATE (), the INTERVAL 1 DAY), '% % m - Y % d 00:00:00') AS 'yesterday'.
SELECT DATE_FORMAT (DATE_SUB (CURDATE (), the INTERVAL 1 DAY), '% % m - Y % d 23:59:59') AS 'end yesterday;
Last week,
SELECT DATE_FORMAT (DATE_SUB (DATE_SUB (CURDATE (), INTERVAL WEEKDAY (CURDATE () DAY), the INTERVAL 1 WEEK), '% % m - Y % d 00:00:00') AS 'last WEEK;
The SELECT DATE_FORMAT (SUBDATE (CURDATE (), WEEKDAY (CURDATE ()) + 1), Y - '% % m - % d 23:59:59') AS 'last weekend;
This week -
The SELECT DATE_FORMAT (SUBDATE (CURDATE (), DATE_FORMAT (CURDATE (), '% w') - 1), Y - '% % m - % d 00:00:00') AS 'Monday';
SELECT DATE_FORMAT (SUBDATE (CURDATE (), DATE_FORMAT (CURDATE (), '% w') - 7), '% % m - Y % d 23:59:59') AS 'this weekend;
- the above algorithm this week there will be a problem because the mysql is according to the Sunday is the first day of a week, if the current is on Sunday, will take time until next week.
SELECT DATE_FORMAT (DATE_SUB (CURDATE (), INTERVAL WEEKDAY (CURDATE () DAY, '% % m - Y % d 00:00:00') AS 'Monday';
SELECT DATE_FORMAT (DATE_ADD (SUBDATE (CURDATE (), WEEKDAY (CURDATE ())), INTERVAL 6 DAY), '% % m - Y % d 23:59:59') AS 'this weekend;
- last month
The SELECT DATE_FORMAT (DATE_SUB (CURDATE (), the INTERVAL 1 MONTH), '% Y - % m - 01 00:00:00) AS' early last MONTH;
SELECT DATE_FORMAT (LAST_DAY (DATE_SUB (CURDATE (), 1 MONTH INTERVAL)), '% % m - Y % d 23:59:59') AS 'the end of last MONTH;
This month -
The SELECT DATE_FORMAT (CURDATE (), '% Y - % m - 01 00:00:00) AS' the beginning of this month;
The SELECT DATE_FORMAT (LAST_DAY (CURDATE ()), '% % m - Y % d 23:59:59') AS 'at the end of this month;
CodePudding user response:
Thanks for sharing theCodePudding user response:
Ditto ~ thanks for sharing ~ markCodePudding user response:
Thanks for sharing, the main use mysql DATE_FORMAT (date, format) function,