计算PHP中日期/时间之间的差异

Bri*_*n G 5 php

我有一个Date对象(来自Pear)并且想要减去另一个Date对象以获得以秒为单位的时间差.

我尝试了一些东西,但第一个只是给了我几天的差异,第二个允许我将一个固定时间转换为unix时间戳而不是Date对象.

        $now = new Date();
        $tzone = new Date_TimeZone($timezone);
        $now->convertTZ($tzone);
        $start = strtotime($now);
        $eob = strtotime("2009/07/02 17:00"); // Always today at 17:00

        $timediff = $eob - $start;
Run Code Online (Sandbox Code Playgroud)

**注意**它总是不到24小时的差异.

Car*_*ima 1

仍然给出了一些错误的值,但考虑到我有一个旧版本的 PEAR Date,也许它适合你或者给你一个如何修复的提示:)

<pre>
<?php
  require "Date.php";

  $now = new Date();
  $target = new Date("2009-07-02 15:00:00");

  //Bring target to current timezone to compare. (From Hawaii to GMT)
  $target->setTZByID("US/Hawaii");
  $target->convertTZByID("America/Sao_Paulo");

  $diff = new Date_Span($target,$now);

  echo "Now (localtime): {$now->format("%Y-%m-%d %H:%M:%S")} \n\n";
  echo "Target (localtime): {$target->format("%Y-%m-%d %H:%M:%S")} \n\n";
  echo $diff->format("Diff: %g seconds => %C");
?>
</pre>
Run Code Online (Sandbox Code Playgroud)