I'll get right to the point Here is my code for generating the name of the month based on what the number of the month is. function displayMonth($num) { if ($num = 1) {$month = 'January';} else if ($num = 2) {$month = 'February';} else if ($num = 3) {$month = 'March';} else if ($num = 4) {$month = 'April';} else if ($num = 5) {$month = 'May';} else if ($num = 6) {$month = 'June';} else if ($num = 7) {$month = 'July';} else if ($num = 8) {$month = 'August';} else if ($num = 9) {$month = 'September';} else if ($num = 10) {$month = 'October';} else if ($num = 11) {$month = 'November';} else if ($num = 12) {$month = 'December';} else {echo '(!) Month Calculator error!';} echo $month; } Code (markup): Now for some reason it only displays it as January no matter what month I use! What is the error here? Or is there a better way of going about this?
Change if($num = 1) to if($num == 1) In fact you'd be better with the following; switch($num){ case '1': $month = 'January'; break; case '2': $month = 'February'; break; case '3': $month = 'March'; break; case '4': $month = 'April'; break; case '5': $month = 'May'; break; case '6': $month = 'June'; break; case '7': $month = 'July'; break; case '8': $month = 'August'; break; case '9': $month = 'September'; break; case '10': $month = 'October'; break; case '11': $month = 'November'; break; case '12': $month = 'December'; break; default: echo "error"; break; } echo $month; PHP:
here a little "smarter" version of your function. In this version you can use setlocale() to switch the language for the returned value: setlocale(LC_TIME, "en_US"); # for english setlocale(LC_TIME, "de_DE"); # for german setlocale(LC_TIME, "fi_FI"); # for finnish .... function displayMonth($num) { $month = FALSE; $num=intval($num); if ($num>0 && $num<13) { $month = strftime('%B', mktime(10,0,0,$num)); } return $month; } Code (markup):
Or, even quicker.. echo ($num>0&&$num<=12)?date("F", mktime(0, 0, 0, $num+1, 0, 0)):"error"; Code (markup):
Don't be ridiculous! In any case; ceil($num) PHP: would fix this problem for if you were really pernickety
I do this: $months = array(1 => 'January', 'February', 'March','April','May','June','July','August','September','October','November','December'); $x=12; echo $months[$x]; PHP: