我想在这个日期添加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
?
答案 0 :(得分: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
享受! :)
答案 1 :(得分:33)
试试这个:
echo date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime('2011-04-8 08:29:49')));
答案 2 :(得分:11)
$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
答案 3 :(得分:5)
添加
$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
答案 4 :(得分:4)
如果我思考的话。
如果您通过strtotime()将日期转换为unix时间戳,则只需将300(5分钟* 60秒)添加到该数字。
$timestamp = strtotime($date) + (5*60)
希望这有帮助
答案 5 :(得分:3)
$date = '2011-04-8 08:29:49';
$newDate = date("Y-m-d H:i:s",strtotime($date." +5 minutes"))
答案 6 :(得分:2)
更简洁明了的解决方案
$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