Page 1 of 1

Date selection from today.

Posted: Thu Nov 17, 2005 4:30 am
by mhouldridge
Hi,

I currently have a selection (drop down box) which shows a list of dates, here is the code;

Code: Select all

<?
$leap = array(1, 0, 0, 0, 1, 0); //<- Not accurate! Look up the leapyears. 
$years = array(2005, 2006, 2007); //<- List of years 

echo "<SELECT NAME=datetwo><OPTION>Choose One</OPTION>n"; 

foreach ($years as $year) {  //Cycle thru years 

  for ($i = 1; $i<=12; $i++) { //Cycle thru months 

    $day = 1; //Start at day 1 

     while ($day <= date('t', $month = mktime(0, 0, 0,  $i, 1, $year)))  { //Cycle thru days 
	 echo "<OPTION VALUE=\"{$day}-" 
                  .date('m', $month) 
                  ."-{$year}\">{$day}-" 
                  .date('m', $month)."-{$year}</OPTION>\n";
       $day++; 
    } 
  } 
} 

echo "</SELECT>\n"; 
?>

My question - I need to set the start point from today's date. I do not want to show any dates from the past as this is going to be my expiry date. If a past date is selected it messes up my output on another page.

Please help.

Posted: Thu Nov 17, 2005 4:35 am
by n00b Saibot
use strtotime() to normalize flawed/ridiculous date selections :wink:

Posted: Thu Nov 17, 2005 4:55 am
by twigletmac
Using PHP's date and time functions can make dates a lot easier to deal with (handles things like leap years without need for additional coding):

Code: Select all

<?php

$start_date = time(); // today's date
$end_date = strtotime('+2 years'); // Two years from today's date

echo '<select name="datetwo"><option>Choose One</option>'."\n";

for ($i = 1, $date = $start_date; $date <= $end_date; ++$i) {
	$option_date = date('d-m-Y', $date);

	echo '<option value="'.$option_date.'">'.$option_date.'</option>'."\n";

	$date = strtotime('+'.$i.' day'); // add one day to the date
}

echo '</select>';

?>
Mac

Posted: Thu Nov 17, 2005 8:00 am
by mhouldridge
Sorted!.....

Many thanks!