Creating Form Elements Based on the Current Time and/or Date : Form Select « Form « PHP






Creating Form Elements Based on the Current Time and/or Date

 
<html>
<body>
    <?php
      if ($_POST['submitted'] == "yes"){
        echo $_POST['month'] . "/" . $_POST['day'] . "/" . $_POST['year'] . " - " . $_POST['hour'] . ":" . $_POST['minute'] . ":" . $_POST['second'];
        ?><br /><a href="index.php">Try Again</a><?php
      }
      if ($_POST['submitted'] != "yes"){
        ?>
        <form action="index.php" method="post">
          <p>Example:</p>
          <input type="hidden" name="submitted" value="yes" />
          Select a Date and Time: <br />
          <select name="month">
            <?php
              for ($i = 1; $i <= 12; $i++){
                ?><option value="<?php echo $i; ?>"<?php if ($i == date ("n")){?> selected="selected"<?php } ?>><?php echo $i; ?></option><?php
              }
            ?>
          </select> / 
          <select name="day">
            <?php
              for ($i = 1; $i <= 31; $i++){
                ?><option value="<?php echo $i; ?>"<?php if ($i == date ("j")){?> selected="selected"<?php } ?>><?php echo $i; ?></option><?php
              }
            ?>
          </select> / 
          <select name="year">
            <?php
              for ($i = 1950; $i <= date ("Y"); $i++){
                ?><option value="<?php echo $i; ?>"<?php if ($i == date ("Y")){?> selected="selected"<?php } ?>><?php echo $i; ?></option><?php
              }
            ?>
          </select> - 
          <select name="hour">
            <?php
              for ($i = 1; $i <= 24; $i++){
                ?><option value="<?php echo $i; ?>"<?php if ($i == date ("G")){?> selected="selected"<?php } ?>><?php echo $i; ?></option><?php
              }
            ?>
          </select> : 
          <select name="minute">
            <?php
              for ($i = 1; $i <= 60; $i++){
                //Deal with leading zeros.
                if ($i < 10){
                  $comparem = "0" . $i;
                } else {
                  $comparem = $i;
                }
                ?><option value="<?php echo $i; ?>"<?php if ($comparem == date ("i")){?> selected="selected"<?php } ?>><?php echo $i; ?></option><?php
              }
            ?>
          </select> : 
          <select name="second">
            <?php
              for ($i = 1; $i <= 60; $i++){
                if ($i < 10){
                  $compares = "0" . $i;
                } else {
                  $compares = $i;
                }
                ?><option value="<?php echo $i; ?>"<?php if ($compares == date ("s")){?> selected="selected"<?php } ?>><?php echo $i; ?></option><?php
              }
            ?>
          </select>
          <br /><input type="submit" value="Submit" style="margin-top: 10px;" />
        </form>
        <?php
      }
    ?>
  </div>
</body>
</html>
  
  








Related examples in the same category

1.Form select input
2.An HTML Form Including a SELECT Element
3.A
4.A day choice
5.An HTML Form with a select Element
6.Validating a drop-down menu with in_array()
7.Displaying a
8.Generating a dynamic pull-down menu
9.Setting a default value in a
10.Setting defaults in a multi-valued
11.Creating Form Elements with Multiple Options
12.One choice for each day from 1 to 31
13.One choice for each year from last year to five years from now