How to add 5 minutes to current datetime on php < 5.3


How to add 5 minutes to current datetime on php < 5.3

我想在此日期后增加5分钟:2011-04-8 08:29:49

$date = '2011-04-8 08:29:49';

当我使用strtotime时,我总是得到1970-01-01 08:33:31

如何将5分钟正确添加到2011-04-8 08:29:49

$date = '2011-04-8 08:29:49';
$currentDate = strtotime($date);
$futureDate = $currentDate+(60*5);
$formatDate = date("Y-m-d H:i:s", $futureDate);

现在,结果是2011-04-08 08:34:49并存储在$formatDate

享受吧!:(

试试这个:

echo date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime('2011-04-8 08:29:49')));
$expire_stamp = date('Y-m-d H:i:s', strtotime("+5 min"));
$now_stamp    = date("Y-m-d H:i:s");
echo "Right now: " . $now_stamp;
echo "5 minutes from right now: " . $expire_stamp;

结果:

2012-09-30 09:00:03

2012-09-30 09:05:03

$date = '2011-04-8 08:29:49';
$newDate = date("Y-m-d H:i:s",strtotime($date." +5 minutes"))

用于添加

$date = new DateTime('2014-02-20 14:20:00');
$date->add(new DateInterval('P0DT0H5M0S'));
echo $date->format('Y-m-d H:i:s');
It add 5minutes
For subtracting
$date = new DateTime('2014-02-20 14:20:00');
$date->sub(new DateInterval('P0DT0H5M0S'));
echo $date->format('Y-m-d H:i:s');
It subtract 5 minutes

如果我的想法正确。

如果您通过strtotime((将日期转换为unix时间戳,那么只需在这个数字上加300(5分钟*60秒(。

$timestamp = strtotime($date) + (5*60)

希望这能帮助

简单清晰的解决方案更具说明性

    $date = '2011-04-8 08:29:49';
    $newtimestamp = strtotime($date. ' + 5 minute');//gets timestamp
    //convert into whichever format you need 
    $newdate = date('Y-m-d H:i:s', $newtimestamp);//it prints 2011-04-08 08:34:49