douzhi3776 2015-06-05 01:13
浏览 26
已采纳

检查星期几和时间

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条回答 默认 最新

  • doukougua7873 2015-06-05 01:20
    关注

    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;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序