The database tutorial design must note that the Time field is int (11) so that the database is stored in a digital date-time stamp, we can use the Mktime function to find the current date of the timestamp to add and subtract on OK, see the example below
One month
Copy Code code as follows:
$lastMonth = mktime (date (' H '), date (' I '), date (' s '), date (' m ') -1,date (' d '), date (' Y '));
$where. = "Dtime > $lastMonth";
Three months
Copy Code code as follows:
$lastThreeMonth = mktime (date (' H '), date (' I '), date (' s '), date (' m ') -3,date (' d '), date (' Y '));
$where. = "Dtime > $lastThreeMonth";
$sql = "SELECT * from TestTable". $where
/*
principle is:
If the month is the current month minus the time you want to count, like I'm going to query the database for all the records from today's three months forward, our statements are as follows: Mktime (date (' H '), date (' I '), date (' s '), date (' m ') -3,date (' d ') , date (' Y '));
Seven days: mktime (Date (' H '), date (' I '), date (' s '), date (' m '), date (' d ') -7,date (' Y '));
Within an hour: mktime (date (' H ') -1,date (' I '), date (' s '), date (' m '), date (' d '), date (' Y '));
First day of last month: Mktime (0,0,0,date (' m ') -1,01,date (' Y '));
Last Last day: Mktime (0,0,0,date (' m '), 0,date (' y '));
The first day of the month: this simple, that is 01;
Last day of the month: This takes the date function, and the date function has an argument T, which is used to find the last day; for example: Date (' t ')
The other approach is the same.