[英]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";
}
However I tried doing this 但是我尝试这样做
if (isBetween('22:30','02:00',date('H:i')){
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.. 但是那只是破坏了站点,我缺乏处理时间的经验,我基本上想在星期一的晚上11点到星期二的下午2点之间进行检查,或者星期一的星期一晚上11点到星期六的下午5点之间进行检查,无论是一年中的哪个月/月,都必须进行检查。要动态而不是静态
I've also tried: 我也尝试过:
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 但这非常简单,我不知道如何获得日期和小时+秒等复杂的信息
your missing 1 more closing )
您还缺少1个结账
)
if (isBetween('22:30','02:00',date('H:i'))){
You can just do like this: 您可以这样做:
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
; 输出=
true
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.