Search the Community
Showing results for tags 'time calculations'.
-
Hi folks, Have the following, which is part of a script I am working on to calculate working hours minus break time. However, it's not calculating the correct result. For example, if I worked 12 hours, take away 1 hour break, should be 11 hours. But it will show it as something like 7 hours. Here is the part for Monday: <div class="col-lg-12"> <div class="card"> <div class="card-header"><strong>Add </strong><small>Timesheet <?php if($message = isset($message) ? $message : ''){ printf($message); } ?></small></div> <div class="card-body card-block"> <form role="form" method="post" action=""> <input type="hidden" name="new" value="1" /> <div class="modal-body"> <div class="row form-group"> <div class="col-6"> <label for="driver_name" class=" form-control-label">Driver Name (required)</label> <input type="text" class="form-control" id="driver_name" name="driver_name" placeholder="" value="<?php echo $_SESSION['NAME']; ?>" required > </div> <div class="col-6"> <label for="week_ending" class=" form-control-label">Week Ending (required)</label> <input type="date" class="form-control" id="week_ending" name="week_ending" placeholder="" value="<?php echo isset($_POST['week_ending']) ? $_POST['week_ending'] : '' ?>" required > </div> </div> <hr> <h3>Monday</h3> <div class="row form-group"> <div class="col-4"> <label for="monday_start" class=" form-control-label">Start Time</label> <input type="text" class="form-control" id="monday_start" name="monday_start" placeholder="" value="<?php echo isset($_POST['monday_start']) ? $_POST['monday_start'] : '' ?>" > </div> <div class="col-4"> <label for="monday_start" class=" form-control-label">Break Period</label> <input type="text" class="form-control" id="monday_break" name="monday_break" placeholder="Example: 1:30 = 1hr 30min" value="<?php echo isset($_POST['monday_break']) ? $_POST['monday_break'] : '' ?>" > </div> <div class="col-4"> <label for="monday_finish" class=" form-control-label">Finish Time</label> <input type="text" class="form-control" id="monday_finish" name="monday_finish" placeholder="" value="<?php echo isset($_POST['monday_finish']) ? $_POST['monday_finish'] : '' ?>" > </div> </div> <?php if(isset($_POST['monday_start']) && $_POST['monday_finish'] && $_POST['monday_break'] != "") { $monday_start = new DateTime($_POST['monday_start']); $monday_finish = new DateTime($_POST['monday_finish']); list($h, $m) = explode(":", $_POST['monday_break']); $monday_break = new DateInterval("PT{$h}H{$m}M"); $mondiff = $monday_start->add($monday_break)->diff($monday_finish); ?> <div class="form-group"> <label for="monday" class=" form-control-label">Monday Hours</label> <input type="text" readonly class="form-control" id="monday_hours" name="monday_hours" value="<?php echo $mondiff->format('%H:%I'); ?>"> </div> <?php } ?> Here is the calculating code: <?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 += (int)$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); ?> <div class="row form-group"> <div class="col-6"> <div class="form-group"> <label for="total_hours" class=" form-control-label">Total Hours (required)</label> <input name="total_hours" readonly class="form-control" value= "<?php echo $counter->get_total_time(); ?>" /> </div> </div> <?php } ?> Can someone with the geneius knowledge have a look and point me to where I am going wrong? Also, is there a way, to leave a time field blank and not have the above code crack the poops and call an Undefined Array Key on it? TIA
- 19 replies
-
- php
- time calculations
-
(and 1 more)
Tagged with: