php - 从上周的星期日开始的第 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;
}
解决方案
你真的应该看看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'
}
有人可能会找到一种适用于两者的格式。
推荐阅读
- typescript - 基于另一个泛型参数的类型对象参数
- c++ - 如何为采用 stl 容器迭代器的函数提供函数签名?
- jquery - 如何在 jQuery 中过滤数据表值?
- python - 在Python中添加年份到日期(dmy)列
- android - 有没有办法用一个命令添加多个 gradle 依赖项?
- ethereum - web3.eth.abi.encodeFunctionSignature 没有按预期工作
- c# - A* 算法中的无限循环
- c# - ENC1003 C# 在项目中所做的更改将不会在应用程序运行时应用
- python - Pandas 中的嵌套 If 语句
- python - 如何制作使用多个 python 环境的工作流