How Do I Convert String Values From A Form Input Into Integers?

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.
[php]

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 ''; foreach ($months as $key => $value) { print "\n$value"; } print ''; ?>

						<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>

[/php]

I tried executing your code. What I have understood is that you would want $_POST[‘month’] give you 5 for May, 8 for August and 12 for December…?
It is already giving integer values in $_POST[‘month’]

This is how your select form looks like in HTML(copied from Firebug). As you can see, value attribute has integer values and these will be passed to $_POST[‘month’] on form submit.

January February March April May June July August September October November December

Or you want to pass string value to $_POST[‘month’] and then get it converted to integer and put that integer value in database? I think your code’s perfectly fine.

I hope we are on the same level of understanding!!

@sarthakpatel et al, thanks for making me realize my foly. I should have known that the post values were already integers. Thank you all.

Sponsor our Newsletter | Privacy Policy | Terms of Service