如何在PHP中添加日期,小时,分钟和秒

其中我们将为使用PHP提供最简单的方式来添加日期,分钟,小时和秒。
在PHP中,使用'date()'和'strtotime()'函数,我们可以轻松增加或者减少时间。
提供的PHP代码允许我们执行以下操作。

  • 添加日期到DateTime。
  • 将小时添加到DateTime。
  • 向日期时间添加分钟。
  • 将秒添加到DateTime。
$startTime = date("Y-m-d H:i:s");
//display the starting time
echo 'Starting Time: '.$startTime;
//add 1 hour to time
$cenvertedTime = date('Y-m-d H:i:s',strtotime('+1 hour',strtotime($startTime)));
//display the converted time
echo 'Converted Time (added 1 hour): '.$cenvertedTime;
//add 1 hour and 30 minutes to time
$cenvertedTime = date('Y-m-d H:i:s',strtotime('+1 hour +30 minutes',strtotime($startTime)));
//display the converted time
echo 'Converted Time (added 1 hour & 30 minutes): '.$cenvertedTime;
//add 1 hour, 30 minutes and 45 seconds to time
$cenvertedTime = date('Y-m-d H:i:s',strtotime('+1 hour +30 minutes +45 seconds',strtotime($startTime)));
//display the converted time
echo 'Converted Time (added 1 hour, 30 minutes  & 45 seconds): '.$cenvertedTime;
//add 1 day, 1 hour, 30 minutes and 45 seconds to time
$cenvertedTime = date('Y-m-d H:i:s',strtotime('+1 day +1 hour +30 minutes +45 seconds',strtotime($startTime)));
//display the converted time
echo 'Converted Time (added 1 day, 1 hour, 30 minutes  & 45 seconds): '.$cenvertedTime;

使用上面的代码,我们可以向当前时间或者任何欲望时间添加时间。
子跟踪使用除“ - '而不是'+'之外的相同代码。

日期:2020-06-02 22:15:25 来源:oir作者:oir