user2330264
user2330264

Reputation: 11

Adding 2 hours to a date where the date and time are stored as 2 separate strings

$date = $feed[$x]['date'];    // Date string = 04-29-2013
$time = $feed[$x]['time'];    // Time string = 11:30pm

I need to add 2 hours to the time but date also has to be correct.

How do I add 2 hours to this string or what ever x hours I need to add to it.

Upvotes: 1

Views: 38

Answers (1)

zerkms
zerkms

Reputation: 254886

$date = '04-29-2013';
$time = '11:30pm';

$compound = $date . ' ' . $time;

$dt = DateTime::createFromFormat('m-d-Y h:ia', $compound);
$dt->modify('+2 hour');

$newDate = $dt->format('m-d-Y');
$newTime = $dt->format('h:ia');

var_dump($newDate, $newTime);

Online demo: http://ideone.com/x4WgDV

Upvotes: 4

Related Questions