用PHP将给定的时间添加到明天的日期中


Add given time to tomorrows date in PHP

这可能相当容易,但似乎无法解决。

这就是我想要得到的;

$t = 12:00
$todaysdate = 2015-11-30

我想得到附加了$t$tomorrowsdate

$tomorrowsdate = 2015-12-01 12:00

所以基本上我希望能够在第二天增加一个给定的时间。

如果要使用strtotime,将它们定义为字符串非常重要。否则,你会得到奇怪的结果,例如1970年;)

$t = '12:00';
$todaysdate = '2015-11-30';
$tomorrowsdate = date('Y-m-d', strtotime("{$todaysdate} + 1 day")) . " {$t}";
echo $tomorrowsdate;

如果您想使用面向对象的样式,请使用以下内容;

$today = new DateTime(); //today (2015-12-29)
$tommorow = $today->add(new DateInterval('P1D')); // add one day
echo $tommorow->format('Y-m-d H:i'); // echo results in desired format eg. 2015-12-30 12:36
// incase that 12:00 is important..
$tommorow->setTime(12,0);
echo $tommorow->format('Y-m-d H:i'); // 2015-12-30 12:00