How mysql queries SQL statements within a specified date and time and code _ PHP Tutorial

Source: Internet
Author: User
Mysql queries the implementation principle and code of SQL statements within a specified date. When designing a database tutorial, you must note that the time field is int (11). In this way, a digital date timestamp is stored in the database, we can use the mktime function to find the timestamp of the current date. in the database tutorial design, we must note that the time field is int (11). In this way, a digital date timestamp is saved in the database, we can use the mktime function to find the timestamp of the current date and add or subtract it. The following figure shows the instance.
// One month

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

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 the month is the current month minus the time you want to calculate, for example, to query all records in the database for the first 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 '));
In an hour: mktime (date ('h')-1, date ('I'), date ('s'), date ('M '), date ('D'), date ('Y '));
First day of last month: mktime (, 0, date ('M')-, date ('Y '));
Last day of last month: mktime (0, 0, 0, date ('M'), 0, date ('Y '));
The first day of this month: this is simple, that is, 01;
Last day of the month: this requires the date function. the date function has a parameter t, which is used to calculate the last day. for example, date ('t ')
Other methods are the same.

In this case, the database stores a digital date timestamp. we can use the mktime function to find the timestamp of the current date...

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.