【问题标题】:php strtotime in seconds and minutesphp strtotime 以秒和分钟为单位
【发布时间】:2014-06-25 12:54:20
【问题描述】:

我使用 ths 方法来查找两个时间戳之间的差异并获取这两个时间之间的秒数,然后像计数器一样用 jquery 刷新信息。

$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = intval(date('s', $diff));
echo $time;

当差值超过 60 秒时,$time 回到 0,就像重置一样。

例如,我想显示 1 min XX s

【问题讨论】:

    标签: php date strtotime date-math


    【解决方案1】:

    date() 的 s 标志永远不会返回大于 59 的值,因为它只表示给定时间的当前秒数,在滚动到新的分钟之前永远不会超过 59。

    如果您想要总秒数,您实际上可以删除第二行代码,因为两个 Unix 时间戳之间的差异始终以秒为单位:

    $time = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
    echo $time;
    

    如果您想将其显示为分钟和秒,您可以使用DateTime(),它为此提供了更好的工具:

    $now = new DateTime();
    $then = new DateTime('2014-06-25 14:50:03');
    $diff = $now->diff($then);
    echo $diff->format('%i minutes %s seconds');
    

    【讨论】:

      【解决方案2】:

      格式化日期

      $diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
      $time = date('i:s', $diff);
      echo $time;
      

      【讨论】:

      • 非常适合我。
      【解决方案3】:

      像 1 和现在 2 一样打发时间

      function diffrencePassTimeAction($DataTime){
          $im = $DataTime - strtotime("now");
        return $im;
      }
      

      未来像 2 和现在 1

      function diffrenceFuturTimeAction($DataTime){
          $im = strtotime("now") - $DataTime;
        return $im;
      }
      

      这个函数删除(-less)

      function diffrencePassTimeAction($DataTime){
          if ($DataTime > 0)
              return $DataTime - strtotime("now");
          else
              return strtotime("now"); // OR return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-02-08
        • 1970-01-01
        • 2011-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-14
        • 1970-01-01
        • 2011-09-12
        相关资源
        最近更新 更多