douliu8327 2017-07-20 23:17
浏览 19
已采纳

需要此功能在周末返回false

Need help with php.

So I am writing this for a shop. This function returns false whenever they are closed (opening at 9:30 and closing at 22:45).

I need it to ALSO return false when the current day of the week is Saturday or Sunday.

Here is the function:

function can_order_now_timeframe(){

 $morning= new DateTime("09:30", new DateTimeZone('Europe/Budapest'));
 $night = new DateTime("22:45", new DateTimeZone('Europe/Budapest'));


 $morning_u = $morning->format('U');
 $night_u = $night->format('U');

 $datenow = new DateTime("now", new DateTimeZone('Europe/Budapest'));
 $timestampnow = $datenow->format('U');


 if(($morning_u<$timestampnow) && ($timestampnow < $night_u)){
 return true;
 }else{
 return false;
 }

}

Thanks for the help.

  • 写回答

2条回答 默认 最新

  • dongzang7182 2017-07-20 23:23
    关注

    Using the date object (credits to Icecub)

    function isWeekend($datenow){
        $day = $datenow->format('%w');
        if ($day=="0"||$day=="6"){
            return true;
        }else{
            return false;
        }
    }
    

    Using date and strtotime in combination with the timestamp ($timestampnow)

    If you have PHP >= 5.1:

    function isWeekend($date) {
        return (date('N', strtotime($date)) >= 6);
    }
    

    otherwise:

    function isWeekend($date) {
        $weekDay = date('w', strtotime($date));
        return ($weekDay == 0 || $weekDay == 6);
    }
    

    A complete code would look like ths. (You can use a function inside a function)

    <?php
    function isWeekend($datenow){
        $day = $datenow->format('%w');
        if ($day=="0"||$day=="6"){
            return true;
        }else{
            return false;
        }
    }
    
    function can_order_now_timeframe(){
    
        $morning= new DateTime("09:30", new DateTimeZone('Europe/Budapest'));
        $night = new DateTime("22:45", new DateTimeZone('Europe/Budapest'));
    
    
        $morning_u = $morning->format('U');
        $night_u = $night->format('U');
    
        $datenow = new DateTime("now", new DateTimeZone('Europe/Budapest'));
        $timestampnow = $datenow->format('U');
    
    
        if(($morning_u<$timestampnow) && ($timestampnow < $night_u) && !isWeekend($datenow)){
            return true;
        }else{
            return false;
        }
    
    }
    ?>
    

    or you can have it all inside one function like this if you dont think you might need the weekend detection again:

    <?php
    function can_order_now_timeframe(){
        $morning= new DateTime("09:30", new DateTimeZone('Europe/Budapest'));
        $night = new DateTime("22:45", new DateTimeZone('Europe/Budapest'));
    
        $morning_u = $morning->format('U');
        $night_u = $night->format('U');
    
        $datenow = new DateTime("now", new DateTimeZone('Europe/Budapest'));
        $timestampnow = $datenow->format('U');
    
        $day = $datenow->format('%w');
        if ($day!="0" and $day!="6"){   
            if(($morning_u<$timestampnow) && ($timestampnow < $night_u)){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据