dsgawmla208057 2015-12-31 06:52
浏览 208
已采纳

如何获得2个时间戳之间的差异?

I am using codeigniter 3.0.3 for this project.

I am trying to get the difference between two timestamps (clock in and clock out).

Here is the function attempting to do so:

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'); 

}

I believe the error is when I am getting the variable $uhours by subtracting $uclockin from $uclockout.

There are 5 columns in the table I am working with.

  1. ID [int]
  2. USER_EMAIL [varchar]
  3. USER_CLOCK_IN [timestamp]
  4. USER_CLOCK_OUT [timestamp]
  5. USER_WORK_HOURS [time]

Now for the one I tested these were the results.

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

but the result for the USER_WORK_HOURS ended up being 00:20:15. Which does not make sense to me. The only thing that it looks like it did was take the year 2015 and broke it up into the TIME format in the database?

Is that what happened? Am I using the wrong format for that column?

What is the right way to go about doing this?

Thanks in advance.

EDIT: Problem solved thanks to Ashok Chitroda and Jorge Torres here is the corrections I had to make

$uhours = $uclockout-$uclockin;

to

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

and also I spelled a variable wrong.

Thank you everyone

  • 写回答

4条回答 默认 最新

  • dqc3469 2015-12-31 07:13
    关注

    Replace

    $uhours = $uclockout-$clockin;
    

    to

    $uhours = date('H:i:s' , (strtotime(date('Y-m-d 00:00:00'))+ strtotime($uclockout) - strtotime($clockin) ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致