【发布时间】:2021-09-16 18:07:32
【问题描述】:
我在试图弄清楚如何获取 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;
}
【问题讨论】:
-
今天是
August 30还是September 6? -
没错..!
-
不!哪一个?他们之间有一个 OR。
-
8 月 30 日是正确的。
-
哇。如果您使用 PHP 处理日期的最佳工具(请参阅 DateTime),这可以减少到仅几行代码。