-2

我想制作一个限制用户在下班后登录的代码:用户只能在 8:00 到 4:35 之间登录:波纹管是我尝试过的代码,我设法使两个条件起作用,但第三个条件不起作用:任何帮助将不胜感激:

<?php
    # function to get right time of the targeted city
    date_default_timezone_set('Africa/Johannesburg');
    function time_diff_conv($start, $s)
    {
        $string="";
        $t = array( //suffixes
            'd' => 86400,
            'h' => 3600,
            'm' => 60,
        );
        $s = abs($s - $start);
        foreach($t as $key => &$val) {
            $$key = floor($s/$val);
            $s -= ($$key*$val);
            $string .= ($$key==0) ? '' : $$key . "$key ";
        }
        return $string . $s. 's';
    }
$date = date('h:i');
 echo  $date;
 /*Start time for work*/
 $workStart = strtotime("08:00");//am
  /*Stop time for work*/
 $workStop = strtotime("04:35");//pm
 /*This is the current time*/
 $currentTime = strtotime($date); 
 //$fromSatrtToEnd = $workStart->diff($workStop);
 /*This Condition works*/
 if($currentTime >=$workStart){
 echo "Start Working";
 /*This Condition also  works*/
 } else if($currentTime < $workStart){
 echo "You too early at work";
 }
 /*This Condition does not works*/
 else if($currentTime < $workStop){
 echo "Its after work";
 }

?>
4

3 回答 3

1
if($currentTime >=$workStart AND $currentTime <= $workStop){
    echo "Start Working";
    /*This Condition also  works*/
} else if($currentTime < $workStart){
    echo "You too early at work";
}
/*This Condition does not works*/
else if($currentTime < $workStop){
    echo "Its after work";
}

如果时间在开始时间之后,您的第一个条件始终为真。您需要检查它是否在时间范围内。

于 2013-07-13T06:43:01.200 回答
0

您必须明确日期的时间段,程序无法识别 08:00 是上午日期和 04:35 是下午日期。

只需更改您的变量声明

 /*Start time for work*/
 $workStart = strtotime("08:00 AM");//am
  /*Stop time for work*/
 $workStop = strtotime("04:35 PM");//pm

或将第二个变量转换为 24 小时格式:

$workStop = strtotime("16:35 PM");//下午

与 相比,这种方式$workStart将被正确评估为次要值$workStop

然后更改您的 if 以正确匹配条件:

if($currentTime >=$workStart && $current <= $workstop){
    echo "Start Working";
} else if($currentTime < $workStart){
    echo "You too early at work";
}
else if($currentTime > $workStop){ 
    echo "Its after work";
}
于 2013-07-13T06:45:15.603 回答
0

首先,如果您必须管理上午/下午时间,则应在使用它们的每一行中将时间转换为 24 小时格式:

$date = date('H:i'); // H rather than h
$workStart = strtotime("08:00");
$workStop = strtotime("16:35");

并且您的条件不正确,您应该有类似的东西:

if($currentTime >=$workStart AND $current <= $workstop){ // need to check both
    echo "Start Working";
} else if($currentTime < $workStart){
    echo "You too early at work";
}
else if($currentTime > $workStop){ // > rather than <
    echo "Its after work";
}
于 2013-07-13T06:49:26.587 回答