首页 > 解决方案 > 从上周的星期日开始的第 2 周星期一。如何?

问题描述

我在试图弄清楚如何捕捉 2 周工资单的范围时遇到了一些挑战。

我将如何修改-2W案例?

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

标签: phpdatetime

解决方案


你真的应该看看DateTime可能是DatePeriod。但是要使用现有代码回答这个问题,我认为您需要两种格式,一种如果今天是星期一,另一种如果不是:

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

有人可能会找到一种适用于两者的格式。


推荐阅读