将一天添加到日期的代码返回日期之前的日期: 2009-09-30 20:24:00日期后增加一天应滚动到下个月:1970-01-01 17:33:29

<?php

    //add day to date test for month roll over

    $stop_date = date('Y-m-d H:i:s', strtotime("2009-09-30 20:24:00"));

    echo 'date before day adding: '.$stop_date; 

    $stop_date = date('Y-m-d H:i:s', strtotime('+1 day', $stop_date));

    echo ' date after adding one day. SHOULD be rolled over to the next month: '.$stop_date;
?>

我之前用过类似的代码,这里我做错了什么?


当前回答

由于我们经常通过API从另一个时区接收ISO字符串,我们可以动态地将其转换为本地时间:

// The machine local time is GMT+2, but we received a date at GMT+0 (UTC)
echo date(DATE_ISO8601, strtotime("-1 hour", strtotime("2022-08-17T23:25:51-00:00")));
// 2022-08-18T00:25:51+0200

其他回答

<?php

function plusTimetoOldtime($Old_Time,$getFormat,$Plus_Time) {
    return date($getFormat,strtotime(date($getFormat,$Old_Time).$Plus_Time));
}

$Old_Time = strtotime("now");
$Plus_Time = '+1 day';
$getFormat = 'Y-m-d H:i:s';

echo plusTimetoOldtime($Old_Time,$getFormat,$Plus_Time);

?>

这招对我很管用: 当前日期

$date = date('Y-m-d', strtotime("+1 day"));

anydate:

date('Y-m-d', strtotime("+1 day", strtotime($date)));

由于我们经常通过API从另一个时区接收ISO字符串,我们可以动态地将其转换为本地时间:

// The machine local time is GMT+2, but we received a date at GMT+0 (UTC)
echo date(DATE_ISO8601, strtotime("-1 hour", strtotime("2022-08-17T23:25:51-00:00")));
// 2022-08-18T00:25:51+0200

我总是加86400秒(一天):

$stop_date = date('Y-m-d H:i:s', strtotime("2009-09-30 20:24:00") + 86400);

echo 'date after adding 1 day: '.$stop_date; 

这可能不是你能做到的最巧妙的方法,但它是有效的!

<?php
$stop_date = '2009-09-30 20:24:00';
echo 'date before day adding: ' . $stop_date; 
$stop_date = date('Y-m-d H:i:s', strtotime($stop_date . ' +1 day'));
echo 'date after adding 1 day: ' . $stop_date;
?>

对于PHP 5.2.0+,你也可以这样做:

$stop_date = new DateTime('2009-09-30 20:24:00');
echo 'date before day adding: ' . $stop_date->format('Y-m-d H:i:s'); 
$stop_date->modify('+1 day');
echo 'date after adding 1 day: ' . $stop_date->format('Y-m-d H:i:s');