S.M Talha S.M Talha - 1 year ago 66
MySQL Question

Selecting records older than 15 days ago

How can i display only records older then 15 days ago by the column



$conn = mysql_connect($servername,$database_username,$database_password);
$sql = 'SELECT * FROM user_shift_test2';
$result = mysql_query( $sql, $conn );
if(! $result )
die('Could not get data: ' . mysql_error());

While loop which is fetching all the rows

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
echo '<tr>';
$res = $row['userid'];
echo "<td><input type='checkbox' name='chk[]' value='$res'></td>";

"<td>{$row['userid']} <br> </td>".
"<td>{$row['id']} <br> </td>".
"<td>{$row['name']} <br> </td>".
"<td>{$row['shifttime']} <br> </td>".
"<td>{$row['dayoff']} <br> </td>".
"<td>{$row['updatetime']} <br> </td></tr>";

Answer Source

You can do it in pure SQL , filter the records on the DB side. Try using DATE_SUB() :

SELECT * FROM user_shift_test2 t
WHERE t.updatetime < DATE_SUB(NOW(), INTERVAL 15 DAY)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download