PHP: add seconds to a date

前端 未结 9 1516
庸人自扰
庸人自扰 2020-12-04 01:22

I have $adate; which contains:

Tue Jan 4 07:59:59 2011

I want to add to this date the following:

$duration=674         


        
9条回答
  •  庸人自扰
    2020-12-04 01:50

    I made this example for a timezone, but if you change some parts it may help you out:

    $seconds_to_add = 30;
    $time = new DateTime();
    $time->setTimezone(new DateTimeZone('Europe/London'));
    $time2 = $time->format("Y/m/d G:i:s");
    $time->add(new DateInterval('PT' . $seconds_to_add . 'S'));
    $timestamp = $time->format("Y/m/d G:i:s");
    echo $timestamp;
    echo '========';
    echo $time2;
    

    Result:

    2018/06/17 3:16:23========2018/06/17 3:15:53
    

提交回复
热议问题