1

How to get record of years last week when week is end in next year for example I have to get record of following four week where week 53 is end in 2016 .

week_no  week_start  -   week_end 
==================================
week-53 (2015-12-28 to 2016-01-03) 
week-52 (2015-12-21 to 2015-12-27) 
week-51 (2015-12-14 to 2015-12-20) 
week-50 (2015-12-07 to 2015-12-13) 

and following is my query

function getLastFourWeekUser($table=FALSE){        
    if($table==TRUE){
        $this->db->from($table);
    }
    else{
        $this->db->from('users');        
    }        
    $endDate=date('Y-m-d',  strtotime('-3 week'));        
    $dto = new DateTime();
    $year=date('Y',strtotime($endDate));
    $week=date('W',strtotime($endDate));
    $week_start = $dto->setISODate($year,$week)->format('Y-m-d');        
    $to = date("Y-m-d", strtotime(date('o')-date('W')-7));                  
    $query = $this->db->select('count(*) as count,DATE_FORMAT(created_at,"%u-%Y") as week',false)                                        
                      ->where('created_at >=',$week_start)  
                      ->where('created_at <=',$to)
                      ->group_by('week')
                      ->order_by('week')
                      ->get();
    }

I wrote a query which will give me record of only this year i.e. till 2015-12-31 only. But I want records of 1st-jan to 3rd-jan too. Because week 53 ends in 2016-01-03.

Can any one help me to solve this issue.

5
  • not sure but it happen beacuse you used date('o') in $to and set : 'created_at <=',$to where created at consider 2015 year so it not be able to go in 2016
    – jilesh
    Jan 1, 2016 at 13:26
  • Yes, you right,but if i write ('Y') instead of ('o') than it give me result as week-0. Jan 1, 2016 at 13:33
  • use turnery operator and set condition in $to = (if condition ?)if week completed in past year use this(put your logic) : if in same year use another(put your logic) ;
    – jilesh
    Jan 1, 2016 at 13:35
  • but i think so every time last week should end in next year Jan 1, 2016 at 13:39
  • try using $to = date("Y-m-d", strtotime(date('Y')-date('W')-7)); it will give you current year result or use it like $to = (condition)? date("Y-m-d", strtotime(date('O')-date('W')-7)): date("Y-m-d", strtotime(date('Y')-date('W')-7));
    – jilesh
    Jan 1, 2016 at 13:40

1 Answer 1

0

Try this :)

SELECT
DATE_FORMAT(DATE_FORMAT(STR_TO_DATE(CONCAT(DATE_FORMAT(created_at, '%x%v'), 'Monday'), '%x%v%W'), '%Y-%m-%d'), "%u-%Y") AS week,
DATE_FORMAT(STR_TO_DATE(CONCAT(DATE_FORMAT(created_at, '%x%v'), 'Monday'), '%x%v%W'), '%Y-%m-%d') AS week_start,
DATE_FORMAT(STR_TO_DATE(CONCAT(DATE_FORMAT(created_at, '%x%v'), 'Sunday'), '%x%v%W'), '%Y-%m-%d') AS week_end,
COUNT(*) as count
WHERE created_at >='$week_start' AND created_at <='$to'
GROUP BY week
ORDER BY week
3
  • it gives me record still 31-12-2015 Jan 1, 2016 at 14:05
  • $to = date("Y-m-d", strtotime(date('o')-date('W')-7)); by this contruction you want to get last date of current week?
    – Max P.
    Jan 1, 2016 at 14:18
  • than $to = new \DateTime(); $to = $to->add(new \DateInterval('P'.(7-$to->format('N')).'D'))->format('Y-m-d');
    – Max P.
    Jan 4, 2016 at 8:44

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.