检查星期几和时间 [英] Check day of week and time
问题描述
我一直试图弄清楚如何在一周中的一天和时间之间找到...我找到了这段代码:
I have been trying to figure out how to get between a day of week and time...I found this snippet of code:
function isBetween($from, $till, $input) {
$f = DateTime::createFromFormat('!H:i', $from);
$t = DateTime::createFromFormat('!H:i', $till);
$i = DateTime::createFromFormat('!H:i', $input);
if ($f > $t) $t->modify('+1 day');
return ($f <= $i && $i <= $t) || ($f <= $i->modify('+1 day') && $i <= $t);
};
并且我设法让它在两次之间检查并查看当前是否匹配使用:
and I have managed to get it to check between two times and see if current matches using:
if (isBetween('22:30','02:00','23:00')){
echo "true";
}
但是我尝试这样做
if (isBetween('22:30','02:00',date('H:i')){
但这只是破坏了网站,我缺乏处理时间的经验,我基本上想在周一晚上 11 点到周二下午 2 点或周一晚上 11 点到周六下午 5 点之间进行检查,无论一年/月的哪一天,它都必须进行此检查我希望它是动态的而不是静态的..
but that just broke the site, I lack experience handling time and I basically want to check between 11pm monday to 2pm tuesday or 11pm monday to 5pm saturday and it has to do this check no matter what day of the year/month meaning I want it to be dynamic not static..
我也试过:
if (date('H') < 14)
但它非常简单,我不知道如何获得像日期和小时+秒这样复杂的东西
but it was very simplistic and I didn't know how to get something as complex as both date and hour+seconds
推荐答案
您还缺少 1 个关闭的 )
your missing 1 more closing )
if (isBetween('22:30','02:00',date('H:i'))){
你可以这样做:
function isBetween($from, $till, $input) {
$f = date('H:i', strtotime($from));
$fd= date('l', strtotime($from));
$t = date('H:i', strtotime($till));
$td= date('l', strtotime($till));
$i = date('H:i', strtotime($input,'+8 hours'));
$id= date('l', strtotime($input));
$days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
if(array_search($fd,$days)<array_search($id,$days) && array_search($td,$days)>array_search($id,$days)){
return true;
}elseif(array_search($fd,$days)==array_search($id,$days) && $f<$i){
return true;
}elseif(array_search($td,$days)==array_search($id,$days) && $t<$i){
return true;
}
return false;
};
$from = 'Wednesday 12:00';
$till = 'Saturday 23:00';
$input = 'Friday 12:23';
if(isBetween($from,$till,$input)){
echo "True";
}else{
echo "not";
}
output = true
;
这篇关于检查星期几和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!