在给定时间前5分10秒


5 minutes and 10 seconds before given time

我需要弄清楚

  • 给定时间前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

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

// 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。