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 lots of diffe
$time = date("m/d/Y h:i:s a", time() + 30);
//or
$time = date("m/d/Y h:i:s a", strtotime("+30 seconds"));
What about using strtotime? The code would then be:
strtotime( '+30 second' );
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;
}
$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()
$time = date("m/d/Y h:i:s a", time() + 30);