Home >Backend Development >PHP Tutorial >mysql query sql statement implementation principle and code within specified date_PHP tutorial

mysql query sql statement implementation principle and code within specified date_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:14:49954browse

When designing the database tutorial, you must pay attention to the time field as int (11). In this way, what is saved in the database is a numeric date and time stamp. We can use the mktime function to find the timestamp of the current date and add or subtract. Let’s see an example below.
//One month

Copy code The code is as follows:

$lastMonth = mktime(date('h') ,date('i'),date('s'),date('m')-1,date('d'),date('y'));
$where .= ” dtime > $lastMonth”;

//Three months
Copy code The code is 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
/*
The principle is:
If it is a month, it is the current month minus the time you want to count. For example, if I want to query all the records in the database for the past three months from today, our statement is as follows: mktime(date('h' ),date('i'),date('s'),date('m')-3,date('d'),date('y'));
Within seven days:mktime(date ('h'),date('i'),date('s'),date('m'),date('d')-7,date('y'));
One hour Within:mktime(date('h')-1,date('i'),date('s'),date('m'),date('d'),date('y'));
The first day of last month: mktime(0,0,0,date('m')-1,01,date('Y'));
The last day of last month: mktime(0, 0,0,date('m'),0,date('y'));
The first day of this month: This is simple, which is 01;
The last day of this month: This is needed The date function has a parameter t, which is used to find the last day; such as: date('t')
Other methods are the same.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326224.htmlTechArticleWhen designing the database tutorial, you must pay attention to the time field as int (11). In this way, what is saved in the database is a numeric type. Date timestamp, we can use the mktime function to find the timestamp of the current date...
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