2017-02-19 100 views
0

嗨即時嘗試使時間考勤基於時間表,如果不是代碼將阻止我,即時通訊這樣想?PHP通過時間塊插入延遲

date_default_timezone_set('Asia/Hong_Kong'); 
$now = new DateTime(); 

$mysched = new DateTime("16:00"); 

// time sessions 
// 7pm/19:00 
if ($now -> format ("H:i") > "19:00") { 
// getting my late 
$deadline = DateTime::createFromFormat("H:i", "19:15"); 
$diff = $now->diff($deadline); 
echo "You're ".$diff->i." minutes late"; 
} 
// 4pm/16:00 
else if ($now -> format ("H:i") > "16:00") { 

$deadline = DateTime::createFromFormat("H:i", "16:15"); 
$diff = $now->diff($deadline); 
echo "You're ".$diff->i." minutes late"; 
} 
// 1pm :13:00 
if ($now -> format ("H:i") > "13:00") { 

$deadline = DateTime::createFromFormat("H:i", "13:15"); 
$diff = $now->diff($deadline); 
echo "You're ".$diff->i." minutes late"; 
} 
else { 
// block if im trying to log at 1pm 
} 
// continue to inserting late to db.... 

所以問題是我在哪裏添加塊代碼片段?

回答

1

我認爲你必須檢查與UTC時間與你的系統時間檢查difference.The代碼如下

<?php 
$d = new DateTime(); 
$dtz = ($d->getOffset())/3600;//returnt he difference between utc and local time 
timeToSeconds($dtz); 

function timeToSeconds($dtz) 
{ 
    $timeExploded = explode(':', $dtz); 

    if (isset($timeExploded[2])) { 
     return $timeExploded[0] * 3600 + $timeExploded[1] * 60 + $timeExploded[2];//ccalculate it into seconds 

    } 
     $val=$timeExploded[0] * 3600 + $timeExploded[1] * 60;//return the seonds 
     echo "Actual difference between utc and timestamp: ". $dtz."<br />";//the time difference in hours 
     echo "The total second is: ".$val."<br />";//time difference in seconds 
     $hours = floor($val/3600); 
     $mins = floor($val/60 % 60); 
     $secs = floor($val % 60); 
     $timeFormat = sprintf('%02d:%02d:%02d', $hours, $mins, $secs);//convert seconds into H:i:s 
     echo "After seconds to time ".$timeFormat."<br />"; 
     $dda=date("h:i:sa");//return the current timestamp 
     echo "The current time is " . $dda."<br/>"; 
      $convert_current=strtotime($dda);//convert current timestamp 
      $convert_utc=strtotime($timeFormat);//convert utc time 
      echo "Convertion on current time: ".$convert_current."<br />"; 
      echo "Convertion on utc: ".$convert_utc."<br />"; 
       $minus=$convert_current-$convert_utc;//differentiate two time 
       echo "The two difference in seconds: ".$minus."<br />"; 
        $hours = floor($minus/3600); 
        $mins = floor($minus/60 % 60); 
        $secs = floor($minus % 60); 
        $correct = sprintf('%02d:%02d:%02d', $hours, $mins, $secs);//converting again it into H:i:s 
        echo "UTC time: ".$correct."<br />"; 
} 
?> 

在這些你都默認有時間去分析你已經在當前時間被帶走了。