Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STRTOTIME to find current week, date for Monday and Saturday, PHP

I have these lines:

$staticstart = date('Y-m-d',strtotime('last Monday'));
$staticfinish = date('Y-m-d',strtotime('next Saturday'));

And I am using them to select the monday and saturday of the current week, But when it is actually Monday, it choses the monday of the previous week, thus showing 2 weeks of data.... I have tried this and it produces no result:

$staticstart = date('Y-m-d',strtotime('this Monday'));

What have I missed? Is there a better way to find the monday and Saturday (dates) of the current week?

like image 254
atomapps Avatar asked Nov 29 '22 15:11

atomapps


1 Answers

why don't you try like this

//check the current day
if(date('D')!='Mon')
{    
 //take the last monday
  $staticstart = date('Y-m-d',strtotime('last Monday'));    

}else{
    $staticstart = date('Y-m-d');   
}

//always next saturday

if(date('D')!='Sat')
{
    $staticfinish = date('Y-m-d',strtotime('next Saturday'));
}else{

        $staticfinish = date('Y-m-d');
}
like image 126
Sundar Avatar answered Dec 05 '22 22:12

Sundar