【问题标题】:Calculate the difference between date/times in PHP计算PHP中日期/时间之间的差异
【发布时间】:2010-11-07 15:21:27
【问题描述】:

我有一个 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;

** 注意 ** 时差总是小于 24 小时。

【问题讨论】:

  • $now 的输出格式是否与您输入 strtotime() 的字符串相同?即“yyyy/mm/dd H:i”
  • 可能只有我一个人,但我似乎无法在 PHP 文档中找到“日期”类的描述。它来自哪个图书馆?
  • 为什么只将其中一个转换为另一个时区?您不应该在目标 TZ 上同时使用两者作为本地时间吗?
  • 我应该说的目的是找出我在另一个时区到 17:00 之前还有多少时间,实际上它看起来很多其他时区。
  • 根据您的目的更新了我的答案,希望对您有所帮助。

标签: php


【解决方案1】:

也许有些人想通过 facebook 的方式获得时差。它告诉您“一分钟前”或“2 天前”等...这是我的代码:

function getTimeDifferenceToNowString($timeToCompare) {

        // get current time
        $currentTime = new Date();
        $currentTimeInSeconds = strtotime($currentTime);
        $timeToCompareInSeconds = strtotime($timeToCompare);

        // get delta between $time and $currentTime
        $delta = $currentTimeInSeconds - $timeToCompareInSeconds;

        // if delta is more than 7 days print the date
        if ($delta > 60 * 60 * 24 *7 ) {
            return $timeToCompare;
        }   

        // if delta is more than 24 hours print in days
        else if ($delta > 60 * 60 *24) {
            $days = $delta / (60*60 *24);
            return $days . " days ago";
        }

        // if delta is more than 60 minutes, print in hours
        else if ($delta > 60 * 60){
            $hours = $delta / (60*60);
            return $hours . " hours ago";
        }

        // if delta is more than 60 seconds print in minutes
        else if ($delta > 60) {
            $minutes = $delta / 60;
            return $minutes . " minutes ago";
        }

        // actually for now: if it is less or equal to 60 seconds, just say it is a minute
        return "one minute ago";

    }

【讨论】:

    【解决方案2】:

    仍然给出了一些错误的值,但考虑到我有一个旧版本的 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>
    

    【讨论】:

      【解决方案3】:

      我认为您不应该将整个 Date 对象传递给 strtotime。改用其中一种;

      $start = strtotime($now->getDate());
      

      $start = $now->getTime();
      

      【讨论】:

        【解决方案4】:

        要做到这一点没有梨,找到秒'直到 17:00 你可以这样做:

        $current_time = mktime (); 
        $target_time = strtotime (date ('Y-m-d'. ' 17:00:00')); 
        $timediff = $target_time - $current_time;
        

        没有测试过,但它应该可以满足您的需求。

        【讨论】:

          【解决方案5】:

          您确定 Pear Date 对象 -> 字符串 -> 时间戳的转换会可靠地工作吗?这就是这里正在做的事情:

          $start = strtotime($now);
          

          作为替代方案,您可以根据documentation 获得这样的时间戳

          $start = $now->getTime();
          

          【讨论】:

            猜你喜欢
            • 2011-05-16
            • 2021-11-25
            • 2011-08-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-10
            • 1970-01-01
            相关资源
            最近更新 更多