-1

The date and time format I have is:

Example 1:

$dateTime = 2016-09-08 21:00; //(year-month-day hour:minute)
$interval = 04:00; //(hour:minute)
$outputShouldBe = 2016-09-09 01:00;

Example 2:

$dateTime = 2016-09-08 21:00; //(year-month-day hour:minute)
$interval = 01:40; //(hour:minute)
$outputShouldBe = 2016-09-08 22:40;
Deepak
  • 79
  • 4
  • 14

3 Answers3

3

You can try with strtotime and date function

$dateTime = '2016-09-08 21:00';
echo date( "Y-m-d h:i", strtotime( "2016-09-08 21:00 +4 hours" ) ); 
echo date( "Y-m-d H:i", strtotime( "2016-09-08 21:00 +1 hours 40 minutes" ) ); 
halfer
  • 19,824
  • 17
  • 99
  • 186
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
1

You can also use a DateTime and DateInterval to archieve your task. The code can be made shorter if you desire. But it is more readable like that.

$dt = new Datetime('2016-09-08 21:00');
$interval = new DateInterval('PT4H');
$dt->add($interval);

echo $dt->format('Y-m-d H:i');


$dt = new Datetime('2016-09-08 21:00');
$interval = new DateInterval('PT1H40M');
$dt->add($interval);

echo $dt->format('Y-m-d H:i');
Philipp Palmtag
  • 1,310
  • 2
  • 16
  • 18
0
    $dateTime = '2016-09-08 21:00';
    $intervalhr = 4;
    $intervalmin= 0;
    //display the converted time
    echo date('Y-m-d H:i',strtotime('+'.$intervalhr .' hour ',strtotime($dateTime)));

you can do by giving hour in integer and same for minutes

Gopalakrishnan
  • 957
  • 8
  • 19