【问题标题】:php function for generating MM/DD/YYY and HH/MM dropdowns with current date selected用于生成选择当前日期的 MM/DD/YYYY 和 HH/MM 下拉列表的 php 函数
【发布时间】:2009-11-01 09:28:04
【问题描述】:

有人知道这样做的方法吗?我确定它以前做过。

需要生成html的downdown列表,填写月、日、年,然后是小时、分钟字段。我只需要 MM/DD/YYYY 来显示当前值(如果这些值与当前值匹配,则标记该选项已选中)。我什至不知道从哪里开始。我假设循环使用日期函数?我在这里迷路了。

编辑
所有月份、第 1-31 天、当前年份以及未来 6 年。

【问题讨论】:

  • 如果不是太麻烦,day字段应该有适合所选月份的天数?
  • 我不明白你在这里做什么。你是在为这个问题做出贡献,还是试图讽刺?

标签: php datetime


【解决方案1】:

两个选择示例:

<!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>

或使用 jQuery UI 日期选择器(恕我直言更友好):

<!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】:

    到目前为止,我发现以下内容有效。使其适应您对天/分钟/小时等的特定需求。现在可以持续数月。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 2019-02-17
      • 2016-07-24
      • 2023-03-04
      • 2012-02-14
      • 1970-01-01
      • 2023-03-26
      相关资源
      最近更新 更多