用于生成当前日期选择的MM / DD / YYY和HH / MM下拉菜单的php功能 [英] php function for generating MM/DD/YYY and HH/MM dropdowns with current date selected

查看:188
本文介绍了用于生成当前日期选择的MM / DD / YYY和HH / MM下拉菜单的php功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道有没有办法做到这一点?我确定以前做过。



需要生成html下拉列表,并填写月份,日期,年份,然后填写小时和分钟字段。我只需要MM / DD / YYYY显示当前(这些值与当前匹配,标记该选项)。我甚至不知道从哪里开始。我假设循环使用日期功能的东西?我在这里输了。



编辑

所有月份,第1-31天,当年,加上6年

解决方案

两个选择示例:

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0严格// EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1 -strict.dtd>
< html xmlns =http://www.w3.org/1999/xhtmlxml:lang =en>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< meta http-equiv =Content-Languagecontent =en/>
< title>测试< / title>
< / head>
< body>
< form>
< fieldset>
< legend>< / legend>
< label for =date> date:< / label>
< select id =datename =datetitle =Peek a date>
<?php
$ beg = new DateTime();
$ end = new DateTime();
$ end-> modify(+ 6年);

while($ beg-> format(U)< = $ end->格式(U)){
$ d = $ beg->格式(d / m / Y);
echo< option value ='。 $ d。 。 (date(d / m / Y)== $ d?selected ='selected':)。 > 。 $ d。 < / option> \\\
;
$ beg-> modify(+ 1天);
}
?>
< / select>
< label for =time> time:< / label>
< select id =timename =timetitle =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> \\\
;
}
}
?>
< / select>
< / body>
< / html>

或使用jQuery UI datepicker(更友好的IMHO):

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0严格// EN
http://www.w3.org/ TR / xhtml1 / DTD / xhtml1-strict.dtd>
< html xmlns =http://www.w3.org/1999/xhtmlxml:lang =fr>
< head>
< title>测试< / title>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< meta http-equiv =Content-Languagecontent =en/>
< script type =text / javascriptsrc =includes / js / jquery-1.3.2.min.js>< / script>
< script type =text / javascriptsrc =includes / js / jquery-ui-1.7.2.custom.min.js>< / script>
< link rel =stylesheettype =text / csshref =includes / js / themes / ui-lightness / jquery-ui-1.7.2.custom.cssmedia =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,
强制性: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 =textname =dateid =date/>
& nbsp;& nbsp;
< label for =time> time:< / label>
< select id =timename =timetitle =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> \\\
;
}
}
?>
< / select>
< / body>
< / html>


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.

解决方案

two selects example:

<!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 friendly IMHO):

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

这篇关于用于生成当前日期选择的MM / DD / YYY和HH / MM下拉菜单的php功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆