MySQL Query on timestamp using PHP

In this example I had to create a query only to display orders for the last 90 days.

standard timestamp field in MySQLThe orders table is using a standard MySQL timestamp field to store the date of the order. I made timestamp indexed since we will be doing this query against this field.

I figured the easiest way was to only query those 90 days, that way I didn’t have to worry about looking at each row and comparing a date during output. All I had to worry about was creating the right query.

The first line simply is the number of days to go back in the query, in this case the number of days is 90.
The next 2 lines are used to create and format our new date 90 days in the past.
In the query we simply select all records that are newer than our 90 day old date.

$daysback = 90;
$accessdate = strtotime(“-$daysback days”,time());
$mysqldate = date( ‘Y-m-d H:i:s’, $accessdate );

$myquery = “select id,ordernumber,timestamp from tblorders where (timestamp > ‘$mysqldate’);”;