Moorcam Posted July 14, 2021 Share Posted July 14, 2021 Hi guys, I am trying to calculate hours a person works by calculating the values of text fields in a form. However, when I load the page I get "Class 'times_counter' not found. Here is the calculation code" 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'), $tudiff->format('%H:%I'), $weddiff->format('%H:%I'), $thdiff->format('%H:%I'), $fridiff->format('%H:%I'), $satdiff->format('%H:%I'), $sundiff->format('%H:%I'), ); $counter = new times_counter($times); I had this on an old project, which I no longer have but it worked then. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/313388-class-times_counter-not-found/ Share on other sites More sharing options...
kicken Posted July 14, 2021 Share Posted July 14, 2021 Your class definition (class time_counter { ... }) should not be inside your if statement. Move it outside to the top level. There's no reason for it's declaration to be conditional. No processing gets done when you declare the class, only when you instantiate it (new time_counter()). So, declare the class unconditionally, instantiate it conditionally if needed. Quote Link to comment https://forums.phpfreaks.com/topic/313388-class-times_counter-not-found/#findComment-1588339 Share on other sites More sharing options...
Moorcam Posted July 14, 2021 Author Share Posted July 14, 2021 6 minutes ago, kicken said: Your class definition (class time_counter { ... }) should not be inside your if statement. Move it outside to the top level. There's no reason for it's declaration to be conditional. No processing gets done when you declare the class, only when you instantiate it (new time_counter()). So, declare the class unconditionally, instantiate it conditionally if needed. Hey kicken, Thanks for the reply. I did that and now get this: PHP Parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION) or const (T_CONST) Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/313388-class-times_counter-not-found/#findComment-1588340 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.