我试图将3年添加到从数据库返回的日期中

时间:2018-09-04 22:45:47

标签: php html css mysql sql

我试图将3年添加到从myphpadmin数据库返回的每个日期中。该代码提取了该类第一次上课的日期(date_aerial),我想返回一个值,例如date_aerial + 3 years。因此,如果给定的date_aerial为1-24-1994,我希望返回值为1-24-1997。对于从phpmyadmin数据库中提取的每个ID,都必须对此进行回显。因此,astern_refresh所在的位置应该是将date_aerial添加3年并返回该值的值。这是代码

<?php
$db_host = 'localhost'; 
$db_user = 'nick'; 
$db_pass = 'ramon';
$db_name = 'safetytraining'; 
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="safetytraining.css" rel="stylesheet" type="text/css" />
<title>Safety Training</title>
</head>
<body>
<div class="topnav">
 <a href="http://152.116.203.115/safetytraining.php">Home</a>
  <a  href="http://152.116.203.115/selectcourse.php">Select Course</a>
  <a href="http://152.116.203.115/calendar.php">Calendar</a>
  <a class="active" href="http://152.116.203.115/editcourse.php">Edit Course</a>
</div>
<br>
<h1>Facilities Tradesmen</h1>
<input type="button" onclick="location.href='http://152.116.203.115/search_aerial2.php';" value="Back to Search" style="float: right;"/><br>
<iframe id="txtArea1" style="display:none"></iframe>
<button  id="btnExport" onclick="fnExcelReport();" style="width:100px;height:50px;"> EXPORT </button><br>
</body>
<table width="100%" align="center" id="myTable" >
     <thead>
         <tr>
		    <th  onclick="sortTable(0)"width="2%" >ID</th>
			<th onclick="sortTable(1)" width="2%" >First Name</th>
			<th onclick="sortTable(2)" width="2%" >Last Name</th>
			<th onclick="sortTable(3)" width="2%" >Supervisor Group</th>
			<th onclick="sortTable(4)" width="2%" >Trade</th>
			<th onclick="sortTable(5)" width="2%" >T-ID</th>
			<th onclick="sortTable(6)" width="2%" >C-ID</th>
			<th onclick="sortTable(7)" width="2%" >Department</th>
			<th onclick="sortTable(8)" width="2%" >Aerial Devices Training <br>#:NTCHST01</th>
			<th onclick="sortTable(9)" width="2%" >Refresh Date</th>
			<th onclick="sortTable(10)" width="2%" >Edit Data</th>


			
         </tr>
    </thead>
     <tbody>
</html>
 <?php
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
    die ('Fail to connect to MySQL: ' . mysqli_connect_error());   
}

$value1 = $_POST['searchTP'];
$value2 = $_POST['searchTR'];
$value3 = $_POST['searchTD'];
$value4 = $_POST['searchCD'];
$value5 = $_POST['searchDP'];
$value6 = $_POST['searchAD'];
$value10 = $_POST['searchSG'];
$value11=$_POST['date_aerial'];

$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 years'));
echo date_format($date, 'm-d-Y');


echo '<br>';
echo "<font color=white size='4pt'> You searched for Tradesperson:</font><font color=red size='4pt'> $value1</font>";
echo '<br>';
echo  "<font color=white size='4pt'> You searched for Trade:</font><font color=red size='4pt'>  $value2</font>"; 
echo '<br>';
echo  "<font color=white size='4pt'> You searched for T-ID:</font><font color=red size='4pt'>  $value3</font>"; 
echo '<br>';
echo  "<font color=white size='4pt'> You searched for C-ID:</font><font color=red size='4pt'>  $value4</font>"; 
echo '<br>';
echo  "<font color=white size='4pt'> You searched for Department:</font><font color=red size='4pt'>  $value5</font>"; 
echo '<br>';


// Add conditional statement here that if value1 or value is blank then search the other.

$sql = "SELECT ID, first_name, last_name, supervisor_group, trade, t_id, c_id, department_number, date_aerial, aerial_refresh
        FROM peopleinfo WHERE
		(last_name LIKE '%$value1%' )
		and 
		(trade LIKE '%$value2%')
		and
		(t_id LIKE '%$value3%')
		and
		(c_id LIKE '%$value4%')
		and
		(department_number LIKE '%$value5%')
		and
		(date_aerial LIKE '%$value6%')
		and
		(date_lock LIKE '%$value7%')
		and
		(date_confine LIKE '%$value8%')
		and
		(date_fall LIKE '%$value9%')
		and
		(supervisor_group LIKE '%$value10%')";
		
	
		

 
 
$query = mysqli_query($conn, $sql);
 
if (!$query) {
    die ('SQL Error: ' . mysqli_error($conn));
}
 
while ($row = mysqli_fetch_array($query))
{
    echo '<tr>
			<td>'.$row['ID'].'</td>
			 <td>'.$row['first_name'].'</td>
			 <td>'.$row['last_name'].'</td>
			 <td>'.$row['supervisor_group'].'</td>
            <td>'.$row['trade'].'</td>
            <td>'.$row['t_id'].'</td>
			<td>'.$row['c_id'].'</td>
            <td>'.$row['department_number'].'</td>
			<td>'.$row['date_aerial'].' </td>
			<td>'.$row['aerial_refresh'].' </td>
			<td><a href="edit.php?ID=' . $row['ID'] . '">Edit</a></td>
        </tr>';
		
}  

mysqli_free_result($query);
mysqli_close($conn);
?>
 </tbody>
</table>

任何帮助都将非常感谢。

1 个答案:

答案 0 :(得分:1)

您可以在此输入“ DATE_ADD(date_aerial,间隔3年)”

$sql = "SELECT ID, first_name, last_name, supervisor_group, 
                trade, t_id, c_id, department_number, 
                DATE_ADD (date_aerial, INTERVAL +3 Year), 
                aerial_refresh 
        FROM peopleinfo 
        WHERE (last_name LIKE '%$value1%' ) 
        and (trade LIKE '%$value2%') 
        and (t_id LIKE '%$value3%') 
        and (c_id LIKE '%$value4%') 
        and (department_number LIKE '%$value5%') 
        and (date_aerial LIKE '%$value6%') 
        and (date_lock LIKE '%$value7%') 
        and (date_confine LIKE '%$value8%') 
        and (date_fall LIKE '%$value9%') 
        and (supervisor_group LIKE '%$value10%')";

使用此功能,您无需对代码执行任何操作。

让我知道它是否对您有用。