将一天添加到日期

新手上路,请多包涵

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

我以前使用过非常相似的代码,我在这里做错了什么?

原文由 ian 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 323
2 个回答
<?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');

原文由 w35l3y 发布,翻译遵循 CC BY-SA 3.0 许可协议

由于我们经常通过 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

原文由 NVRM 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题