Home > front end >  Monday 2 weeks from sunday last week. How?
Monday 2 weeks from sunday last week. How?

Time:09-17

I run into a bit of a challenge trying to figure out how to capture the range of 2 week payrolls.

How would I go about modifying the -2W case?

$from = null;
$to = null;

switch ($range) {
  case '1D':
    // code...
    $from = date('Y-m-d ', strtotime('today')) . '00:00:00';
    $to = date('Y-m-d ', strtotime('today')) . '23:59:59';
    $objectView->historyFrom = date('D, F j');
    $objectView->historyTo = date('D, F j');
    break;
  case '1W':
    // code...
    $from = date('Y-m-d ', strtotime('monday this week')) . '00:00:00';
    $to = date('Y-m-d ', strtotime('sunday this week')) . '23:59:59';
    $objectView->historyFrom = date('D, F j', strtotime('monday this week'));
    $objectView->historyTo = date('D, F j', strtotime('sunday this week'));
    break;
  case '-1W':
    // code...
    $from = date('Y-m-d ', strtotime('monday last week')) . '00:00:00';
    $to = date('Y-m-d ', strtotime('sunday last week')) . '23:59:59';
    $objectView->historyFrom = date('D, F j', strtotime('monday last week'));
    $objectView->historyTo = date('D, F j', strtotime('sunday last week'));
    break; 
  case '-2W':
    // code...
    $from = date('Y-m-d ', strtotime('monday previous to last week')) . '00:00:00';
    $to = date('Y-m-d ', strtotime('sunday last week')) . '23:59:59';
    $objectView->historyFrom = date('D, F j', strtotime('monday last week'));
    $objectView->historyTo = date('D, F j', strtotime('sunday last week'));
    break; 
  default:
    // code...
    $from = date('Y-m-d ', strtotime('today')) . '00:00:00';
    $to = date('Y-m-d ', strtotime('today')) . '23:59:59';
    $objectView->historyFrom = date('D, F j');
    $objectView->historyTo = date('D, F j');
    break;
}

CodePudding user response:

You should really look at DateTime probably DatePeriod. But to answer the question with existing code I think you will need two formats, one if today is a Monday and another if it's not:

if(date('w') == '1') {
    $from = date('Y-m-d ', strtotime('-2 weeks');
    //or '-14 days'
} else {
    $from = date('Y-m-d ', strtotime('previous monday -2 weeks'));
    //or 'previous monday -14 days'
}

Someone may be able to find one format that works for both.

  • Related