Developer
Developer

Reputation: 1050

Adding date or time in datetime format to another datetime

WHAT I AM DOING

I want to find the difference between 2 datetime and add it to another datetime. I am only able to get the difference in Y-m-d H:i:s format.

CODE

    $begin = new DateTime($start);
    $finish =  new DateTime($end);
    $diff = $begin->diff($finish);
    $difference = $diff->format("%Y-%M-%D %H:%I:%S");

Here I want to add $difference to another datetime say $finaldate. If its not possible is there any way of getting the difference in only minutes, then i could use $date->modify("+$difference minutes");

Upvotes: 0

Views: 188

Answers (2)

gries
gries

Reputation: 1143

*This is a method using DateTime:*

$begin = new DateTime($start);
$finish =  new DateTime($end);


$difference = $finish->format('U') - $begin->format('U');

// working version
$minutesDiff = round(($difference/60), 0);
$finalDate = new DateTime();
$finalDate->modify(sprintf('+%s minutes', $minutesDiff));

edit added missing bracket

edit2 version without ->diff() method

Upvotes: 1

Fallen
Fallen

Reputation: 4565

What about:

$begin = strtotime($start);
$finish= strtotime($end);
$diff  = $finish-$begin;
$finaldate = strtotime($finaldate)+$diff;
echo date("Y-M-D h-i-s",$finaldate);

Upvotes: 0

Related Questions