【问题标题】:How to get time difference of a datetime value and a time value in PHP如何在PHP中获取日期时间值和时间值的时间差
【发布时间】:2021-03-27 23:12:19
【问题描述】:

我有一个 DATETIME 值“D1”和一个 TIME 值“T1”。比较这两个值后,我需要找到 D1 和 T1 的差异。 D1:2020-12-07 18:21:47 和 T1:19:30:00 我查找时差的代码:

如果(strtotime(T1) > strtotime(D1)){ $diff = date("H:i:s",(strtotime(T1) - strtotime(D1))); }`

我得到的结果不正确。

【问题讨论】:

  • 你得到什么结果?您对上述示例的预期结果是什么?
  • 我期待 01:08:13 但我得到 06:38:13

标签: php datetime time strtotime


【解决方案1】:

正在寻找同一天两次之间的差异。使用DateTime 的这个解决方案,从 $d1 开始的日期设置为当天,计算差值并格式化。

$d1 = '2020-12-07 18:21:47';
$t1 = '19:30:00';

$diffHis = date_create($d1)
  ->modify(date('Y-m-d'))  
  ->diff(date_create($t1))
  ->format('%r%H:%I:%s')
;
echo $diffHis; //01:08:13

另一种变体使用DateTime::createFromFormat,其格式隐藏$d1 中的日期。所以从 $d1 开始的时间获取当前日期。

$diffHis = DateTime::createFromFormat('????-??-?? H:i:s',$d1)
  ->diff(date_create($t1))
 ->format('%r%H:%I:%s')
;

【讨论】:

    【解决方案2】:

    2020-12-07 18:21:47 不是完整的日期时间值。缺少可能导致意外或错误结果的时区。

    strtotime() 是一个非常古老的函数。更好的方法是 DateTimeImmutable 类。您可以使用DateTimeImmutable::setTime() 设置时间并使用DateTimeImmutable::diff() 获取差异。

    $dates = [
        new DateTimeImmutable('2020-12-07T00:21:47Z'),
        new DateTimeImmutable('2020-12-07T00:21:47-0800'),
        new DateTimeImmutable('2020-12-07 00:21:47', new DateTimeZone('+02:00')),
        new DateTimeImmutable('2020-03-29 00:21:47', new DateTimeZone('Europe/Berlin')),
    ];
    $timeParts = explode(':', '19:30:00');
    
    foreach ($dates as $date) {
        // get a modified copy of the immutable date time
        $other = $date->setTime(...$timeParts);
        var_dump($other->format(DateTimeInterface::ATOM));
        // differences as a DateInterval
        $difference = $date->diff($other);
        var_dump($difference->format('%h hour(s), %i minute(s)'));
    }
    

    输出:

    string(25) "2020-12-07T19:30:00+00:00"
    string(23) "19 hour(s), 8 minute(s)"
    string(25) "2020-12-07T19:30:00-08:00"
    string(23) "19 hour(s), 8 minute(s)"
    string(25) "2020-12-07T19:30:00+02:00"
    string(23) "19 hour(s), 8 minute(s)"
    string(25) "2020-03-29T19:30:00+02:00"
    string(23) "18 hour(s), 8 minute(s)"
    

    请注意,最后一个结果是 18 小时。这是因为 3 月 29 日是这个时区的夏令时。

    【讨论】:

      【解决方案3】:
      $d1 = '2020-12-07 18:21:47';
      $t1 = date('Y-m-d',strtotime('2020-12-07 18:21:47')).' 19:30:00'; // or $t1 = '2020-12-07 19:30:00'; 
      
      if( strtotime($t1) > strtotime($d1)){
          $diff = date("H:i:s",(strtotime($t1) - strtotime($d1)));
      }
      
      print_r( $diff );
      

      //应该返回正确的结果 01:08:13

      检查您是否以正确的方式传递变量。

      【讨论】:

      【解决方案4】:

      使用此方法将日期添加到您的 t1 值

      date('Y-m-d',strtotime('2020-12-07 18:21:47'));
      

      然后用这个方法来获取差异

       $starttimestamp = strtotime($startdatetime);
          $endtimestamp = strtotime($enddatetime);
          $difference = abs($endtimestamp - $starttimestamp)/3600; in hours difference
      

      【讨论】:

      • 一个是日期时间,另一个是时间,由于时间戳被定义为日期时间/时间和1970年00:00:00之间的秒数,差异会很大。
      • 使用 date('Y-m-d',strtotime('2020-12-07 18:21:47')) 从 d1 中提取相同的日期;并将其与 t1 时间连接
      • 然后用这个区别法
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2013-09-08
      相关资源
      最近更新 更多