Jump to content

Days based on months


jor133d

Recommended Posts

I am trying to modify some code so that the days dropdown has the correct amount of days for each month( Jan - 31 days, Feb 28, etc). I managed to get it so the correct days appear however it lists all the days.

 

http://www.glitzphotography.com/date/dateform.php

 

Can someone help me get this to work properly?

 

Thanks!

 

<?php 


function make_calendar_pulldowns($m = NULL, $d = NULL, $y = NULL)	// This function makes three pull-down lists
								// for the months, days, and years.
    {

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

// Make the months pull-down list
echo '<select name="month">';
foreach ($months as $key => $value)
	{
	    echo "<option value=\"$key\"";
	    if ($key == $m) { echo ' selected="selected"'; }
	    echo ">$value</option>\n";
	}
echo '</select>';

// Make the days pull-down list
echo '<select name="day">';

foreach ($months as $key => $value) {

$num = cal_days_in_month(CAL_GREGORIAN, $key, 2007); 

for ($day = 1; $day <= $num; $day++)
	{
	    echo "<option value=\"$day\"";
	    if ($day == $d) { echo ' selected="selected"'; }
	    echo ">$day</option>\n";
	}
}

echo '</select>';

// Make the years pull-down list
echo '<select name="year">';
for ($year = 2005; $year <= 2015; $year++)
	{
	    echo "<option value=\"$year\"";
	    if ($year == $y) { echo ' selected="selected"'; }
	    echo ">$year</option>\n";
	}
echo '</select>';

    } 	// End of the function definition.


// Create the form tags.
echo '<h1 id="mainhead">Select a Date:</h1>
<p><br /></p><form action="dateform.php" method="post">';

// Call the function.
$dates = getdate();
make_calendar_pulldowns($dates['mon'], $dates['mday'], $dates['year']);

// End of form.
echo '</form><p><br /></p>';

// Print the current date and time
echo '<p>Today is ' . date('l') . '. The current time is ' . date('g:i a') . '.</p>';

$num = cal_days_in_month(CAL_GREGORIAN, 11, 2007); // 31
echo "There was $num days";



?>

Link to comment
https://forums.phpfreaks.com/topic/70934-days-based-on-months/
Share on other sites

If want the days drop down menu to adapt so the correct number of days are displayed for the selected the month then you'll need to use javascript to repopulate the days list, or submit the form when the month drop down box has changed.

Link to comment
https://forums.phpfreaks.com/topic/70934-days-based-on-months/#findComment-356586
Share on other sites

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.