根据变量更改输出日期

时间:2016-02-19 06:25:34

标签: javascript php html

我想要的是输出日期应根据输入日期和选择的剩余天数(d_stay)而变化,即outputdate = inputdate(i_d)+ value(d_stay),它应该动态更改,而不刷新页面。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
      <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
      <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      <script>
         $(function () {
             var dataSrc = ["australia", "austria", "antartica", "argentina", "algeria"];

             $("#myText").autocomplete({minLength:3,source:dataSrc });
         });
      </script>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Make MY TRIP</title>
   </head>
   <body>
      <?php
         If(isset($_REQUEST['submit']) != '') {
             If($_REQUEST['s_state'] == '' || $_REQUEST['s_date'] == '' || $_REQUEST['myText'] == '') {
                 Echo "please fill the empty field.";
             }

             Else {
                 $s = $_POST["myText"];
                 $i_d = $_POST["s_date"];

                 $date = DateTime::createFromFormat("Y-m-d", $i_d);
                 echo "Inputdate";
                 echo "&nbsp";
                 echo $date - > format("d F");
                 echo "&nbsp";
                 echo "&nbsp";
                 // echo "out date";


                 echo "

                 < html >
                     < head >
                     < body >
                     < form method = 'Post' >
                     < select name = 'd_stay' >
                     < option value = '1' > 1 < /option> < option value = '2' > 2 < /option> < option value = '3' > 3 < /option> < option value = '4' > 4 < /option> < option value = '5' > 5 < /option> < option value = '6' > 6 < /option> < option value = '7' > 7 < /option> < /select> < /body> < /head> < /html>
                 ";

             }
         }
         ?>
      <p>
         Starting City
      <form method="post" action="23.php">
         <select name="s_state">
            <option value="Shimla">Shimla</option>
            <option value="Delhi">Delhi</option>
            <option value="Chandigarh">Chandigarh</option>
            <option value="Pune">Pune</option>
         </select>
         Starting date
         <input type="date" name="s_date" />
         <p>
            <input id="myText" placeholder="Add city you wish to travel" name="myText"  role="textbox"/>
         </p>
         <input type="submit" name="submit" value="submit" />
      </form>
      </p>
   </body>
</html>

1 个答案:

答案 0 :(得分:0)

使用strtotime功能,它解决了所需的问题

 <html>
    <head>
    <body>
    <?php

    if($_REQUEST['submit'])

     {  $nights=$_POST['nights'];

       $dateTs = strtotime($_POST['sd']);
       echo "Starting date is " . date('M-d-y',$dateTs);

      echo "</br>";

      $add="+".$nights. " ". "day";
      $newdate=strtotime($add,strtotime($_POST['sd']));
      echo "Departure date is " . date('M-d-y',$newdate);
     }
        ?>
    <form method="POST">
    <input type="text" name="sd" placeholder="monts/date/years">
    Number of nights<select name="nights">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    </select>
    <input type="submit" name="submit" value="submit"> 
    </form>
    </body>
    </head>
    </html>