Page 1 of 1

function timeToDecimal

Posted: 2024-09-12 07:32
by tchristophe
Hello,
Php8.1.26
I use this Php function in a hook :
function timeToDecimal ($time){
$split = explode(':', $time);
return ($split[0] + ($split[1]/60) + ($split[2]/3600));
}

and it return "fatal error: Uncaught TypeError: Unsupported operand types: string + int in C:\wamp64\www\...."
Any idea about the problem ?
Note : the function works well out of AppGini...
Thanks !
Regards
Chris

Re: function timeToDecimal

Posted: 2024-09-12 08:29
by jsetzer
return ($split[0] + ($split[1]/60) + ($split[2]/3600));
}
explode() function returns a string array.

Therefore, $split[0] to $split[n] are strings, not numbers.

For string concat use ., not +.

For adding numbers, use + on numeric values, not on strings.

For casting strings into integers, you can use intValue function.

But don't forget about leading zeroes: string '09' becomes (int)9.

Also watch out for the number of decimals when dividing by 60 or by 3600.

In that function you mix up adding numbers and concatenating strings. First part is a string, 2nd and 3rd are numbers.

For me it is not clear what output you expect. You may post the expected input and output and we can help fixing the code.

Re: function timeToDecimal

Posted: 2024-09-12 10:24
by tchristophe
Thank jsetzer for your answer !
I work on it !

Re: function timeToDecimal (Resolved)

Posted: 2024-09-13 08:32
by tchristophe
Hey,
The code below works fine :

Code: Select all

// Calcul facturation horaire
function timeToDecimal ($time){
 $split = explode(':', $time);
 return ($split[0]) + ($split[1]/60) + ($split[2]/3600);
}
$heures_decimales = timeToDecimal($tot_tmp_visite);
$fact_horaire = $heures_decimales * $tarif_horaire;
$data['fact_horaire'] = $fact_horaire;
Hope it could help...
Christophe