Jump to content

[SOLVED] how do u keep date format when passing through a form


jeppers

Recommended Posts

My problem is that i have a pull down boxes with day/month/year in them i would like to be able to keep that format. at the moment when choosing the values (example shown below) 

 

3 august 2007 would pint out as 3 8 0.

 

i want to no how to keep the format they are originally chosen

 

wanted outcome

03 august 2007

how i retrieved the variables

 

Code:

$days = ($_POST['days']);
$months = ($_POST['months']);
$years = ($_POST['years'] as string);

//create a date variable 
$date = $days .''. $months .''. $years;

echo "$date";

 

 

can u help

thanks

I think you misunderstood him. I guess your problem is mostly your HTML (although you didn't show it). The drop-down list should look like this in your case:

 

<select name="days">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
...
</select>

<select name="months">
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
...
</select>

<select name="years">
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
...
</select>

 

And the PHP:

 

<?php
$days = $_POST['days'];
$months = $_POST['months'];
$years = $_POST['years'];

$date = $days.' '.$months.' '.$years;

echo $date;
?>

i used a forloop which i think may be the problem

 

//make the months array 
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

//make the days and years array 
$days = range (0, 31);
$years = range (2007, 2020);

//make the days pull down menu 
echo '<select name="days">';
foreach ($days as $key => $value) {
echo "<option value=\"$key\">$value</option>\n";
}
echo '</select>';

//make that months pull down menus
echo '<select name="months">';
foreach ($months as $key => $value) {
echo "<option value=\"$key\">$value</option>\n";
}
echo '</select>';



//make the year pull down menu 
echo '<select name="years">';
foreach ($years as $key => $value) {
echo "<option value=\"$key\">$value</option>\n";
}
echo '</select>';

 

any clues

Your code is fine, you just need to change $key to $value 3 places to get what you want. And the days range should be from 1-31, not from 0-31. If you want months to be in lowercase when sent, use strtolower().

 

<?php
//make the months array 
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

//make the days and years array 
$days = range (1, 31);
$years = range (2007, 2020);

//make the days pull down menu 
echo '<select name="days">';
foreach ($days as $key => $value) {
echo "<option value=\"$value\">$value</option>\n";
}
echo '</select>';

//make that months pull down menus
echo '<select name="months">';
foreach ($months as $key => $value) {
echo "<option value=\"$value\">$value</option>\n";
}
echo '</select>';



//make the year pull down menu 
echo '<select name="years">';
foreach ($years as $key => $value) {
echo "<option value=\"$value\">$value</option>\n";
}
echo '</select>';
?>

No problem :)

 

What I meant with changing the $key with $value:

<option value="This is sent to the server/whatever">This is shown in the drop-down</option>

 

You had from 1-12 ($key) sent to the server, and jan-dec ($value) shown in the drop-down.

 

About the lowercase-thing, just do like this in the months-"foreach":

 

	echo "<option value=\"strtolower($value)\">$value</option>\n";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.