将一天添加到日期的代码返回日期之前的日期: 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

其他回答

你可以使用DateTime()和DateInterval()在今天的日期上添加一天,如下所示:

echo (new DateTime())->add(new DateInterval('P1D'))->format('Y-m-d');

或特定的日期:

$date = '2009-09-30 20:24:00';
echo (new DateTime($date))->add(new DateInterval('P1D'))->format('Y-m-d');

下面的代码使用DateTime类及其方法modify()和format()获得当年1月的第一天(但可以是另一个日期),并在这一天上添加365天(但可以是N天):

echo (new DateTime((new DateTime())->modify('first day of January this year')->format('Y-m-d')))->modify('+365 days')->format('Y-m-d');

试试这个

echo date('Y-m-d H:i:s',date(strtotime("+1 day", strtotime("2009-09-30 20:24:00"))));
<?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 = new DateTime('2000-12-31');

$date->modify('+1 day');
echo $date->format('Y-m-d') . "\n";