【问题标题】:Difference between 2 times PHP2倍PHP之间的差异
【发布时间】:2017-10-16 13:47:50
【问题描述】:

您好,我知道您可能认为他们中的许多人可能已经问过这个问题,但情况却大不相同。

由于我是按月计算劳动力完成的总轮班时间,因此小时数将超过 24 小时。我通过 DateInterval 实现了这一点。

现在我有 2 次,想得到那些在 php 中的区别。

$workedHours        = '78:25:10';
$miscSpentHours     = '02:26:11';

$totalWorkedHours = $workedHours - $miscSpentHours; // Must Result - 75:59:59

我已经使用了 sub、diff、strtotime ,但没​​有正确使用。我在转换时将其转换为适当的日期类型。

非常感谢任何帮助。谢谢。

【问题讨论】:

标签: php datetime dateinterval


【解决方案1】:

您可以将其转换为秒,然后进行减法,然后将结果转换回来。

【讨论】:

    【解决方案2】:

    我没有测试过这个。但这应该会让你朝着正确的方向前进

    <?php
    
    $hours = 70;//enter number of hours
    $zero    = new DateTime('@0');
    $offset  = new DateTime('@' . $hours * 3600);//convert to minutes
    $diff    = $zero->diff($offset);
    
    
    $formattedHours = $diff->format('%a Days, %h Hours, %i Minutes');//get format in days hours and minutes
    
    $start_date = new DateTime($formattedHours);//pass in contruct
    $since_start = $start_date->diff(new DateTime('02:26:11'));//do difference
    echo $since_start->days.' days total<br>';
    echo $since_start->y.' years<br>';
    echo $since_start->m.' months<br>';
    echo $since_start->d.' days<br>';
    echo $since_start->h.' hours<br>';
    echo $since_start->i.' minutes<br>';
    echo $since_start->s.' seconds<br>';
    
    ?>
    

    【讨论】:

      【解决方案3】:
      1. 检查我的函数TimeToSec() 将 HH:MM:SS 转换为秒
      2. 减法
      3. 将秒转换回 HH:MM:SS(检查 this answer)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-09
        • 1970-01-01
        • 1970-01-01
        • 2013-05-28
        • 1970-01-01
        • 1970-01-01
        • 2014-04-21
        • 2013-08-23
        相关资源
        最近更新 更多