Why is my variable uninitialized in a separate if test?

Posted by user2932733 on Stack Overflow See other posts from Stack Overflow or by user2932733
Published on 2013-11-08T21:42:37Z Indexed on 2013/11/08 21:53 UTC
Read the original article Hit count: 303

For the first run, I need to use dates that are six months prior to the last MySQL date $row_recent[0]. For all of the runs after 1, I am using a previous variable to store the previous date and then decrease the date by 6 months. I have confirmed that the first if test produces the expected result. (MySQL date - 6 Months). However, the second if test outputs $startdate6m as the PHP default due to $previous_6m being uninitialized. Any idea why it won't recognize $previous_6m = $initial6m?

<?php
while ($run_number < 15) {
    $run_number++;
    if($run_number == 1){
        if ($month <= 06){
            $year6m = date("Y", strtotime($row_recent[0]))-1;                               
            $month6m = str_pad((12-(6-date("m", strtotime($row_recent[0])))), 2, "0", STR_PAD_LEFT);
            $startdate6m = "'".$year6m."-".$month6m."-01'";
            $end_date = $startdate6m;
            $initial6m = $startdate6m;
        } else{
            $year6m = date("Y", strtotime($row_recent[0]));
            $month6m = str_pad(date("m", strtotime($row_recent[0]))-6, 2, "0", STR_PAD_LEFT);   
            $startdate6m = "'".$year6m."-".$month6m."-01'";
            $end_date = $startdate6m;
            $initial6m = $startdate6m;
        }
    }
    $previous_6m = $initial6m;
    if($run_number > 1){
        #  6 Month
        #  Decrement date by 6 months
        $month6m = date("m", strtotime($previous_6m));
        if ($month6m <= 06){
            $year6m = date("Y", strtotime($previous_6m))-1; 
            $month6m = str_pad((12-(6-date("m", strtotime($previous_6m)))), 2, "0", STR_PAD_LEFT);
            $startdate6m = "'".$year6m."-".$month6m."-01'";
            $end_date = $startdate6m;
        } else{
            $year6m = date("Y", strtotime($previous_6m));
            $month6m = str_pad(date("m", strtotime($previous_6m))-6, 2, "0", STR_PAD_LEFT); 
            $startdate6m = "'".$year6m."-".$month6m."-01'";
            $end_date = $startdate6m;
        }
    }
    $previous_6m = $startdate6m;
}
?>

© Stack Overflow or respective owner

Related posts about php

Related posts about if-statement