-2

I got some problems handling string and time. I am reading a form which gives me a string like this: "08:00"

Now i am running a foreach loop after which i want to add e.g. 15 minutes to the upper string. I tried to convert the "08:00" to a time with

$string = "08:00";
$time =  date("H:i", strtotime($string)); 
echo $time; //echos 1577260800

How can i add e.g. 15 minutes or even better a string like $add = "10" to the $time? The following doesnt work.

$add = "10";
$newtime = $time + strtotime($add);
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Michael H.
  • 79
  • 1
  • 1
  • 11
  • 2
    in your example code `echo $time;` cannot return 1577260800, fix your question – Anatoliy R Dec 25 '19 at 16:27
  • What is the final result you want to achieve? Do you want after this process to have "08:15" if the initial time was "08:00" and "09:00" if the initial time was "08:45"? – Tulio Faria Dec 25 '19 at 16:37

2 Answers2

1

Just add time in seconds to an existing time.

$string = "08:00"; 
$timeInSeconds = strtotime($string) + 15*60; // 15*60 => 15 minutes in seconds
$time =  date("H:i", $timeInSeconds );  
echo $time; // shows 8:15
InDevX
  • 212
  • 1
  • 9
  • this worked. I just jused $add = 10 as another string and i was able to add it to the $timeinseconds by multiplying it with 60. – Michael H. Dec 26 '19 at 10:44
0

You can just use the strtotime's parsing of words.
Meaning you can just as you ask "add 10 minutes".

$string = "08:00";
$time =  date("H:i", strtotime($string . " +10 minutes")); 
echo $time; //8:10
Andreas
  • 23,610
  • 6
  • 30
  • 62