How to add seconds in a PHP date time

前端 未结 2 828
被撕碎了的回忆
被撕碎了的回忆 2021-01-24 17:48

How can I add second(s) in a date time. ex. I have date time 2009-01-14 06:38:18 I need to add -750 seconds in that date and want result in datetime

相关标签:
2条回答
  • 2021-01-24 18:04

    strtotime() also supports some date/time arithmetic.

    $ts = strtotime('2009-01-14 06:38:18 -750 seconds');
    echo date('Y-m-d H:i:s', $ts);

    or e.g.

    $ts = strtotime('2009-01-14 06:38:18 next monday');
    echo date('Y-m-d H:i:s', $ts);
    prints 2009-01-19 00:00:00

    0 讨论(0)
  • 2021-01-24 18:13

    You need to convert that date to a unix timestamp, where such operations are trivial:

    $unix_timestamp = strtotime('2009-01-14 06:38:18');
    $new_string = date('Y-m-d H:i:s', $unix_timestamp - 750);
    
    0 讨论(0)
提交回复
热议问题