简体   繁体   中英

How to get next week monday to sunday in my sql

I am using below code for fetching next monday but i am unable to fetch next sunday on same week. like today is friday and if i search next week then i need to get next week monday to sunday.

SELECT now() + INTERVAL 7 - weekday(now()) DAY 

This above give me next monday.

I need to add 7 more days and get sunday date in same format.

2018-07-30 14:45:43 Monday

2018-08-05 14:45:43 Sunday

The strtotime() function is great for vague relative queries like this:

$monday = date('Y-m-d', strtotime('next monday'));
$sunday = date('Y-m-d', strtotime('next monday + 6 days'));

Yields:

2018-07-30
2018-08-05

Add 6 days to get the sunday of the monday. Add 13 days to add the second sunday after the monday:

SELECT (now() + INTERVAL 7 - weekday(now()) DAY) + INTERVAL 6 DAY;

If I understand correctly, you have gotten "2018-07-30 14:45:43" and need to get Sunday. Sunday is six days after Monday, so just add 6 to 7 and get 13.

SELECT now() + INTERVAL 13 - weekday(now()) DAY

How about

select monday, monday + interval 6 day as sunday from
(select now() + INTERVAL 7 - weekday(now()) DAY as monday) s1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM