Jump to content

Script posting wrong date


LeeQ

Recommended Posts

Can anyone tell me why this goes backwards 1 day every time it loops.

 

 

Sample Output:

Mixologist Date Reason Filling In Excused

LQ August 25, 2017 Test Auto Yes  

LQ August 19, 2017 Test Auto Yes  

LQ August 13, 2017 Test Auto Yes  

 

This is supposed to be every Sunday starting Sunday, August 13th

private function scheduleAbsent($filling, $before, $after) {

        //$slot[0]['day'] = Monday 
        //$slot[6]['day'] = Sunday
        $this->db->from('rp_timetable');
        $slot = $this->db->get()->result_array();


        $cycle_month = $this->input->post('amonth');
        $cycle_day = $this->input->post('aday');
        $cycle_year = $this->input->post('ayear');
        $DayOfWeek = date("N", mktime(0, 0, 0, $cycle_month, $cycle_day, $cycle_year)); //number of the week 0 = Monday 6 = Sunday
        $days_in_month = cal_days_in_month(CAL_GREGORIAN, $cycle_month, $cycle_year);
        
        $firstSlot = $this->input->post('slot1');
        $secondSlot = $this->input->post('slot2');
            
        if(!isset($firstSlot) OR !is_numeric($firstSlot) AND !isset($secondSlot) OR !is_numeric($secondSlot)) {
            $slots = '1,24';
        } else {
            $slots = $firstSlot.','.$secondSlot;
        }

        for ($b = $DayOfWeek; $b <= 7; $b++) {
            
            $s = $b-1;
            
            //check if djName in array
            if (in_array($this->input->post('djName'), $slot[$s])) {

                //find key for $slot[$b] equal to djName
                $shifts = array_keys($slot[$s], $this->input->post('djName'));
                $NumberOfShifts = count($shifts);
                
                $weekdayBefore = $s;
                $weekdayAfter = $s;
                
                $shiftBefore = $shifts[0]-1;
                $shiftAfter = $shifts[0]+$NumberOfShifts;
                
                $month = date("F", mktime(0, 0, 0, $cycle_month, $cycle_day, $cycle_year));
                
                $insertData = array(
                    'djname'    => $this->input->post('djName'), 
                    'month'     => $month,
                    'day'       => $cycle_day,
                    'year'      => $cycle_year,
                    'reason'    => $this->input->post('reason'), 
                    'slots'     => $slots,
                    'filling'   => $filling,
                    'excused'   => $this->input->post('excused')
                );
                
                $this->db->insert('rp_absent', $insertData);
                $insertID = $this->db->insert_id();
                
                
                if($shiftBefore <= 0) {
                    $shiftBefore = 24;
                    $weekdayBefore--;
                    $dayBefore = date("j", mktime(0, 0, 0, $cycle_month, $cycle_day-1, $cycle_year));
                    $monthBefore = $date = date("F", mktime(0, 0, 0, $cycle_month, $cycle_day-1, $cycle_year));
                    $yearBefore = $date = date("Y", mktime(0, 0, 0, $cycle_month, $cycle_day-1, $cycle_year));
                    if($weekdayBefore < 0) {
                        $weekdayBefore = 6;
                    }
                } else{
                    $dayBefore = $cycle_day;
                    $monthBefore = $month;
                    $yearBefore = $cycle_year;                    
                }
                
                
                if($shiftAfter >= 25) {
                    $shiftAfter = 1;
                    $weekdayAfter++;
                    $dayAfter = date("j", mktime(0, 0, 0, $cycle_month, $cycle_day+1, $cycle_year));
                    $monthAfter = $date = date("F", mktime(0, 0, 0, $cycle_month, $cycle_day+1, $cycle_year));
                    $yearAfter = $date = date("Y", mktime(0, 0, 0, $cycle_month, $cycle_day+1, $cycle_year));
                    if($weekdayAfter >= 6) {
                        $weekdayAfter = 0;
                    }
                } else{
                    $dayAfter = $cycle_day;
                    $monthAfter = $month;
                    $yearAfter = $cycle_year;                    
                }
                
                $slotBefore = $slot[$weekdayBefore][$shiftBefore];
                $slotAfter = $slot[$weekdayAfter][$shiftAfter];
                $date = date("l, F j, Y", mktime(0, 0, 0, $cycle_month, $cycle_day, $cycle_year));
                
                
                if(isset($slotBefore) AND $slotBefore != '' AND $slotBefore != 'RESERVED') {
                    $isAbsent = $this->checkAbsent($slotBefore,$monthBefore,$dayBefore,$yearBefore);
                }
                
                if(isset($slotAfter) AND $slotAfter != '' AND $slotAfter != 'RESERVED') {
                    $isAbsent = $this->checkAbsent($slotAfter,$monthAfter,$dayAfter,$yearAfter);
                }               
                
            }

            if ($b >= 7) {
                $b = 1;
            }

            $cycle_day++;
            if ($cycle_day >= $days_in_month) {
                $cycle_day = 1;
                $cycle_month++;
                if ($cycle_month >= 13) {
                    $cycle_month = 1;
                    $cycle_year++;
                }
                $days_in_month = cal_days_in_month(CAL_GREGORIAN, $cycle_month, $cycle_year)+1;
            }

            if ($cycle_month == $this->input->post('rmonth') AND $cycle_day >= $this->input->post('rday') AND $cycle_year == $this->input->post('ryear')) {
                break;
            }            
        }
    }
    
    private function checkAbsent($mixologist,$amonth,$aday,$ayear) {
        
        $this->db->where('djname', $mixologist);
        $this->db->where('month', $amonth);
        $this->db->where('day', $aday);
        $this->db->where('year', $ayear);
        
        $count = $this->user_model->count_results('rp_absent');
        
        if(isset($count) AND is_numeric($count) AND $count != 0) {            
            
            $this->db->where('djname', $mixologist);
            $this->db->where('month', $amonth);
            $this->db->where('day', $aday);
            $this->db->where('year', $ayear);
            $absent = $this->user_model->get_data('filling', 'rp_absent', NULL, NULL, NULL, NULL);
            
            if ($absent[0]['filling'] != 'Auto') {
                return $absent[0]['filling'];                              
            } else {
                return NULL;
            }
            
        }
        return NULL;
    }

Link to comment
Share on other sites

Right, but originally $b is based on $DayOfWeek which is 0-7. When you reset $b you need to go all the way back to 0, not 1.

 

That

$s = $b-1;
is also at fault: when $b = $DayOfWeek = Monday (in the first pass through the loop) $s = -1 and that will never exist in $slot.

 

So either reset $b=0 and let the $s bug stay, or go through the code and make sure you're using a base of 0 or 1 for every place that uses $b.

Link to comment
Share on other sites

I get the same result if I make $b start at 0. $DayOfWeek = 0-6.

 

I did make a change:

if ($b >= 6) {
                $b = 0;
                $cycle_day++;
             }

This seems to work till the month changes then its one day a head.

Link to comment
Share on other sites

The $b++ at the top of the loop is still going to happen. And putting $cycle_day++ into that doesn't make any sense at all - yeah, it increments twice to make up for the missing $b day, but that doesn't justify it being there.

 

The code is already plenty messy and long past the point of needing to be refactored, but fixing this should be close. If the first week works then $b=0..6 and $s=-1..5 works. So keep it in that range.

 

Drop the $b

Then deal with the $b++ problem. Resetting $b anywhere after it's used (which is just with the $s=$b-1 statement) won't be enough because it'll increment with the next loop. Drop the increment and

$s = $b - 1;
$b = ($b + 1) % 7;
Link to comment
Share on other sites

The code is already plenty messy and long past the point of needing to be refactored, but fixing this should be close. If the first week works then $b=0..6 and $s=-1..5 works. So keep it in that range.

 

Yeah, you can save yourself a lot of work and headache by using DateTime objects.

$dt = new DateTime('August 13, 2017', new DateTimeZone('America/New_York'));
$int = new DateInterval('P7D');
do{
	print("<p>{$dt->format('M j, Y')}</p>");
	$dt->add($int);
}while($dt->format('m') < 11)

Obviously this doesn't handle any of the absent/present logic in your code, but it certainly does make dealing with the time interval easier to read and hopefully it gets the point across.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.