0

由于某种原因,我无法让 php 将 HTML 表单输入识别为日期。

我有以下 HTML 表单:

<form action="Input.php" method="post">
Depature Date: 
<select name="DMonth">
<option>- Select Month -</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="DDay">
<option>- Select Day -</option>
    <option value="01">1</option>
    <option value="02">2</option>
    <option value="03">3</option>
    <option value="04">4</option>
    <option value="05">5</option>
    <option value="06">6</option>
    <option value="07">7</option>
    <option value="08">8</option>
    <option value="09">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
    <option value="21">21</option>
    <option value="22">22</option>
    <option value="23">23</option>
    <option value="24">24</option>
    <option value="25">25</option>
    <option value="26">26</option>
    <option value="27">27</option>
    <option value="28">28</option>
    <option value="29">29</option>
    <option value="30">30</option>
    <option value="31">31</option>
</select>
<select name="DYear">
<option>- Select Year -</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
</select><br>
Departure Time: <select name="DHour" input type="number">
<option>- Select Hour -</option>
<option value="00">12 a.m.</option>
<option value="01">1 a.m.</option>
<option value="02">2 a.m.</option>
<option value="03">3 a.m.</option>
<option value="04">4 a.m.</option>
<option value="05">5 a.m.</option>
<option value="06">6 a.m.</option>
<option value="07">7 a.m.</option>
<option value="08">8 a.m.</option>
<option value="09">9 a.m.</option>
<option value="10">10 a.m.</option>
<option value="11">11 a.m.</option>
<option value="12">12 p.m.</option>
<option value="13">1 p.m.</option>
<option value="14">2 p.m.</option>
<option value="15">3 p.m.</option>
<option value="16">4 p.m.</option>
<option value="17">5 p.m.</option>
<option value="18">6 p.m.</option>
<option value="19">7 p.m.</option>
<option value="20">8 p.m.</option>
<option value="21">9 p.m.</option>
<option value="22">10 p.m.</option>
<option value="23">11 p.m.</option>
</select>
<select name="DMin">
<option>- Select Minute -</option>
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
<br>
<input type="submit" value="Submit">
</form>

和以下PHP:

$DCity=(int) $_POST["DCity"];
$ACity=(int) $_POST["ACity"];
$DMonth=(int) $_POST["DMonth"];
$DDay=(int) $_POST["DDAY"];
$DYear=(int) $_POST["DYear"];
$DHour=(int) $_POST["DHour"];
$DMin=(int) $_POST["DMin"];
$DTime = mktime($DHour, $DMin, 0, $DMonth, $DDay, $DYear);
print date(DATE_RFC822, $DTime);

我应该从中获得的回报应该反映在表格中输入的任何内容,但是返回的只是当前日期和时间。我的代码某处有错误吗?我尝试了多种 php 时间方法,但都返回当前日期而不是 HTML 表单中输入的日期。

4

3 回答 3

2

mktime()默认每个参数为当前日期/时间。因此,您的$_POST值似乎没有按预期设置、为空或类型错误。

According to Converting to integer you shouldn't need to explicitly cast at all, as the function expects ints anyhow. But doing the explicit cast or conversion will allow you to check if that is actually working prior to relying on the function call to do it for you (and in this case, possibly fail to do so).

You're using (int) to cast... What about using intval() instead or in addition, and then test/print the input values before your conversion, and then again after (before mktime()).

I expect you'll find they're not as expected, and if so, that your explicit casts are also failing.

于 2013-03-31T03:02:46.860 回答
0

Perhaps there is a problem because some of the numbers start with a leading zero, which makes them octal. A number like 08 means nothing in octal.

于 2013-03-31T03:10:40.223 回答
0

Figured it out! It was a simple mistake with capitalizations. I have $_POST("DDAY") instead of $_POST("DDay") so it wasn't calling the "DDay" input from the HTML. Thanks for everyone's help!

于 2013-03-31T03:38:15.500 回答