2016-11-23 21 views
0

私はクロックインとクロックアウトのような2つのボタンがあるphpを使ってタイムトラックシステムを作ろうとしています。私は時間のクロックを保存しているとセッション変数の時間をクロックアウトです。さて私の要件は、クロックとクロックの時間の差を計算することです。あなたは時間の間の差を計算について明確な考えを持っている場合は、私PHPでクッキーを使って開始時間と終了時間を計算する方法

<?php 
if(isset($_POST['start'])) 
{ 
    date_default_timezone_set('Asia/Calcutta'); 
    $time_start=date("Y-m-d h:i:s A"); 
    setcookie('start',$time_start,time()+(36400)*3,'/'); 
    echo "time start now ".$time_start."<br>"; 
} 
if(isset($_POST['end'])) 
{ 
    date_default_timezone_set('Asia/Calcutta'); 
    $time_end=date("Y-m-d h:i:s A"); 
    setcookie('end',$time_end,time()+(36400)*3,'/'); 
    echo "time was ended".$time_end."<br>"; 
?> 
<html> 
    <body> 
    <form method="POST"> 
     <input type="submit" name="start" value="Start"> 
     <br><br> 
     <input type="submit" name="end" value="End"> 
    </form> 
    </body> 
</html> 

答えて

1

との考えを共有DateTimeクラスは、非常にシンプルな日数で作業することができ、様々な方法があります。おそらく、あなたの目標をどのように達成できるか、次のように考えてください。

$format='Y-m-d H:i:s'; 
$timezone=new DateTimeZone('Asia/Calcutta'); 

$clockin = new DateTime(date($format, strtotime($_COOKIE['start'])),$timezone); 
$clockout= new DateTime(date($format, strtotime($_COOKIE['end'])), $timezone); 

$diff=$clockout->diff($clockin); 

echo $diff->format('%h hours %i minutes %s seconds'); 

Further reading can be found here

完全にテスト、私はすぐにこれを書いたに - うまく動作するように見えます!

<?php 
    $cs='shift_start'; 
    $cf='shift_end'; 

    date_default_timezone_set('Europe/London'); 

    if(isset($_POST['start'])){ 
     setcookie($cs, date("Y-m-d h:i:s A"), time()+(36400)*3,'/'); 
    } 
    if(isset($_POST['end'])){ 
     setcookie($cf, date("Y-m-d h:i:s A"), time()+(36400)*3,'/'); 
    } 
?> 
<!doctype html> 
<html> 
    <head> 
     <title>Set cookies and get time difference</title> 
    </head> 
    <body> 
    <form method="post"> 
     <input type="submit" name="start" value="Clock-In"> 
     <input type="submit" name="end" value="Clock-Out"> 
     <br /><br /> 
     <input type='submit' name='show' value='Show duration' /> 
    </form> 
    <?php 
     if(isset($_POST['show'])){ 

       $format='Y-m-d H:i:s'; 
       $timezone=new DateTimeZone('Europe/London'); 

       $clockin = new DateTime(date($format, strtotime($_COOKIE[ $cs ])),$timezone); 
       $clockout= new DateTime(date($format, strtotime($_COOKIE[ $cf ])), $timezone); 

       $diff=$clockout->diff($clockin); 

       echo $diff->format('%h hours %i minutes %s seconds'); 
     } 
    ?> 
    </body> 
</html> 
+0

ただし、動作しません。 – manikandan

+0

私はちょうど静的に作成された2つのクッキー値(約4分離れて設定)でテストしましたが、それは違いとして「0時間4分49秒」を正しく示しました。 – RamRaider

関連する問題