richrock Posted November 14, 2008 Share Posted November 14, 2008 Hi all, I've written a basic conversion script to change a numerical value entered into a form to a calendar month, ie - 05 = May. Here's the code: if ($_POST['month'] == 01) { $month = "January"; } elseif ($_POST['month'] == 02) { $month = "February"; } elseif ($_POST['month'] == 03) { $month = "March"; } elseif ($_POST['month'] == 04) { $month = "April"; } elseif ($_POST['month'] == 05) { $month = "May"; } elseif ($_POST['month'] == 06) { $month = "June"; } elseif ($_POST['month'] == 07) { $month = "July"; } elseif ($_POST['month'] == 08) { $month = "August"; } elseif ($_POST['month'] == 09) { $month = "September"; } elseif ($_POST['month'] == 10) { $month = "October"; } elseif ($_POST['month'] == 11) { $month = "November"; } elseif ($_POST['month'] == 12) { $month = "December"; } echo $month."<br />"; $cat_title = $_POST['saletitle']; $cat_day = $_POST['day']; $cat_mon = $_POST['month']; $cat_year = $_POST['year']; $cat_salenum = $_POST['salenum']; But for some really strange reason, it stops working for August, September and I can't for the life of me figure out why... Any ideas? All the code was cut'n'pasted so it should work consistently right? Quote Link to comment https://forums.phpfreaks.com/topic/132703-solved-converting-numbers-to-months-not-working/ Share on other sites More sharing options...
.josh Posted November 14, 2008 Share Posted November 14, 2008 typo in your form values? Quote Link to comment https://forums.phpfreaks.com/topic/132703-solved-converting-numbers-to-months-not-working/#findComment-690123 Share on other sites More sharing options...
rhodesa Posted November 14, 2008 Share Posted November 14, 2008 Yeah, I don't see any errors either. On a side note, it's much easier to read if you setup an array: $months = array( 1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December", ); $month = $months[$_POST['month']]; Quote Link to comment https://forums.phpfreaks.com/topic/132703-solved-converting-numbers-to-months-not-working/#findComment-690127 Share on other sites More sharing options...
richrock Posted November 14, 2008 Author Share Posted November 14, 2008 The form field is like this: <td><input type="text" maxlength="2" name="day" size="1"/> - <input type="text" maxlength="2" name="month" size="1" /> - <input type="text" maxlength="4" name="year" size="3" /> <?php echo SALE_add_date_details; ?></td> And when the form is submitted, it sends to a function - addSale (), which has the original code I posted earlier. I get what you mean with an array - I am forcing the end user to input 2 numbers, so August would always be 08 - would this be reflected in the array by doing: 08 => "August", ?? Quote Link to comment https://forums.phpfreaks.com/topic/132703-solved-converting-numbers-to-months-not-working/#findComment-690131 Share on other sites More sharing options...
Mark Baker Posted November 14, 2008 Share Posted November 14, 2008 I'll be willing to bet that it works for October, November and December. With that as a clue, look at your code again. Answers on the back of a $10 note Quote Link to comment https://forums.phpfreaks.com/topic/132703-solved-converting-numbers-to-months-not-working/#findComment-690137 Share on other sites More sharing options...
rhodesa Posted November 14, 2008 Share Posted November 14, 2008 don't use leading zeros...you'll run into problems with that. cus if you don't put quotes around it, PHP will drop the leading zero and make it a number. made one small mod to force the users input to a number: $months = array( 1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December", ); $month = $months[(int)$_POST['month']]; Quote Link to comment https://forums.phpfreaks.com/topic/132703-solved-converting-numbers-to-months-not-working/#findComment-690140 Share on other sites More sharing options...
thebadbad Posted November 14, 2008 Share Posted November 14, 2008 A number starting with a zero is treated as an octal, I heard. Shorter code: <?php $month = date('F', strtotime('2000-' . str_pad($_POST['month'], 2, '0', STR_PAD_LEFT) . '-15')); ?> Edit: Actually even shorter using mktime(): <?php $month = date('F', mktime(0, 0, 0, (int) $_POST['month'], 15)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/132703-solved-converting-numbers-to-months-not-working/#findComment-690159 Share on other sites More sharing options...
richrock Posted November 14, 2008 Author Share Posted November 14, 2008 Thanks guys, I solved it with the putting quotes round it - if ($_POST['month'] == "08") { and it did the trick... Quote Link to comment https://forums.phpfreaks.com/topic/132703-solved-converting-numbers-to-months-not-working/#findComment-690160 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.