如何计算在PHP中从开始时间到结束时间消耗的时间 [英] How to calculate the time consumed from start time to end time in php

查看:316
本文介绍了如何计算在PHP中从开始时间到结束时间消耗的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的代码:

  $ start_time = $ this-> input-> post('start_time'); 
$ end_time = $ this-> input-> post('end_time');


$ t1 = strtotime($ start_time);
$ t2 = strtotime($ end_time);
$ differenceInSeconds = $ t2 - $ t1;
$ differenceInHours = $ differenceInSeconds / 3600;

if($ differenceInHours <0){
$ differenceInHours + = 24;



$ b $ p
$ b $ p $ $ start_time = 11:00:00 PM $ end_date = 11:30:00 它给了我输出 0.5 而不是 30分钟。有没有适当的方法来做到这一点?

所以如果:

$ $ $ $ $ $ $ $ start_time = '01:00:00 PM';
$ end_time = '01:25:00 PM';

$ total = '25:00'; // 25分钟

或者:

  $ start_time = '11:00:00 PM'; 
$ end_time = '11:00:25 PM';

$ total = '00:00:25'; // 25秒

问候!

解决方案

试试区别功能

 <?php 
echo date_create('03:00:00 PM') - > diff(date_create '03:25:00 PM')) - >格式('%H:%i:%s');
?>

输出




  00:25:00 


I want to get the perfect time consumed or the total time from start time to end time:
My code:

   $start_time =  $this->input->post('start_time');
   $end_time =  $this->input->post('end_time');


   $t1  = strtotime($start_time);
   $t2 = strtotime($end_time);
   $differenceInSeconds = $t2 - $t1;
   $differenceInHours = $differenceInSeconds / 3600;

   if($differenceInHours<0) {
     $differenceInHours += 24; 
    }

In the code above if $start_time = 11:00:00 PM and $end_date =11:30:00 it gives me the output of 0.5 instead of 30minutes. Is there any appropriate way to do it like that?
So if the:

  $start_time  = '01:00:00 PM';
  $end_time = '01:25:00 PM';

  $total = '25:00'; // 25 minutes

or:

  $start_time  = '11:00:00 PM';
  $end_time = '11:00:25 PM';

  $total = '00:00:25'; // 25seconds

Regards!

解决方案

Try diff function

<?php
echo date_create('03:00:00 PM')->diff(date_create('03:25:00 PM'))->format('%H:%i:%s');
?>

Output


00:25:00

这篇关于如何计算在PHP中从开始时间到结束时间消耗的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆