MySQL: select the date of the current week of Monday

I create a weekly report using MySQL queries. First I get the week number on

SELECT WEEK(CURDATE()); 

Then I need to display the start date of the week (Monday for my region). How to do this only with MySQL ?

Thanks!

+6
source share
4 answers

If you need a Monday date for the current week, try the following:

 SELECT DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY) 

He will return you the current week of the current week.

+10
source
 SELECT DATE_ADD((SELECT curdate() - INTERVAL (WEEKDAY(curdate())+1)DAY),INTERVAL 1 DAY) as current_monday 
+1
source

In fact, DATE_ADD is not necessary and complicates the logic. Here is the simplest version for "Date of the first day of the current week":

 select date(curdate() - interval weekday(curdate()) day) 

which translates to: get the date () part (current date - interval in N days) where N = today is the number per week, i.e. Thu - number 3 (during the week starting on Monday).

Then, having received the Monday of the previous week, follow these steps:

 select date(curdate() - interval weekday(curdate()) day - interval 1 week) 

which is more readable IMHO.

+1
source

You can use this:

 <?php $week = date('W'); $year = date('Y'); echo $date1 = date( 'Ym-d', strtotime($year . 'W' . str_pad($week, 2, '0', STR_PAD_LEFT)) ); ?> 
0
source

All Articles