function timeToDecimal

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
tchristophe
Posts: 10
Joined: 2019-10-22 14:13

function timeToDecimal

Post by tchristophe » 2024-09-12 07:32

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

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1869
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: function timeToDecimal

Post by jsetzer » 2024-09-12 08:29

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.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.14 Revision 1665 + all AppGini Helper tools

tchristophe
Posts: 10
Joined: 2019-10-22 14:13

Re: function timeToDecimal

Post by tchristophe » 2024-09-12 10:24

Thank jsetzer for your answer !
I work on it !

tchristophe
Posts: 10
Joined: 2019-10-22 14:13

Re: function timeToDecimal (Resolved)

Post by tchristophe » 2024-09-13 08:32

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

Post Reply