2013-07-13 30 views
-2

我想製作一個限制用戶在工作時間後登錄的代碼: 用戶只應該從8:00登錄到4:35: Bellow是我嘗試過的代碼,我設法讓兩個條件工作,但第三個條件不工作:任何幫助將是非常讚賞:如何讓用戶只能在工作時間登錄?

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

?> 

回答

0

你必須明確你的日期,程序不能識別08:00是AM日期而04:35是PM日期。

只是改變你的變量聲明中

/*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」); //下午

$workStop相比,這種方式$workStart將被正確評估爲次要值。

然後改變你的,如果正確匹配的條件:

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"; 
} 
+0

我希望我可以upvote你:你做了一個偉大的工作,現在我的代碼現在工作reming這是與腰部代碼集成 –

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

你的第一個條件是總是正確的,如果時間爲開始時間之後。你需要檢查它是否在時間範圍內。

+0

當我把這個AND $ currentTime <= $ workStop的if語句不顯示任何東西,如果我刪除它是的,它確實: –

0

首先,如果你要管理的AM/PM小時,你應該將時間轉換爲24H格式中的每一行使用它們:

$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"; 
} 
+0

非常感謝我將Kawashita86理念與您的代碼相結合,然後工作我非常感謝 –

相關問題