如何在 php < 5.3 上为当前日期时间添加 5 分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5608967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add 5 minutes to current datetime on php < 5.3
提问by beginner
I want to add 5 minutes to this date: 2011-04-8 08:29:49
我想为这个日期增加 5 分钟: 2011-04-8 08:29:49
$date = '2011-04-8 08:29:49';
When I use strtotime
I am always getting 1970-01-01 08:33:31
当我使用时,strtotime
我总是得到1970-01-01 08:33:31
How do I add correctly 5 minutes to 2011-04-8 08:29:49
?
如何正确添加 5 分钟2011-04-8 08:29:49
?
回答by Richard Rodriguez
$date = '2011-04-8 08:29:49';
$currentDate = strtotime($date);
$futureDate = $currentDate+(60*5);
$formatDate = date("Y-m-d H:i:s", $futureDate);
Now, the result is 2011-04-08 08:34:49and is stored inside $formatDate
现在,结果是2011-04-08 08:34:49并存储在里面$formatDate
Enjoy! :)
享受!:)
回答by Blender
Try this:
尝试这个:
echo date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime('2011-04-8 08:29:49')));
回答by Chance Sanders
$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;
Results in:
结果是:
2012-09-30 09:00:03
2012-09-30 09:00:03
2012-09-30 09:05:03
2012-09-30 09:05:03
回答by Nidhin
For adding
用于添加
$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
回答by studioromeo
If i'm right in thinking.
如果我的想法是对的。
If you convert your date to a unix timestamp via strtotime(), then just add 300 (5min * 60 seconds) to that number.
如果您通过 strtotime() 将日期转换为 unix 时间戳,则只需将 300(5 分钟 * 60 秒)添加到该数字。
$timestamp = strtotime($date) + (5*60)
Hope this helps
希望这可以帮助
回答by MaxEcho
$date = '2011-04-8 08:29:49';
$newDate = date("Y-m-d H:i:s",strtotime($date." +5 minutes"))
回答by Developer
more illustrative for simple and clear solution
对于简单明了的解决方案更具说明性
$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