tags:

views:

61

answers:

3

Does anyone know of a way to do this? Im sure its been done before.

Need to generate html downdown lists, and fill in the month, day, year, and then the hour, and minute fields. I only need the MM/DD/YYYY to display current (where those values match current, mark that option selected). I wouldnt even know where to start. I assume looping through something with the date function? Im lost here.

edit
all months, days 1-31, and the current year, plus 6 years into the future.

A: 

How many MM/DD/YYYY options do you want?

Murrieta Web Design
all months, days 1-31, and the current year, plus 6 years into the future.
Patrick
@Murrieta, this should have been a comment not an answer.
pavium
A: 

Ive found the following to work so far. Adapt it to your particular needs for day/minute/hours etc. Right now its for months.

<?

$current_time_m = date('n');

for ($i = 1; $i <= 12; $i++) {
   echo "<option value='$i'";
if ($i == $current_time_m) { echo " selected='selected'"; }
$month_text = date("F", mktime(0, 0, 0, $i+1, 0, 0, 0));
   echo ">$month_text</option>
"; } 
?>
Patrick
+2  A: 

two selects example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta http-equiv="Content-Language" content="en" />
  <title>Test</title>
</head>
<body>
  <form>
    <fieldset>
      <legend></legend>
      <label for="date">date : </label>
      <select id="date" name="date" title="Peek a date">
      <?php
        $beg = new DateTime();
        $end = new DateTime();
        $end->modify("+6 years");

        while($beg->format("U") <= $end->format("U")) {
          $d = $beg->format("d/m/Y");
          echo "<option value='" . $d . "'" . (date("d/m/Y") == $d ? " selected='selected'" : "") . ">" . $d . "</option>\n";
          $beg->modify("+1 day");
        }
      ?>
      </select>
      <label for="time">time : </label>
      <select id="time" name="time" title="Peek a time">
      <?php
        foreach(range(0, 24) as $h) {
          foreach(range(0, 59) as $m) {
            $t = sprintf("%02d:%02d", $h, $m);
            echo "<option value='" . $t . "'" . (date("H:i") == $t ? " selected='selected'" : "") . ">" . $t . "</option>\n";
          }
        }
      ?>
      </select>
</body>
</html>

or with jQuery UI datepicker (more friendly IMHO):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
  <title>Test</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta http-equiv="Content-Language" content="en" />
  <script type="text/javascript" src="includes/js/jquery-1.3.2.min.js"></script>
  <script type="text/javascript" src="includes/js/jquery-ui-1.7.2.custom.min.js"></script>
  <link rel="stylesheet" type="text/css" href="includes/js/themes/ui-lightness/jquery-ui-1.7.2.custom.css" media="screen, print" />
  <script type="text/javascript">
  $(document).ready(function() {
    var d = new Date();

    $("#date").datepicker({
      minDate: d,
      maxDate: new Date(d.getFullYear() + 6, d.getMonth(), d.getDay()),
      dateFormat: "dd/mm/yy",
      mandatory: true,
      changeFirstDay: false,
      changeYear: true,
      showStatus: true,
      showOn: "both",
      buttonImage: "images/calendar.gif",
      buttonImageOnly: true
    }).addClass("embed");
  });
  </script>
</head>
<body>
  <form>
    <fieldset>
      <legend></legend>
      <label for="date">date : </label>
      <input type="text" name="date" id="date" />
      &nbsp;&nbsp;
      <label for="time">time : </label>
      <select id="time" name="time" title="Peek a time">
      <?php
        foreach(range(0, 24) as $h) {
          foreach(range(0, 59) as $m) {
            $t = sprintf("%02d:%02d", $h, $m);
            echo "<option value='" . $t . "'" . (date("H:i") == $t ? " selected='selected'" : "") . ">" . $t . "</option>\n";
          }
        }
      ?>
      </select>
</body>
</html>
RC