使用 PHP 为时间添加 30 秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3052865/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:32:54  来源:igfitidea点击:

Add 30 seconds to the time with PHP

phptime

提问by Sam

How can I add 30 seconds to this time?

我怎样才能在这个时间上增加 30 秒?

$time = date("m/d/Y h:i:s a", time());

I wasn't sure how to do it because it is showing lots of different units of time, when I only want to add 30 seconds.

我不知道该怎么做,因为它显示了很多不同的时间单位,而我只想添加 30 秒。

回答by Artefacto

$time = date("m/d/Y h:i:s a", time() + 30);

回答by dmp

If you're using php 5.3+, check out the DateTime::add operationsor modify, really much easier than this.

如果您使用的是 php 5.3+,请查看DateTime::add 操作modify,真的比这容易得多。

For example:

例如:

$startTime = new DateTime("09:00:00");
$endTime = new DateTime("19:00:00");


while($startTime < $endTime) {

$startTime->modify('+30 minutes'); // can be seconds, hours.. etc

echo $startTime->format('H:i:s')."<br>";
break;
}

回答by Martijn

What about using strtotime? The code would then be:

使用 strtotime 怎么样?代码将是:

strtotime( '+30 second' );

回答by Alex

$time = date("m/d/Y h:i:s a", time() + 30);

//or

$time = date("m/d/Y h:i:s a", strtotime("+30 seconds"));

回答by Ivoglent Nguyen

General :

一般的 :

$add_time=strtotime($old_date)+30;
$add_date= date('m/d/Y h:i:s a',$add_time);

回答by Digital Human

$time = date("m/d/Y h:i:s", time());
$ts = strtotime($time);
$addtime = date("m/d/Y h:i:s", mktime(date("h", $ts),date("i", $ts),date("s", $ts)+30,date("Y", $ts),date("m", $ts),date("d", $ts));

Would be a more explained version of all of the above.

将是上述所有内容的更详细解释版本。

回答by Bob Fincheimer

See mktime:

见mktime:

mktime (date("H"), date("i"), date("s") + 30)

http://www.php.net/manual/en/function.mktime.php

http://www.php.net/manual/en/function.mktime.php

should do what you want.

应该做你想做的。