问题描述
所以我有一个脚本在32,48和72小时之前进行多次检查。基本上我检查我的数据库是否至少为x小时的条目。
现在这样做很好:
$ date = date('Ymd H:i:s',strtotime(' - 32 hours'));
$ q =SELECT * FROM`table` WHERE`date`< ='。$ date。';
现在我想要排除周末。我知道你可以使用 strtotime
中的工作日
来获得这个效果,但这不会在几个小时内运作。 >
48小时很容易,因为我可以简单地执行以下操作:
echo日期('Ymd H:i:s',
strtotime(date(Ymd H:i:s)
-2 weekdays
date('H:i: s))));
72小时,这也很简单,因为它是3天。但是32个小时会造成问题,因为它是±1.3天。
总而言之,如何获得32小时前的日期时间,不包括周末。
使用 strtotime
,就像你最初一样:
$ time = strtotime(' - 32 hours');
然后手动执行周末/周日计算。
//如果一天是星期天或星期六减去一整天。
while(date('w',$ time)%6 == 0){
$ time = strtotime(' - 1天',$ time);
}
$ date = date('Y-m-d H:i:s',$ time);
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.
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);
这篇关于32小时前,除了周末与php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!