簡體   English   中英

如何獲得兩個時間戳之間的差異?

[英]How to get the difference between 2 timestamps?

我正在為此項目使用codeigniter 3.0.3。

我試圖得到兩個時間戳之間的差異(時鍾輸入和時鍾輸出)。

這是嘗試執行此操作的函數:

public function clock_user_out()
{
    $this->db->where('USER_EMAIL', $this->session->userdata('USER_EMAIL')) 


    $data = $this->db->get('clocked_in_users'); 
    $uemail = $data->row()->USER_EMAIL;

    $uclockin = $data->row()->USER_CLOCK_IN;

    $uclockout = date(('Y-m-d H:i:s'));

    $uhours = $uclockout-$uclockin;

    $newdata['USER_EMAIL'] =  $uemail;

    $newdata['USER_CLOCK_IN'] = $uclockin;  


    $newdata['USER_CLOCK_OUT'] = $uclockout; 


    $newdata['USER_WORK_HOURS'] = $uhours;

    $this->db->insert('user_hours',$newdata);



    $this->db->where('USER_EMAIL', $this->session->userdata('USER_EMAIL'));     




    $this->db->delete('clocked_in_users'); 

}

我相信錯誤是當我通過從$ uclockout減去$ uclockin得到變量$ uhours時。

我正在處理的表中有5列。

  1. ID [int]
  2. USER_EMAIL [varchar]
  3. USER_CLOCK_IN [時間戳記]
  4. USER_CLOCK_OUT [時間戳記]
  5. USER_WORK_HOURS [時間]

現在,對於我測試的結果,這是結果。

  • USER_CLOCK_IN = 2015-12-31 07:37:59
  • USER_CLOCK_OUT = 2015-12-31 07:38:07

但是USER_WORK_HOURS的結果最終為00:20:15。 這對我沒有意義。 看起來唯一的事情是花了2015年並將其分解為數據庫中的TIME格式?

那是怎么回事? 我是否為該列使用了錯誤的格式?

正確的做法是什么?

提前致謝。

編輯:由於Ashok Chitroda和Jorge Torres解決了問題,這是我不得不進行的更正

$uhours = $uclockout-$uclockin;

$uhours = date('H:i:s' , (strtotime(date('Y-m-d 00:00:00'))+ strtotime($uclockout) - strtotime($clockin) ));

而且我拼寫了一個錯誤的變量。

謝謝大家

更換

$uhours = $uclockout-$clockin;

$uhours = date('H:i:s' , (strtotime(date('Y-m-d 00:00:00'))+ strtotime($uclockout) - strtotime($clockin) ));

您可能在減法中使用了錯誤的變量?

$uclockin = $data->row()->USER_CLOCK_IN;

$uclockout = date(('Y-m-d H:i:s'));

$uhours = $uclockout-$clockin;

注意$ uclockin與$ clockin

在查詢中使用timestampdiff(UNIT,FIRST TIMESTAMP,SECOND TIMESTAMP)。 點擊此鏈接

你可以這樣使用

$to_time = $clockin;
$from_time =$uclockout;
$a= round(abs($to_time - $from_time) / 60);

echo ($a/60).' Hours';

如果您的$ clockin不在時間戳中,請像這樣將日期轉換為時間戳。

$to_time = strtotime("2008-12-13 09:42:00");
$from_time = strtotime("2008-12-13 11:42:00");
$a= round(abs($to_time - $from_time) / 60);

echo ($a/60).' Hours';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM