Check day of week and time

橙三吉。 提交于 2021-02-11 00:50:33

问题


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..

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


回答1:


your missing 1 more closing )

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;



来源:https://stackoverflow.com/questions/30656789/check-day-of-week-and-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!