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

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


当前回答

试试这个

echo date('Y-m-d H:i:s',date(strtotime("+1 day", strtotime("2009-09-30 20:24:00"))));

其他回答

虽然我同意Doug Hays的回答,但我要在这里说一下,您的代码不工作的原因是因为strtotime()期望将INT作为第二个参数,而不是字符串(甚至是表示日期的字符串)

如果你打开最大错误报告,你会看到这是一个E_NOTICE级别的“非格式良好的数值”错误。

简单的阅读和理解方式:

$original_date = "2009-09-29";

$time_original = strtotime($original_date);
$time_add      = $time_original + (3600*24); //add seconds of one day

$new_date      = date("Y-m-d", $time_add);

echo $new_date;

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

$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

试试这个

echo date('Y-m-d H:i:s',date(strtotime("+1 day", strtotime("2009-09-30 20:24:00"))));