Skip to content
Advertisement

Monday 2 weeks from sunday last week. How?

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;
}

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement