Toolmaster Posted March 23, 2007 Share Posted March 23, 2007 Hi guys new to the forum, glad to be here among all you PHP experts! Anyway I have a problem in php, I have three drop down list boxes for users to select a day/month they want. <?php /* Program name: dateSelect.php * Description: Program displays a selection list that * customers can use to select a date. */ echo "<html> <head><title>Select a date</title></head> <body>"; /* create an array of months*/ $monthName = array(1=> "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $today = time(); //stores today’s date $f_today = date("M-d-Y",$today); //formats today’s date echo "<div align=’center’>\n"; /* display today’s date */ echo "<p> <h3>Today is $f_today</h3><hr>\n"; /* create form containing date selection list */ echo "<form action=’processform.php’ method=’POST’>\n"; /* build selection list for the month */ $todayMO = date("m",$today); //get the month from $today echo "<select name=’dateMO’>\n"; for ($n=1;$n<=12;$n++) { echo "<option value=$n\n"; if ($todayMO == $n) {echo " selected"; } echo "> $monthName[$n]\n"; } echo "</select>"; /* build selection list for the day */ $todayDay= date("d",$today); //get the day from $today echo "<select name=’dateDay’>\n"; for ($n=1;$n<=31;$n++) { echo " <option value=$n"; if ($todayDay == $n ) { echo " selected"; } echo "> $n\n"; } echo "</select>\n"; /* build selection list for the year */ $startYr = date("Y", $today); //get the year from $today echo "<select name=’dateYr’>\n"; for ($n=$startYr;$n<=$startYr+3;$n++) { echo " <option value=$n"; if ($startYr == $n ) { echo " selected"; } echo "> $n\n"; } echo "</select>\n"; echo "</form>\n"; ?> (code obtained from PHP and MYSQL for Dummies) I have created an array to insert all the months of the year, and I have stored today's date on there so that each day it changes every 24hours. I would like to get help with a formula that would enable the user to only select the current date and any remaining days after in one particular month. So for example, today is the 23rd March, so I will be able to select 23rd or up to 31st March (31 days in March), but no date before 23rd. If a user selects another month, so instead of March they choose April, then the days that will appear will be dependant on how many days there are in April. (30 days in April). Anyone of you smart intellectuals got any ideas? Link to comment https://forums.phpfreaks.com/topic/43926-date-problem/ Share on other sites More sharing options...
monk.e.boy Posted March 23, 2007 Share Posted March 23, 2007 Changes: $num_days = array(31,29,31,30,31,30,31,31,30,31,30,31); for ($n=$todayDay; $n<=$num_days[$todayMO]; $n++ ) I'm not sure if I got all the month lengths right. This will strart $n at today, and finish on the last day in the month (so Feb works etc.) monk.e.boy Link to comment https://forums.phpfreaks.com/topic/43926-date-problem/#findComment-213347 Share on other sites More sharing options...
Toolmaster Posted March 23, 2007 Author Share Posted March 23, 2007 Is there a way to do it without using such an array? I mean it is all well and good as it looks to be the easy way of doing it, but is there a way that it can be done so that I won't need to keep changing the day numbers every year? Thanks for your help look forward to reading your response Tim Link to comment https://forums.phpfreaks.com/topic/43926-date-problem/#findComment-213380 Share on other sites More sharing options...
kenrbnsn Posted March 23, 2007 Share Posted March 23, 2007 You can get the number of days in a month by using the date() function: <?php $days_per_month = array(); $this_year = date('Y'); for ($i=1;$i<13;$i++) $days_per_month[$i] = date('t',strtotime($this_year . '-' . $i . '-1')); ?> Ken Link to comment https://forums.phpfreaks.com/topic/43926-date-problem/#findComment-213448 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.