1 Replies - 1318 Views - Last Post: 23 May 2011 - 12:31 PM Rate Topic: -----

#1 drayarms   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 199
  • Joined: 18-May 11

How do I convert string values from an input form into integers?

Posted 23 May 2011 - 12:25 PM

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>





Is This A Good Question/Topic? 0
  • +

Replies To: How do I convert string values from an input form into integers?

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

Re: How do I convert string values from an input form into integers?

Posted 23 May 2011 - 12:31 PM

something like
(int) date("m", strtotime("february"));
or
filter_input(INPUT_POST, 'month', FILTER_VALIDATE_INT, array("options" => array("min_range" => 1, "max_range" => 12)));
?

PS. <tab> is still not a valid HTML(5) element

This post has been edited by Dormilich: 23 May 2011 - 12:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1