2

i am storing date in strtotime format in codeigniter. and week start from sunday.

$this->db->where($this->weight . '.create_date <=', 'curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY');
$this->db->where($this->weight . '.create_date >', 'curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY');
1
  • for current week create_date <= DATE_SUB(NOW(), INTERVAL 1 WEEK for last week create_date >= DATE_SUB(NOW(), INTERVAL 1 WEEK for last month create_date >= DATE_SUB(NOW(), INTERVAL 1 MONTH) Mar 9, 2015 at 6:51

1 Answer 1

1

For taking last week:

WHERE date BETWEEN date_sub(now(),INTERVAL 1 WEEK) and now();

For taking current week:

WHERE  YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)

For taking last month:

year(date(FROM_UNIXTIME(u.create))) = year(CURDATE() - INTERVAL 1 MONTH)
and month(date(FROM_UNIXTIME(u.create))) = month(CURDATE() - INTERVAL 1 MONTH)
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.