32 小时前 不包括周末 php

发布于 2024-12-15 15:10:51 字数 590 浏览 1 评论 0原文

所以我有一个脚本可以对 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 小时前(不包括周末)的日期时间。

So I have a script that does multiple checks for 32, 48 and 72 hours ago.
Basically I check my database for entries that are at least x hours old.

Now this works fine like this:

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

Now I want this to exclude weekends. I know you can use weekdays within strtotime to get this effect however this doesn't work for hours.

For 48 hours it's easy because I can simply do the following:

echo date('Y-m-d H:i:s',
          strtotime(date("Y-m-d H:i:s").
          " -2 weekdays ".
          date('H:i:s')));

For 72 hours it's also easy because it's 3 days. However 32 hours poses a problem because it's ±1.3 days.

In conclusion, how do I get the datetime of 32 hours ago excluding weekends.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光是把杀猪刀 2024-12-22 15:10:51

像最初一样使用 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);

Use strtotime as you had initially:

$time = strtotime('-32 hours');

Then do the weekend/weekday calculation manually.

// 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);
久隐师 2024-12-22 15:10:51

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

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);
}

I am not sure if this is correct or the best way to do it but something like:

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);
}
黯然#的苍凉 2024-12-22 15:10:51

为什么不直接去掉两天,加上半手动16个小时来弥补呢?

$DateTMP = date('Y-m-d h:i:s',(strtotime(date(Y-m-d)." -2 weekdays") + (60 * 60 * 16)));

Why not just remove two days and add 16 hours semi-manually to make up for it?

$DateTMP = date('Y-m-d h:i:s',(strtotime(date(Y-m-d)." -2 weekdays") + (60 * 60 * 16)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文