dove2199 2016-09-22 09:23
浏览 65
已采纳

使用strtotime的时差

This shouldn't be hard, but still I don't understand what is happening.

<?php
    echo date( 'H:i', strtotime( '09:00' ) - strtotime( '08:00' ) ); 
    // returns 02:00 instead of 01:00

    echo date( 'H:i', strtotime( '18:45' ) - strtotime( '17:15' ) ); 
    // returns 02:30 instead of 01:30

    echo date( 'H:i', strtotime( '17:00' ) - strtotime( '09:00' ) );
    // returns 09:00 instead of 08:00

What is adding the extra hour?

So maybe I need to add a date?

<?php
    echo date( 'H:i', strtotime( '22-09-2016 17:00:00' ) - strtotime( '22-09-2016 09:00:00' ) );
    // still 09:00 instead of 08:00

Some extra information

I am using Laravel en tested in Xampp.

In Laravel /config/app.php 'timezone' => 'Europe/Amsterdam',

In Xampp date_default_timezone_set('Europe/Amsterdam');

Still no difference.

This calculation is done in the blade template where I cannot access the DateTime class.

I found a way to use the DateTime class. Another solution was changing the timezone to UTC, but then all my other timestamps are wrong.

  • 写回答

3条回答 默认 最新

  • duanlan7903 2016-09-22 09:36
    关注

    find difference using DateTime object like

    $time1 = new DateTime('18:45');
    $time2 = new DateTime('17:15');
    $interval = $time1->diff($time2);
    echo $interval->format('%H:%I');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样