【问题标题】:5 minutes and 10 seconds before given time [duplicate]给定时间前5分10秒[重复]
【发布时间】:2015-04-03 11:57:57
【问题描述】:

我需要弄清楚

  • 指定时间前 5 分钟
  • 在给定时间前 10 秒 我试过这段代码

    echo $time.'<br />'; 
    echo 'fivemin'.$fivemin= date("H:m:s",strtotime("-5 minutes",strtotime($time))); 
    echo '<br />tensec'.$tensec=$datetime_from = date("H:m:s",strtotime("-10 seconds",strtotime($time)));
    

结果

15:55:44
fivemin 15:04:44
tensec 15:04:34

【问题讨论】:

    标签: php codeigniter datetime


    【解决方案1】:

    您可以为此使用DateTime::sub 方法。

    // Set the initial date/time
    $date = new DateTime('2015-04-03 12:00:00');
    
    // Go back 5 minutes
    $date->sub(new DateInterval('PT5M'));
    
    // $date is now 2015-04-03 11:55:00, go back another 10 seconds...
    $date->sub(new DateInterval('PT10S'));
    

    您也可以将两个潜艇合二为一(我只是将它们拆分,以便您更好地了解正在发生的事情,并且您可能希望在潜艇之间运行一些操作):

    $date->sub(new DateInterval('PT5M10S')); // 5 minutes and 10 seconds ago
    

    这将在 1 次中减去 5 分 10 秒。

    您最终会得到 $date 是一个 DateTime 对象,它现在代表 2015-04-03 11:54:50。

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 2015-02-18
      • 2011-12-17
      • 2019-02-14
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      相关资源
      最近更新 更多