Add 30 seconds to the time with PHP

后端 未结 8 2055
旧时难觅i
旧时难觅i 2021-02-05 03:29

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

相关标签:
8条回答
  • 2021-02-05 03:49
    $time = date("m/d/Y h:i:s a", time() + 30);
    
    //or
    
    $time = date("m/d/Y h:i:s a", strtotime("+30 seconds"));
    
    0 讨论(0)
  • 2021-02-05 03:53

    What about using strtotime? The code would then be:

    strtotime( '+30 second' );
    
    0 讨论(0)
  • 2021-02-05 04:03

    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;
    }
    
    0 讨论(0)
  • 2021-02-05 04:04
    $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.

    0 讨论(0)
  • 2021-02-05 04:08

    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 讨论(0)
  • 2021-02-05 04:11
    $time = date("m/d/Y h:i:s a", time() + 30);
    
    0 讨论(0)
提交回复
热议问题