The Mktime function in PHP is queried by time, in fact, in addition to this solution, there are also functions specifically for date and time in MySQL, including date (), Date_add (), Date_sub (), Date_format (), and so on.
The code is as follows |
Copy Code |
Orders in the last three months, the SQL statement is as follows: $sql = "SELECT * from Shopping_order WHERE dttime>=date_sub (now (), INTERVAL 3 MONTH)"; If you are querying for orders within the last 15 days, the SQL statement is as follows: $sql = "SELECT * from Shopping_order WHERE dttime>=date_sub (now (), INTERVAL)"; |
mktime function
The code is as follows |
Copy Code |
function to query the order in the last three months, the SQL statement is as follows: $bgdte =date ("y-m-d h:i:s", Mktime (Date (' H '), date (' I '), date (' s '), date (' m ') -3,date (' d '), date (' Y ')); $sql = "SELECT * from Shopping_order WHERE dttime>= '". $bgdte. "'"; If you are querying for orders within the last 15 days, the SQL statement is as follows: $bgdte =date ("y-m-d h:i:s", Mktime (Date (' H '), date (' I '), date (' s '), date (' m '), date (' d ') -15,date (' Y ')); $sql = "SELECT * from Shopping_order WHERE dttime>= '". $bgdte. "'"; |
SQL by month group and monthly grouped query statement
The code is as follows |
Copy Code |
GROUP BY Month: Select Month (from_unixtime) from table_name GROUP by month (From_unixtime (time)) Grouped by Month: Select Date_format (From_unixtime (Time), "%y-%m") from Tcm_fund_list Group by Date_format (From_unixtime (Time), "%y-%m") Where time is an integer such as: 1159718400 represents 2006-10-02 |
query Today, yesterday, 7 days, nearly 30 days, month, last month data
code is as follows |
copy code |
Today select * from table name where To_days (Time field name) = To_days (now ()); yesterday SELECT * FROM table name WHERE to_days (now ()) –to_days (Time field name) <= 1 7 days &NB sp; SELECT * FROM table name where Date_sub (Curdate (), INTERVAL 7 day) <= DATE (Time field name) nearly 30 days SELECT * from table name where Date_sub (Curdate (), INTERVAL Day) <= DATE (Time field name) This month SELECT * FROM table name WHERE date_format (Time field name, '%y%m ') = Date_format (Curdate (), '%y%m ') &N bsp; Previous Month SELECT * FROM table name WHERE Period_diff (now (), '%y%m '), Date_form At (Time field name, '%y%m ') =1 |