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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题