「已注销」 2018-08-08 20:39 采纳率: 0%
浏览 51
已采纳

PHP - 两次之间的差异

I've created a timing system for a charity race. I'm trying to find the difference between the start time and the finishers time using PHP. I'm not sure I'm recording the times correctly, but this is the start time i just recorded...

20180808180653

And this is a finisher time...

20180808180654

The difference between them is roughly 1 hour 24, but when i use...

date('h:i:s', $finshTime-$startTime)

I get 03:24:20 not 01:34:20.

Can someone please help?

  • 写回答

2条回答 默认 最新

  • duanhegn231318 2018-08-08 20:48
    关注

    The date method accepts as "integer Unix timestamp". You are supplying instead a number of seconds (1 in your example).

    $start = '20180808180653';
    $end = '20180808180654';
    
    $diff = $end - $start;
    var_dump($diff);         //1
    
    $d = date('h:i:s', $$diff);
    var_dump($d);            //04:00:01
    
    //the above is wrong. You need to try something like the code below   
    
    $dStart = new DateTime($start);
    $dEnd = new DateTime($end);
    $interval = $dStart->diff($dEnd);
    var_dump($interval->format('%h:%i:%s'));
    

    I'd be leery using a string representation of a datetime that looks like that. Convert the whole thing into a date format that makes sense like yyyy-mm-dd hh:mm:ss, or a valid unix time stamp.

    Your first approach isn't that far off, you just need to use a strtotime function. I'd guarantee that you can first make an accurate Date or Unix time representation of those strings you are using. Rest should fall into place.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1