3

At the moment I am writing a calendar. According to the chosen motn ($monthnum), I store the abbreviated month name ($monthabbr) in a database. For that I use a switch-case construct. It works for all months, except for 08-August and 09-September. Since I used the same code for all months, I don't know why it's not working. I am close to the edge to start all over again, but before that I'll better ask if you see an error.

switch( $monthnum ) {
            case 01:
                $monthabbr = 'Jan';
                break;
            case 02:
                $monthabbr = 'Feb';
                break;
            case 03:
                $monthabbr = 'Mär';
                break;
            case 04:
                $monthabbr = 'Apr';
                break;
            case 05:
                $monthabbr = 'Mai';
                break;
            case 06:
                $monthabbr = 'Jun';
                break;
            case 07:
                $monthabbr = 'Jul';
                break;
            case 08:
                $monthabbr = 'Aug';
                break;
            case 09:
                $monthabbr = 'Sep';
                break;
            case 10:
                $monthabbr = 'Okt';
                break;
            case 11:
                $monthabbr = 'Nov';
                break;
            case 12:
                $monthabbr = 'Dez';
                break;
}

3 Answers 3

10

Change 01, 02, ..., 09 to just 1, 2, ..., 9 (drop the zeros).


By starting an integer literal with a 0 indicates that it should be interpreted as an octal number (a number in base 8).

For octal numbers the digits 8 and 9 are illegal.

Further reading:


(Btw, you may want to consider using an array or a map from integer to string, and just look up the string using something like monthAbbrs[$monthnum])

3
  • Right. And therefore 08 and 09 are invalid, 10 is 8 (dec) and 11 is 9 (dec).
    – ckruse
    Commented Feb 20, 2012 at 15:20
  • 010 is 8 (dec) and 011 is 9 (dec), 10 is 10 (dec) and 11 is 11 (dec)
    – Mark Baker
    Commented Feb 20, 2012 at 15:24
  • That solved the problem, thanks! I didn't new about the 0 at the beginning.
    – Sven
    Commented Feb 20, 2012 at 15:32
1

You can avoid this issue by using quotes

switch($date){
    case "01": $month = "January"; break;
    case "02": $month = "February"; break;
    case "03": $month = "March"; break;
    case "04": $month = "April"; break;
    case "05": $month = "May"; break;
    case "06": $month = "June"; break;
    case "07": $month = "July"; break;
    case "08": $month = "August"; break;
    case "09": $month = "September"; break;
    case "10": $month = "October"; break;
    case "11": $month = "November"; break;
    case "12": $month = "December"; break;
}
0

I dont know what the purpose is but a better way is to parse your date

$mysqldate = date( 'Y-m-d H:i:s', $phpdate );
$phpdate = strtotime( $mysqldate );

see here

and then format it in the way you like:

// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');

see here

1
  • Indeed, you can do it like that. But for my purpose, you can select any date using a drop-down menu.
    – Sven
    Commented Feb 20, 2012 at 15:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.