Home >Backend Development >PHP Problem >How to implement the query time period function in php
PHP query time period function implementation: First, if the database display time is the current time, there is no need to use the strtotime timestamp conversion function; then directly declare the $timea and $timeb variables to obtain the current time string; finally declare $ sq2 selects the time period in which add_time is located.
#The following is the timestamp query. If the database time displays 2011-04-05, then there is no need to use strtotime
timestamp conversion function:
$timea = strtotime($_POST['timea']); $timeb = strtotime($_POST['timeb']); $sq2="select * from `ecs_order_info` where add_time between '$timea' and '$timeb' and `quanxian`='$dangqian' order by `order_id` DESC limit 50"; $sql = mysql_query($sq2);
Complete in php
1. Use function to convert UNIX timestamp to date: date()
General form: date('Y-m-d H:i:s', 1156219870);
2. Use function to convert date to UNIX timestamp: strtotime()
General form: strtotime('2010-03-24 08:15:42');
Complete this in MySQL
The method is converted in the MySQL query statement. The advantage is that it does not take up the parsing time of the PHP parser and is fast. The disadvantage is that it can only be used in database queries and has limitations.
1, UNIX
Function to convert timestamp to date: FROM_UNIXTIME()
General form: select FROM_UNIXTIME(1156219870);
2. Use the function to convert date to UNIX timestamp: UNIX_TIMESTAMP()
General form: Select UNIX_TIMESTAMP('2006-11-04 12:23:00′);
Example: mysql query the number of records on the day:
$sql=”select * from message Where DATE_FORMAT(FROM_UNIXTIME(chattime),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') order by id desc”。
More learning materials:PHP implements mobile phone location query Video tutorial
The above is the detailed content of How to implement the query time period function in php. For more information, please follow other related articles on the PHP Chinese website!