所以我有一个脚本,对32、48和72小时前进行多次检查。基本上,我会检查数据库中至少有x小时历史的条目。
现在可以这样运行了:
$date = date('Y-m-d H:i:s',strtotime('-32 hours'));
$q = "SELECT * FROM `table` WHERE `date` <= '".$date."'";
现在我想把周末排除在外。我知道你可以在strtotime
中使用weekdays
来得到这个效果,但是这几个小时都不起作用。
对于48小时,这很容易,因为我可以简单地做以下事情:
echo date('Y-m-d H:i:s',
strtotime(date("Y-m-d H:i:s").
" -2 weekdays ".
date('H:i:s')));
72小时也很容易,因为它是3天。然而,32小时就有问题了,因为它是±1.3天。
总之,我如何得到32小时前的日期时间,不包括周末。
按初始设置使用strtotime
:
$time = strtotime('-32 hours');
然后手动计算周末/工作日。
// If the day is Sunday or Saturday subtract a full day.
while (date('w', $time) % 6 == 0) {
$time = strtotime('-1 day', $time);
}
$date = date('Y-m-d H:i:s', $time);
我不确定这是正确的还是最好的方法,但类似于:
function getDateBackExcludingWeekend( $hours ) {
$now = time();
$secondsBack = $hours * 3600;
$actual = $now - $secondsBack;
$monday = strtotime("last monday");
if( $actual < $monday ) {
$diff = ($secondsBack - ($now - $monday));
$backthen = ($monday - 172800 /* two days */) - $diff;
return date("Y-m-d H:i:s", $backthen);
}
return date("Y-m-d H:i:s", $actual);
}
为什么不直接去掉两天,半手动添加16个小时来弥补呢?
$DateTMP = date('Y-m-d h:i:s',(strtotime(date(Y-m-d)." -2 weekdays") + (60 * 60 * 16)));