ProblemHelpPlease Posted July 17, 2009 Share Posted July 17, 2009 The code below returns the month as December for values of 08 and 09, all other values work. i have fixed it by removing the 0 from 09 but I can't see why 08 and 09 fail when 05 etc work fine, any ideas? <?php$asdday = 05;$asdyear = 2005;$asdmonth = 08;switch ($asdmonth){case 01: $asdmonth = "January"; break;case 02: $asdmonth = "February"; break;case 03: $asdmonth = "March"; break;case 04: $asdmonth = "April"; break;case 05: $asdmonth = "May"; break;case 06: $asdmonth = "June"; break;case 07: $asdmonth = "July"; break;case 08: $asdmonth = "August"; break;case 09: $asdmonth = "September"; break;case 10: $asdmonth = "October"; break;case 11: $asdmonth = "November"; break;default: $asdmonth = "December";}echo $asdday . " " . $asdmonth . " " . $asdyear;?> Link to comment Share on other sites More sharing options...
jeffman Posted July 17, 2009 Share Posted July 17, 2009 Prefixing a number with a 0 turns it into an octal number (base 8). Since there is no 08 or 09 in octal (you go directly from 07 to 10, which "means" 8 in decimal), I suppose those values are returning as undefined or zero, so they get picked up by your default condition.A much simpler way to handle the whole project would be to create an array of months and then use $asdmonth as an index to fetch the correct value. Thus:$mo_array = array ('January', 'February', 'March');$asdmonth = 1;$asdmonth = $mo_array[$asdmonth];// returns 'February' Link to comment Share on other sites More sharing options...
Guest FirefoxRocks Posted July 17, 2009 Share Posted July 17, 2009 I varied your code slightly to loop through all months: <!DOCTYPE html><title>Date</title><?php$asdday = 05;$asdyear = 2005;for($i=1; $i<=12; $i=$i+1) { $asdmonth = $i; echo "<p>i=$i ... "; switch ($asdmonth) { case 01: $asdmonth = "January"; break; case 02: $asdmonth = "February"; break; case 03: $asdmonth = "March"; break; case 04: $asdmonth = "April"; break; case 05: $asdmonth = "May"; break; case 06: $asdmonth = "June"; break; case 07: $asdmonth = "July"; break; case 08: $asdmonth = "August"; break; case 09: $asdmonth = "September"; break; case 10: $asdmonth = "October"; break; case 11: $asdmonth = "November"; break; default: $asdmonth = "December"; } echo "$asdday $asdmonth $asdyear</p>";}?> Link to comment Share on other sites More sharing options...
ProblemHelpPlease Posted July 17, 2009 Author Share Posted July 17, 2009 Ahhh that makes sense, didn't think about an octal number. The code is interated with other variables so has to be in that manner, I only included the variables that seemed to be causing the problem, thanks for the advice tho. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.