How MySQL queries SQL statements within a specified date and time

Source: Internet
Author: User

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 and add or subtract it. The following figure shows the instance.
// One month CopyCode The Code is as follows: $ lastmonth = mktime (date ('H'), date ('I'), date ('s'), date ('M')-1, date ('D'), date ('y '));
$ Where. = "dtime> $ lastmonth ";

// Three monthsCopy codeThe 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
/*
principle :
If the month is the current month minus the time you want to calculate, for example, if I want to query all the records in the database from 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');
in the past 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, 0, 0, date ('M ')-, Date ('y');
last day of last month: mktime (, 0, date ('M'), 0, date ('y '));
day 1 of this month: This is simple, that is, 01.
last day of this month: the date function is used. The date function has a parameter t, it is used to calculate the last day. For example, date ('T')
The same applies to other methods.

Related Article

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.