4

所以我有一个脚本可以在 32、48 和 72 小时前进行多次检查。基本上,我会检查我的数据库中是否存在至少 x 小时前的条目。

现在这样可以正常工作:

$date = date('Y-m-d H:i:s',strtotime('-32 hours')); 
$q    = "SELECT * FROM `table` WHERE `date` <= '".$date."'";

现在我希望这不包括周末。我知道您可以使用weekdaysinsidestrtotime来获得这种效果,但这在几个小时内都不起作用。

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 小时前的日期时间,不包括周末。

4

3 回答 3

3

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);
于 2011-11-15T13:10:43.557 回答
1

我不确定这是否正确或最好的方法,但类似于:

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);
}
于 2011-11-15T13:01:46.193 回答
-2

为什么不直接删除两天并半手动增加 16 小时来弥补呢?

$DateTMP = date('Y-m-d h:i:s',(strtotime(date(Y-m-d)." -2 weekdays") + (60 * 60 * 16)));
于 2011-11-15T12:53:03.777 回答