Jump to content

Unsupported Operand Types?


Moorcam
Go to solution Solved by Barand,

Recommended Posts

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

Link to comment
Share on other sites

  • Solution

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 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.