Date selection from today.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Date selection from today.

Post 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.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

use strtotime() to normalize flawed/ridiculous date selections :wink:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Sorted!.....

Many thanks!
Post Reply