S.M Talha S.M Talha - 3 months ago 8
MySQL Question

Selecting records older than 15 days ago

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

updatetime
?

Code

$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());
}
$i=1;


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>";

echo
"<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

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)
Comments