开发者

Add 30 seconds to the time with PHP

开发者 https://www.devze.com 2023-01-03 11:35 出处:网络
How can I add 30 seconds to this time? $time = date(\"m/d/Y h:i:s a\", time()); I wasn\'t sure how to do it because it is showing l开发者_如何学编程ots of different units of time, when I only want

How can I add 30 seconds to this time?

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

I wasn't sure how to do it because it is showing l开发者_如何学编程ots of different units of time, when I only want to add 30 seconds.


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


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

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;
}


What about using strtotime? The code would then be:

strtotime( '+30 second' );


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

//or

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


General :

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


See mktime:

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

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

should do what you want.


$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.


You could solve this with a bit different perspective. First, you can create a current datetime object. Then, you create a future datetime which is 30 seconds later then your given datetime. So it boils down to the following:

(new Future(
    new Now(),
    new NSeconds(30)
))
    ->value();

If you want to output in a specific format you've mentioned, it can be achieved with

(new ISO8601Formatted(
    new Future(
        new Now(),
        new NSeconds(30)
    ),
    'm/d/Y h:i:s a'
))
    ->value()
0

精彩评论

暂无评论...
验证码 换一张
取 消