Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/313388-class-times_counter-not-found/
Share on other sites

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.

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.

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.