代码之家  ›  专栏  ›  技术社区  ›  Sam Johannes Rudolph

用PHP为时间增加30秒

  •  20
  • Sam Johannes Rudolph  · 技术社区  · 14 年前

    我该怎么加30秒呢?

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

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

    7 回复  |  直到 7 年前
        1
  •  58
  •   Artefacto    14 年前
    $time = date("m/d/Y h:i:s a", time() + 30);
    
        2
  •  16
  •   danp    14 年前

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

    例如:

    $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;
    }
    
        3
  •  9
  •   Martijn    7 年前

    用strtotime怎么样?代码将是:

    strtotime( '+30 second' );
    
        4
  •  4
  •   Alex    14 年前
    $time = date("m/d/Y h:i:s a", time() + 30);
    
    //or
    
    $time = date("m/d/Y h:i:s a", strtotime("+30 seconds"));
    
        5
  •  0
  •   Bob Fincheimer    14 年前

    见MKTIME:

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

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

    应该做你想做的。

        6
  •  0
  •   Digital Human    14 年前
    $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));
    

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

        7
  •  0
  •   Ivoglent Nguyen    11 年前

    一般:

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