php - add 3 minutes to date variable

后端 未结 2 1452
盖世英雄少女心
盖世英雄少女心 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').

提交回复
热议问题