-2

I have time like this 10:00:00 AM how to add + two hours with the time.

I have tried this:

$today = "10:00:00 am"; 
$starttime = "02:00:00"; 
$endtime = date("g:i:s a", $starttime+$today);
echo $endtime;
Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65

5 Answers5

3

I vanilla PHP, object style using DataTime()

$date = new DateTime('10:00:00 AM');
$date->modify('+2 hours');
echo $date->format("g:i:s a");

Output:

12:00:00 pm

Or you can try lib Ouzo goodies, and do this in fluent way:

echo Clock::at('10:00:00 AM')->plusHours(2)->format("g:i:s a");

Output:

12:00:00 pm
Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
0

Try this. If it works

 $endtime = date("g:i:s a", strtotime('+2 hours', $starttime + $today));
aldrin27
  • 3,407
  • 3
  • 29
  • 43
0

Try this.

<?php
$today = "10:00:00 am"; 
$endtime = date("g:i:s a", strtotime("+2 hours",strtotime($today)));
echo $endtime;
?>

Add 'x' amount of hours to date

Community
  • 1
  • 1
Surabhil Sergy
  • 1,946
  • 1
  • 23
  • 40
0

Try this:

$today = "10:00:00 am"; 
$starttime = "02:00:00"; 
$endtime = date("g:i:s a", strtotime("+2 hours", $starttime + $today));
echo $endtime;
MarmiK
  • 5,639
  • 6
  • 40
  • 49
Jakir Hossain
  • 2,457
  • 18
  • 23
0

More object oriented approach is using DateTime and DateInterval.

$start_date = new DateTime($result->start_date);
$start_date->add(new DateInterval('P' . (int)$probation_length . 'M'));

Adjust to fit your needs.

Matt Komarnicki
  • 5,198
  • 7
  • 40
  • 92