With the below code I have a problem where I'm getting the 1st and 2nd row just fine, but the 3rd and next rows only give this error:
Notice: Undefined offset: 1 in C:\xampp\htdocs\h_php\addTimes.php on line 19.
<?php
$timearry="";
$timearry=array("1:10","1:40","1:20","0:50");
$i=0;
$day1hours="";
foreach($timearry as $times){
if($i==0){
echo $day1hours= $times;
echo "<br>";
}else{
$day2hours = $times;
$day1=array();
$day1 = explode(":", $day1hours);
$day2 = explode(":", $day2hours);
$totalmins = 0;
$totalmins += $day1[0] * 60;
$totalmins += $day1[1];
$totalmins += $day2[0] * 60;
$totalmins += $day2[1];
$hoursTotal = $totalmins / 60;
$hours=0;
$hours = explode(".", $hoursTotal);
$hours= $hours[0];
$minutes = $totalmins % 60;
echo $day1hours = "$hours".'Hours '."$minutes".' Mints';
echo "<br>";
}
$i++;
}
?>
According to your logic,
Here is the mistake
echo $day1hours = "$hours" . 'Hours ' . "$minutes" . ' Mints';
This line should be
echo $day1hours = $hours . ':'.$minutes;
Output:
1:10
2:50
4:10
5:0
See demo here
That's not an error, it's just a notice, telling you that on the 19th line of your code you're using an offset that doesn't exist.
Assuming the code you posted is complete, this means this line is not working properly:
$totalmins += $day2[1];
because $day2
array doesn't seem to have 2 elements.
Did you check if all the elements from $timearry
are correctly formatted as "H:m"?