需要此功能在周末返回false

时间:2017-07-20 23:17:34

标签: php datetime date-arithmetic

需要有关php的帮助。

所以我正在为一家商店写这篇文章。该函数在关闭时返回false(在9:30开始,在22:45关闭)。

如果当周的当天是星期六或星期日,我还需要它返回false。

这是功能:

(first time)
Failed: Uncaught (in promise): Error: No provider for JhiAlertErrorComponent!
(Nth time)
Failed: Uncaught (in promise): Error: No provider for BarService!

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

使用日期对象(贷记给Icecub)

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

将日期和strtotime与时间戳($ timestampnow)结合使用

如果你有PHP> = 5.1:

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

否则:

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

完整的代码看起来像是。 (您可以在函数内使用函数)

<?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;
    }

}
?>

或者你可以把这一切都放在这样的一个函数中,如果你不认为你可能需要再次周末检测:

<?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 :(得分:0)

如果你有PHP&gt; = 5.1:

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) && !(date('N', strtotime($date)) >= 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');
 $weekDay = date('w', strtotime($date));
 if(($morning_u<$timestampnow) && ($timestampnow < $night_u) && ($weekDay != 0 || $weekDay != 6)){
     return true;
 }else {
     return false; 
 }
}