moh_abk
moh_abk

Reputation: 2164

Add given time to tomorrows date in PHP

This might be fairly easy but can't seem to figure it out.

This is what I want to get;

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

I want to get $tomorrowsdate with $t appended to it

$tomorrowsdate = 2015-12-01 12:00

So basically I want to be able to add a given time to next day.

Upvotes: 0

Views: 38

Answers (2)

PAlphen
PAlphen

Reputation: 186

Incase you want to use object oriented style use the following;

$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

Upvotes: 0

sinaza
sinaza

Reputation: 820

It's important to define them as a string if you wanna use strtotime. Otherwise, you will get freaky results e.g. the year 1970 ;)

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

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

Upvotes: 1

Related Questions