MySQL query specifies the date time of the SQL statement implementation principle and Code _php tutorial

Source: Internet
Author: User
In the database tutorial design, you must note that the Time field is int (11) So, the database is saved in a numeric date time stamp, we can use the Mktime function to find the current date of time stamp to add and subtract is OK, see the example below
One month
Copy CodeThe 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 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 is:
If the month is the current month minus the time you want to count, as I'm querying the database for all records from today onwards for three months, our statement is as follows: Mktime (' 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 '));
Within one 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 '));
First day of the month: This is simple, that is, 01;
Last day of the month: This uses the DATE function, the DATE function has a parameter T, it is used for the last day, for example: Date (' t ')
The other approach is the same.

http://www.bkjia.com/PHPjc/326224.html www.bkjia.com true http://www.bkjia.com/PHPjc/326224.html techarticle you must pay attention to the time field in the Database tutorial design int (11) So, the database is saved in a numeric date time stamp, we can use the Mktime function to find the current date timestamp ...

  • 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.