php - add 3 minutes to date variable

后端 未结 2 1451
盖世英雄少女心
盖世英雄少女心 2021-01-16 02:11

I want to add 3 minutes to a date/time variable I have, but I\'m not sure how to do this. I made the variable from a string like this: (which is in the RFC 2822 date format

相关标签:
2条回答
  • 2021-01-16 02:49

    Instead of

    $time = DateTime::createFromFormat("Y-m-d\TH:i:sO", $date);
    $time = $time->add(new DateInterval('P2H'));
    

    try (for adding 3 minutes)

    $time = DateTime::createFromFormat("Y-m-d\TH:i:sO", $date);
    $time->add(new DateInterval('PT3M'));
    

    First, since you're using PHP's DateTime class, you don't need to assign the output of the add method to a variable - it will modify the DateTime you passed into the constructor. Second, if you're making modifications to time using the same class, you have to make sure there's a T before your time definition. For your example, DateInterval('P2H') is invalid - it should be DateInterval('PT2H').

    0 讨论(0)
  • 2021-01-16 03:08

    You could just use strtotime twice:

    $date = strtotime('2011-10-18T19:56:00+0200');
    echo date('G:i', strtotime('+3 minutes', $date));
    
    0 讨论(0)
提交回复
热议问题