如何将单独的日期和时间组合作为输入以将它们组合在一起?

时间:2015-01-22 11:39:03

标签: php html

我有两个日期字段和两个时间字段作为html表单的输入,并希望将这些单独的日期和时间组合在一起,以便我可以使用mysql查询从数据库表中获取所有值。 如果来自日期:2014-5-6 todate:2014-5-7         从时间:14:8全程:15:5 ans应该从tablename中选择*,其中获取这两个日期和时间之间的数据作为结果。 以下是我的代码..plz告诉我哪里出错..plz帮助我..谢谢你的建议 project.html

<b>From Date:</b>
<input type="text" id="fromdate" name="fromdate" size="20" />
<b>To Date:</b>
<input type="text" id="todate" name="todate" size="20"/><br><br>
<b>From Time:</b>
<input type="text" id="fromtime" name="fromtime" size="30" />
<b>To Time:</b>
<input type="text" id="totime" name="totime" size="30"/><br><br>
<input type="submit" id="submit" name="submit" value="GENERATE"/><br><br>

project.php

  $from_date_and_time=null;
	  $frdttm=null;
	  $to_date_and_time=null;
	  $todttm=null;
	if(isset($_POST["submit"]))
	{
    $fromdate=$_POST['fromdate'];
	$todate=$_POST['todate'];
	$fromtime=$_POST['fromtime'];
	$totime=$_POST['totime'];
	
	  $from_date_and_time=$fromdate .'  '. $fromtime;
      $frdttm=strtotime("$from_date_and_time");
      $to_date_and_time=$todate .'  '. $totime;
      $todttm=strtotime("$to_date_and_time");
	$hours = 24;
     // Check if date is more recent than the specified number of hours
      if ((time() - $frdttm) < (60 * 60 * $hours))
       {
         echo 'Yes!';
       }
	    if ((time() - $todttm) < (60 * 60 * $hours))
       {
         echo 'Yes!';
       }
	
	if(!$fromdate || !$todate ||!$fromtime||!$totime)
	echo " please provide all the fields";		
    $pk=@mysql_connect("localhost", "root" ,"")or die ("cannot open");
    if(!$pk)
	echo "fail";
	
    $db=@mysql_select_db("test",$pk);

     if(!$db)
	 echo "<br>database not selected";
	
		else
		{
		 $query = "SELECT * FROM floattable WHERE (DateAndTime between '$frdttm' AND '$todttm') ORDER BY DateAndTime ASC;";
		 echo "Connected";
		 echo "<br>";
		 $result=mysql_query($query);
		 echo "result";
		 echo"<br>";
		 if($result)
		 echo $result;
		 $numrows=mysql_num_rows($result);
		 echo $numrows;
$i=0;
echo"<table border='1' cellspacing='3' align=left>
<tr>
<th>DateAndTime</th>
<th>MillItm</th>
<th>TagIndex</th>
<th>Value</th>
<th>Status</th>
<th>Marker</th>
</tr>";
while($i<$numrows)
	{
	echo"<tr>";
	echo "<td>".mysql_result($result,$i,'DateAndTime')."</td>";
	echo "<td>".mysql_result($result,$i,'Millitm')."</td>";
	echo "<td>".mysql_result($result,$i,'TagIndex')."</td>";
	echo "<td>".mysql_result($result,$i,'Value')."</td>";
	echo "<td>".mysql_result($result,$i,'Status')."</td>";
	echo "<td>".mysql_result($result,$i,'Marker')."</td>";
	echo"</tr>";
	$i++;
	}
}
}

1 个答案:

答案 0 :(得分:0)

首先,您必须将名称命名为要合并的数组,如:

<b>From Date:</b>
<input type="text" id="fromdatetime[]" name="fromdate" size="20" />
<b>To Date:</b>
<input type="text" id="todatetime[]" name="todate" size="20"/><br><br>
<b>From Time:</b>
<input type="text" id="fromdatetime[]" name="fromtime" size="30" />
<b>To Time:</b>
<input type="text" id="todatetime[]" name="totime" size="30"/><br><br>
<input type="submit" id="submit" name="submit" value="GENERATE"/><br><br>

之后,您可以在操作页面中获取值作为数组,然后您可以使用 implode 函数在String中合并数组,如:

$fromArr = $_POST['fromdatetime'];
implode('-',$fromArr);