PHP Date Time Current Time Add Minutes

前端 未结 12 2107
忘掉有多难
忘掉有多难 2020-12-01 07:21

Simple question but this is killing my time.

Any simple solution to add 30 minutes to current time in php with GMT+8?

相关标签:
12条回答
  • 2020-12-01 07:45

    This is an old question that seems answered, but as someone pointed out above, if you use the DateTime class and PHP < 5.3.0, you can't use the add method, but you can use modify:

    $date = new DateTime();
    $date->modify("+30 minutes"); //or whatever value you want
    
    0 讨论(0)
  • 2020-12-01 07:45

    The question is a little old, but I come back to it often ;p

    Another way, which is also a one liner:

    <?= date_create('2111-11-11 00:00:00')->modify("+30 minutes")->format('Y-m-d h:i:s') ?>
    

    Or from timestamp, returns Y-m-d h:i:s:

    <?= date_create('@'.time())->modify("+30 minutes")->format('Y-m-d h:i:s') ?>
    

    Or from timestamp, returns timestamp:

    <?= date_create('@'.time())->modify("+30 minutes")->format('U') ?>
    
    0 讨论(0)
  • 2020-12-01 07:46

    Time 30 minutes later

    $newTime = date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s")." +30 minutes"))
    
    0 讨论(0)
  • 2020-12-01 07:49
    $time = strtotime(date('2016-02-03 12:00:00'));
            echo date("H:i:s",strtotime("-30 minutes", $time));
    
    0 讨论(0)
  • 2020-12-01 07:50

    It looks like you are after the DateTime function add - use it like this:

    $date = new DateTime();
    date_add($date, new DateInterval("PT30M"));
    

    (Note: untested, but according to the docs, it should work)

    0 讨论(0)
  • 2020-12-01 07:51

    I think one of the best solutions and easiest is:

    strtotime("+30 minutes")
    

    Maybe it's not the most efficient but is one of the more understandable.

    0 讨论(0)
提交回复
热议问题