MySQL query Specify the date time the principle of SQL statement implementation and code _php skills

Source: Internet
Author: User
The database tutorial design must note that the Time field is int (11) so that the database is stored in a digital date-time stamp, we can use the Mktime function to find the current date of the timestamp to add and subtract on OK, see the example below
One month
Copy Code code as follows:

$lastMonth = mktime (date (' H '), date (' I '), date (' s '), date (' m ') -1,date (' d '), date (' Y '));
$where. = "Dtime > $lastMonth";

Three months
Copy Code code 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
/*
principle is:
If the month is the current month minus the time you want to count, like I'm going to query the database for all the records from today's three months forward, our statements are as follows: Mktime (date (' H '), date (' I '), date (' s '), date (' m ') -3,date (' d ') , date (' Y '));
Seven days: mktime (Date (' H '), date (' I '), date (' s '), date (' m '), date (' d ') -7,date (' Y '));
Within an hour: mktime (date (' H ') -1,date (' I '), date (' s '), date (' m '), date (' d '), date (' Y '));
First day of last month: Mktime (0,0,0,date (' m ') -1,01,date (' Y '));
Last Last day: Mktime (0,0,0,date (' m '), 0,date (' y '));
The first day of the month: this simple, that is 01;
Last day of the month: This takes the date function, and the date function has an argument T, which is used to find the last day; for example: Date (' t ')
The other approach is the same.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.