Moorcam Posted September 16, 2021 Share Posted September 16, 2021 Howdy folks, Calculating times between start and finish. Have them working but when calculating total hours - breaks I get the following error: Fatal error: Uncaught TypeError: Unsupported operand types: int + string in Here is the PHP code for the calculations: <?php if(isset($_POST['calculate']) != ""){ class times_counter { private $hou = 0; private $min = 0; private $sec = 0; private $totaltime = '00:00:00'; public function __construct($times){ if(is_array($times)){ $length = sizeof($times); for($x=0; $x <= $length; $x++){ $split = explode(":", @$times[$x]); $this->hou += @$split[0]; $this->min += @$split[1]; $this->sec += @$split[2]; } $seconds = $this->sec % 60; $minutes = $this->sec / 60; $minutes = (integer)$minutes; $minutes += $this->min; $hours = $minutes / 60; $minutes = $minutes % 60; $hours = (integer)$hours; $hours += $this->hou % 24; $this->totaltime = $hours.":".$minutes; } } public function get_total_time(){ return $this->totaltime; } } $times = array( $mondiff->format('%H:%I'), $tuediff->format('%H:%I'), $weddiff->format('%H:%I'), $thurdiff->format('%H:%I'), $fridiff->format('%H:%I'), $satdiff->format('%H:%I'), $sundiff->format('%H:%I'), ); $counter = new times_counter($times); ?> The line that is throwing the error is: $this->hou += @$split[0]; Any help with this would be appreciated. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/313745-unsupported-operand-types/ Share on other sites More sharing options...
Solution Barand Posted September 16, 2021 Solution Share Posted September 16, 2021 PHP v8 doesn't let you mix indeterminate types, such as adding an empty string to an int. $a = 1; $b = '2'; $c = ''; echo $a + $b; // 3 OK echo $a + $c; // Unsupported operand types: int + string echo $a + (int)$c; // 1 OK Use a cast to int. $this->hou += (int)$split[0]; That said, you really are doing the calculations the hard way $t1 = '09:30:00'; $t2 = '17:15:00'; $dt1 = new DateTime($t1); $dt2 = new DateTime($t2); echo $dt2->diff($dt1)->format('%H:%I:%S'); // 07:45:00 Quote Link to comment https://forums.phpfreaks.com/topic/313745-unsupported-operand-types/#findComment-1589955 Share on other sites More sharing options...
Moorcam Posted September 16, 2021 Author Share Posted September 16, 2021 8 minutes ago, Barand said: PHP v8 doesn't let you mix indeterminate types, such as adding an empty string to an int. $a = 1; $b = '2'; $c = ''; echo $a + $b; // 3 OK echo $a + $c; // Unsupported operand types: int + string echo $a + (int)$c; // 1 OK Use a cast to int. $this->hou += (int)$split[0]; That said, you really are doing the calculations the hard way $t1 = '09:30:00'; $t2 = '17:15:00'; $dt1 = new DateTime($t1); $dt2 = new DateTime($t2); echo $dt2->diff($dt1)->format('%H:%I:%S'); // 07:45:00 Thanks mate. Now getting this: Warning: Undefined array key 2 in D:\xampp\htdocs\protour\admin\add-timesheet.php on line 351 Warning: Undefined array key 1 in D:\xampp\htdocs\protour\admin\add-timesheet.php on line 350 Quote Link to comment https://forums.phpfreaks.com/topic/313745-unsupported-operand-types/#findComment-1589956 Share on other sites More sharing options...
Moorcam Posted September 16, 2021 Author Share Posted September 16, 2021 Never mind. Stupid Irish Brain Syndrome. All good now with errors. Quote Link to comment https://forums.phpfreaks.com/topic/313745-unsupported-operand-types/#findComment-1589957 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.