drayarms Posted May 23, 2011 Share Posted May 23, 2011 Well the topic may not sound very explicit. So I'll do my best to explain it here. I have a pull down menu as part of a form, which contains the months of the year. I'm trying to store the value of this form entry using the $_POST['month'] variable into a database but first, I need to convert the month values into their corresponding integer values( that is 1 for January, 2 for February etc), in order to easily do date calculations later down the road. Any ideas about how to do this? It may be helpful to include the code for the pull down menu. <p><tab> Date of Birth: <?php //Make the month pull down menu. //Array to store months. $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); print '<select name= "month">'; foreach ($months as $key => $value) { print "\n<option value=\"$key\">$value</option>"; } print '</select>'; ?> </tab> <tab> <?php //Make the day pull down menu print '<select name= "day">'; for ($day = 1; $day <= 31; $day++) { print "\n<option value=\"$day\">$day</option>"; } print '</select>'; ?> </tab> <tab><?php //Make the year pull down menu print '<select name= "year">'; $start_year = date('Y'); for ($y = ($start_year - 18); $y >= 1900; $y--) { print "\n<option value=\"$y\">$y</option>"; } print '</select>'; ?> </tab> </p> Quote Link to comment https://forums.phpfreaks.com/topic/237253-how-do-i-convert-string-values-from-an-input-form-into-integers/ Share on other sites More sharing options...
The Little Guy Posted May 23, 2011 Share Posted May 23, 2011 That doesn't make sense.... You are converting them already, or am I missing something? Quote Link to comment https://forums.phpfreaks.com/topic/237253-how-do-i-convert-string-values-from-an-input-form-into-integers/#findComment-1219200 Share on other sites More sharing options...
PFMaBiSmAd Posted May 23, 2011 Share Posted May 23, 2011 The month select menu is sending the $key as the value, so you are already getting 1,2,3,... Have you actually tried the code you posted? Quote Link to comment https://forums.phpfreaks.com/topic/237253-how-do-i-convert-string-values-from-an-input-form-into-integers/#findComment-1219201 Share on other sites More sharing options...
Pikachu2000 Posted May 23, 2011 Share Posted May 23, 2011 The month should already be a numeric value between 1-12 in the $_POST array. All you have to do is validate the value as valid, and cast it as an integer. Quote Link to comment https://forums.phpfreaks.com/topic/237253-how-do-i-convert-string-values-from-an-input-form-into-integers/#findComment-1219202 Share on other sites More sharing options...
AbraCadaver Posted May 23, 2011 Share Posted May 23, 2011 What they said. Also, you can maybe simplify your code using range(). I just like foreach(): foreach(range(1,31) as $day) { print "\n<option value=\"$day\">$day</option>"; } // foreach(range(date('Y')-18, 1900) as $y) { print "\n<option value=\"$y\">$y</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/237253-how-do-i-convert-string-values-from-an-input-form-into-integers/#findComment-1219219 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.