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条)

报告相同问题?

悬赏问题

  • ¥500 高有偿提问!求优化设计微信小程序
  • ¥15 matlab在安装时报错 无法找到入口 无法定位程序输入点
  • ¥15 收益高的广告联盟有哪些
  • ¥15 Android Studio webview 的使用问题, 播放器横屏全屏
  • ¥15 删掉jdk后重新下载,Java web所需要的eclipse无法使用
  • ¥15 uniapp正式环境中通过webapi将本地数据推送到设备出现的跨域问题
  • ¥15 xui建立节点,显示错误
  • ¥15 关于#单片机#的问题:开始、复位、十进制的功能可以实现,但是切换八进制的功能无法实现(按下按键也没有效果),把初始状态调成八进制,也是八进制可以实现但是切换到十进制不行(相关搜索:汇编语言|计数器)
  • ¥15 VINS-Mono或Fusion中feature_manager中estimated_depth是特征的深度还是逆深度?
  • ¥15 谷歌浏览器如何备份抖音网页数据