Azulitabijou
Azulitabijou

Reputation: 109

Broken switch statement

Ive been searching and staring for hours and I'm at a loss. I have the following code that when an august date is passed through the switch (as AUG) statement it keeps returning a 0 for the value instead of an 8. The echo results are below the code block. Any ideas with what might be going wrong? It seemed to work well for July and broke on August 1st.

$when=date("Y-m-d");
    $created=date("Ymd");
    $date=explode('-',$emprow->HIRE_DATE);
    echo "<br>hire date is ";print_r($date);echo "<br>";
    switch ($date[1])
    {
        case "JAN":
            $date[1]=01;
            break;
        case "FEB":
            $date[1]=02;
            break;
        case "MAR":
            $date[1]=03;
            break;
        case "APR":
            $date[1]=04;
            break;
        case "MAY":
            $date[1]=05;
            break;
        case "JUN":
            $date[1]=06;
            break;
        case "JUL":
            $date[1]=07;
            break;
        case "AUG":
            $date[1]=08;
            break;
        case "SEP":
            $date[1]=09;
            break;
        case "OCT":
            $date[1]=10;
            break;
        case "NOV":
            $date[1]=11;
            break;
        case "DEC":
        case "Dec":
            $date[1]=12;
            break;
    }
    echo "<br>hire date is ";print_r($date);echo "<br>";
    $startdate=sprintf("%04d%02d%02d",$date[2],$misc->getmonthnum($date[1]),$date[0]);

Which results in the following:

hire date is Array ( [0] => 05 [1] => AUG [2] => 2013 )

hire date is Array ( [0] => 05 [1] => 0 [2] => 2013 )

startdate date is 20130005

Upvotes: 0

Views: 136

Answers (4)

TecBrat
TecBrat

Reputation: 3729

I verified what I put in comments.

08 is a non-existant octal number. Use "08" or 8

You can pad with zeros when you're ready to output it.

Here's an illustration of what's going on in PHP with your original code:

$date[1]=08;
echo $date[1];
echo ' | ';
$date[1]=010;
echo $date[1];

output:

0 | 8

Upvotes: 1

Ghabriel Nunes
Ghabriel Nunes

Reputation: 372

Just remove those extra zeroes on the left (i.e change 08 to 8, 09 to 9 and so on) or turn them into strings. If a number has an extra zero on the left it is interpreted as an octal number (base 8 number system), in which the numbers 8 and 9 are invalid, breaking your code.

Upvotes: 1

sgbj
sgbj

Reputation: 2274

TecBrat is correct. Your number (08) is being interpreted as an octal literal, and since there is no 8 digit in base 8, the number is invalid and therefore taken to be 0. To fix this, just put quotes around your numbers so they're treated as strings.

Alternatively, you could simply do something like

echo date_create('05-AUG-2013')->format('m'); // prints 08

Upvotes: 1

timtj
timtj

Reputation: 94

When calling your Hire Date, you use $emprow->HIRE_DATE, from what I see.

You explode such date, delimiter "-" to break it into 3 arrays, y,m,d, and then attempt parsing the 'm'. I suggest the following:

$date=$emprow->HIRE_DATE;
$unixtime=strtotime($date);
$newdate=date('m',$unixtime);

This then should give you as a result (when echoing $newdate) "08"

Upvotes: 1

Related Questions