How to achieve date comparison, and the implementation of the display within 5 days, showing 10 days of records _php tutorial

Source: Internet
Author: User
A few days ago sent a post, is about showing 5 days of this date comparison,

http://www.oso.com.cn/forum/read.php?theme_id=7459

The main argument is to focus on the select something from table
WHERE To_days (now ())-To_days (Date_col) <= 5; On, I tried half a day, the result or error, today read the function of MySQL, finally came out of the results, dare not exclusive, posted out for everyone to study, (although the technical content is not high, do not throw my persimmon on the line, hehe)

The To_days (DATE) function of MySQL is as follows:
Returns the total number of days from date to 00, and I tested it.
Mysql>select To_days (now (0));
+--------------------------+
| To_days (Now ()) |
+--------------------------+
| 730839 |
+--------------------------+

Coming out is the total number of days of the current time from 00 A.D., and then I tried to test it with the above statement;

Mysql>select To_days (now ())-To_days (Date_col) <= 5;
Results appear:
ERROR 1054:unknown column date_col in field first

Blocked, I'm trying to get 5 generations straight to Date_col.

Mysql>select To_days (now ())-to_days (5);
Results appear:
+---------------------------+
|to_days (now ())-to_days (5) |
+---------------------------+
| NULL |
+---------------------------+

Ah? No way? That's not good, is it?
I'll try the order.
Mysql>select ....

Suddenly think, hey, To_days (now ()) come out is an integer, I directly with the integer operation on the line, why again to_days (date)? Test now

Mysql>select To_days (now ())-5;
+--------------------------+
| To_days (now ())-5 |
+--------------------------+
| 730834 |
+--------------------------+


OK, Long live, finally got the result I want, hehe is in the PHP code with select query

My database has always been the habit of DateAndTime with now () direct assignment, when the display is not formatted, directly take out to be able to use,

Here's a partial structure of one of my libraries
CREATE TABLE infomess (
infoid Int (one) not NULL auto_increment,
Topic varchar (255) is not NULL,
......
Email varchar (50),
DateAndTime datetime DEFAULT 0000-00-00 00:00:00 not NULL,
PRIMARY KEY (infoid)
);


Here DateAndTime is the standard date format, and then I want to query the records within 5 days, here is the SQL query statement
$sql = "SELECT * from Infomess where To_days (dateandtime) >= (To_days (now ())-5) Order BY infoid desc limit $offset, $psi Ze ";

It's going to be a where to_days (DateAndTime) >= (To_days (now ())-5) that's enough. The next is another, where 5 can be set to a variable

where To_days (DateAndTime) >= (To_days (now ())-$limitdays)

The $limitdays can then be passed in a Get mode (most of which are passed by a Get method)

Keep up behind your PHP? Limitdays=5 on the line. Show the same thing in 10 days, $limitdasy change to 10.

The above is the use of MySQL function to get such results, the above results have been tested, because the time is in a hurry, if the code has any problems, please put forward, thank you


There are friends that use the Unix stamp to get such a result, ask who wrote this code, paste point out, for everyone to reference the comparison, you can also test to determine the PHP function or MySQL function implementation of high efficiency


http://www.bkjia.com/PHPjc/532466.html www.bkjia.com true http://www.bkjia.com/PHPjc/532466.html techarticle a few days ago sent a post, is about showing 5 days of this date comparison, http://www.oso.com.cn/forum/read.php?theme_id=7459 main argument is focused on the select something from Tab ...

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