Adding minutes to date time in PHP

Adding minutes to date time in PHP

Asked on December 25, 2018 in PHP.
Add Comment


  • 3 Answer(s)

    Try to use the code below

    $minutes_to_add = 5;
     
    $time = new DateTime('2011-11-17 05:05');
    $time->add(new DateInterval('PT' . $minutes_to_add . 'M'));
     
    $stamp = $time->format('Y-m-d H:i');
    
    Answered on December 25, 2018.
    Add Comment

    A useful modification method in PHP’s DateTime class which takes in easy-to-understand text like below code.

    $dateTime = new DateTime('2011-11-17 05:05');
    $dateTime->modify('+5 minutes');
    

    Use string interpolation or concatenation to make guideline it,

    $dateTime = new DateTime('2011-11-17 05:05');
    $minutesToAdd = 5;
    $dateTime->modify("+{$minutesToAdd} minutes");
    
    Answered on December 25, 2018.
    Add Comment

    Always try to use native functions with Datetime class method.

    strtotime('+59 minutes', strtotime('2011-11-17 05:05'));
    
    
    Answered on December 25, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.