上星期星期日后两周的星期一.如何?



我遇到了一点挑战,试图找出如何捕获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;
}

你应该看看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'
}

也许有人能找到一种同时适用于两者的格式。

相关内容

最新更新