Home >Backend Development >PHP Problem >How to query sql based on time in php

How to query sql based on time in php

藏色散人
藏色散人Original
2021-11-02 10:26:222816browse

php method to query sql based on time: 1. Convert database time into timestamp through strtotime; 2. Select and query SQL data through "select * from `ecs_order_info` where add_time between..." statement Can.

How to query sql based on time in php

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How does php query sql based on time?

The following is the timestamp query. If the database time displays 2011-04-05, then there is no need to use the 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);

How to query sql based on time in php

Extended information

Completed in php

1. Use function to convert UNIX timestamp to date: date()

General form: date('Y-m-d H:i:s', 1156219870);

2 , use the function to convert date to UNIX timestamp: strtotime()

General form: strtotime('2010-03-24 08:15:42');

Complete in MySQL

This 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. Use function to convert UNIX timestamp to date: FROM_UNIXTIME()

General form: select FROM_UNIXTIME(1156219870);

2. Use function to convert date to UNIX timestamp Function: UNIX_TIMESTAMP()

General form: Select UNIX_TIMESTAMP('2006-11-04 12:23:00′);

Example: mysql query the number of records for 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".

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to query sql based on time in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn