Home > Backend Development > PHP Problem > How to query data for a specified time period in php?

How to query data for a specified time period in php?

coldplay.xixi
Release: 2023-03-02 16:40:01
Original
4703 people have browsed it

How to query data for a specified time period in php: use the strtotime timestamp conversion function, the code is [$timea = strtotime($_POST['timea']);$sql = mysql_query($sq2);] .

How to query data for a specified time period in php?

How to query data for a specified time period in php:

The following is a 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);
Copy after login

How to query data for a specified time period in php?

Extended information

Complete in php

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

General form:

date('Y-m-d H:i:s', 1156219870);
Copy after login

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

General form:

strtotime('2010-03-24 08:15:42');
Copy after login

in

is completed 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. Function to convert UNIX timestamp to date: FROM_UNIXTIME()

General form:

select FROM_UNIXTIME(1156219870);
Copy after login

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

General form:

Select UNIX_TIMESTAMP('2006-11-04 12:23:00′);
Copy after login

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”。
Copy after login

Related learning recommendations : PHP programming from entry to proficiency

The above is the detailed content of How to query data for a specified time period in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template