Php to generate drop-down lists MM / DD / YYY and HH / MM with the current date selected

Does anyone know how to do this? I am sure that this has been done before.

You need to create html reduction lists and fill in the month, day, year, and then the hour and minute fields. I only need MM / DD / YYYY to display the current one (where these values ​​correspond to the current one, check this option). I don’t even know where to start. I assume something is happening with the date function? I lost here.

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

0
source share
2 answers

two options:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<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 IMHO friendly):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<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>
+2
source

Ive , . // .. .

<?

$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>
"; } 
?>
0

Source: https://habr.com/ru/post/1728976/


All Articles