Finding Dates for Days of the Current Week
Problem
You want to compute the date for some other day of the current week.
Solution
Figure out the number of days between the starting day and the desired day, and shift the date by that many days.
Discussion
This section and the next describe how to convert one date to another
when the target date is specified in terms of days of the week. To
solve such problems, you need to know day-of-week values. For
example, if you want to know what date it is on Tuesday of this week,
the calculation depends on what day of the week it is today. If today
is Monday, you add a day to CURDATE( ), but
if today is Wednesday, you subtract a day.
MySQL provides two functions that are useful here.
DAYOFWEEK( ) treats Sunday as the first day of the week
and returns 1 through 7 for Sunday through Saturday.
WEEKDAY( ) treats Monday as the first day of the
week and returns 0 through 6 for Monday through Sunday. (The examples
shown here use DAYOFWEEK( ).) Another kind of
day-of-week operation involves determining the name of the day.
DAYNAME( ) can be used for that.
Calculations that determine one day of the week from another depend on the day you start from as well as the day you want to reach. I find it easiest to shift the reference date first to a known point relative to the beginning of the week, then shift forward:
Shift the reference date back by its
DAYOFWEEK( )value, which always produces the date for the Saturday preceding the week.Add one day to the ...