MySQL: How to select records for this week? MySQL: How to select records for this week? mysql mysql

MySQL: How to select records for this week?


Use YEARWEEK():

SELECT *FROM   your_tableWHERE  YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)


Use YEARWEEK. If you use WEEKOFYEAR you will get records of previous years also.

SELECT id, name, dateFROM tableWHERE YEARWEEK(date)=YEARWEEK(NOW());


For selecting records of day, week and month use this way:

function my_func($time, $your_date) {if ($time == 'today') {    $timeSQL = ' Date($your_date)= CURDATE()';}if ($time == 'week') {    $timeSQL = ' YEARWEEK($your_date)= YEARWEEK(CURDATE())';}if ($time == 'month') {    $timeSQL = ' Year($your_date)=Year(CURDATE()) AND Month(`your_date`)= Month(CURDATE())';}$Sql = "SELECT * FROM  your_table WHERE ".$timeSQLreturn $Result = $this->db->query($Sql)->result_array();}