如何从开始时间开始默认结束时间3小时?

时间:2011-05-05 05:06:33

标签: javascript jquery html forms dom

首先检查facebook事件创建页面链接是http://www.facebook.com/?ref=home#!/events/create.php我在我的应用程序中使用该页面的开始和结束日期时间。我的问题是如果我设置开始时间12pm 默认结束时间应该是结束时间= 3pm 之后的3小时(如果用户没有设置结束时间)

开始时间代码

<select name="menu" class="style24">
  <option value="00:00" <?php if($todaytime=="01"){echo "selected";}?>>12:00am</option>
  <option value="00:30" <?php if($todaytime=="01"){echo "selected";}?>>12:30am</option>
  <option value="01:00" <?php if($todaytime=="02"){echo "selected";}?>>1:00am</option>
  <option value="01:30" <?php if($todaytime=="02"){echo "selected";}?>>1:30am</option>
  <option value="02:00" <?php if($todaytime=="03"){echo "selected";}?>>2:00am</option>
  <option value="02:30" <?php if($todaytime=="03"){echo "selected";}?>>2:30am</option>
  <option value="03:00" <?php if($todaytime=="04"){echo "selected";}?>>3:00am</option>
  <option value="03:30" <?php if($todaytime=="04"){echo "selected";}?>>3:30am</option>
</select>

结束时间的代码

<select name="menu1" class="style24">
  <option value="00:00" <?php if($todaytime=="01"){echo "selected";}?>>12:00am</option>
  <option value="00:30" <?php if($todaytime=="01"){echo "selected";}?>>12:30am</option>
  <option value="01:00" <?php if($todaytime=="02"){echo "selected";}?>>1:00am</option>
  <option value="01:30" <?php if($todaytime=="02"){echo "selected";}?>>1:30am</option>
  <option value="02:00" <?php if($todaytime=="03"){echo "selected";}?>>2:00am</option>
  <option value="02:30" <?php if($todaytime=="03"){echo "selected";}?>>2:30am</option>
  <option value="03:00" <?php if($todaytime=="04"){echo "selected";}?>>3:00am</option>
  <option value="03:30" <?php if($todaytime=="04"){echo "selected";}?>>3:30am</option>
  <option value="04:00" <?php if($todaytime=="05"){echo "selected";}?>>4:00 am</option>
</select>

$todaytime定义为date("H", time())

2 个答案:

答案 0 :(得分:0)

假设你的第一个选择器有id =“start”而另一个select有i'=“end”,代码应该是这样的(纯javascript和jquery):

  <script>   
    function hhmm2minutes(hhmm){
       var hhmmArray = hhmm.split(":");
       return parseInt(hhmmArray[0], 10)*60 + parseInt(hhmmArray[1], 10);
    }
    function minutes2hhmm(minutes){
       minutes = minutes % (24*60); //To avoid having hour > 23
       function addZero(x){
          return (x < 10 ? "0"+x : ""+x) ;
       }
       var mm = minutes % 60;
       var hh = (minutes - mm)/60;
       return addZero(hh) + ":" + addZero(mm);   
    }

    function setEnd3HoursFromStart(){
       var hhmmStart = $("#start").val();
       if(hhmmStart=="") return;
       var hhmmEnd = minutes2hhmm(hhmm2minutes(hhmmStart) + 180); //Add 180 minutes
       $("#end").val(hhmmEnd);
    }

    $(document).ready(function(){
      setEnd3HoursFromStart();
      $("#start").change(function(){
         setEnd3HoursFromStart();
      });
    });
  </script>

希望这有帮助。干杯

答案 1 :(得分:0)

制作你的

<?php   if($todaytime=="01"){echo "selected";} ?>

进入函数调用:

<?php selectstarttime(time) ?>  //in your starttime code
<?php selectendtime(time) ?>  // in your endtime code

function selectstarttime(time){
 if (something like time==currentTime){
  return "selected";
 }
 else
  return "";
 } 
}

function selectendtime(time){
 if (something like time==currentTime + 3hours){
  return "selected";
 }
 else
  return "";
 } 
}