PHP Date Processing Small example MySQL by date, time query example

Source: Internet
Author: User
    1. PHP Date Processing
    2. Date_default_timezone_set (' PRC '); Default time zone
    3. echo "Today:", Date ("Y-m-d", Time ()), "
      ";
    4. echo "Today:", Date ("Y-m-d", Strtotime ("June 2008")), "
      ";
    5. echo "Yesterday:", Date ("Y-m-d", Strtotime ("-1 Day")), "
      ";
    6. echo "Tomorrow:", Date ("Y-m-d", Strtotime ("+1 Day"), "
      ";
    7. echo "One week later:", Date ("Y-m-d", Strtotime ("+1 Week")), "
      "; bbs.it-home.org
    8. echo "2 days a week four hours two seconds later:", Date ("Y-m-d g:h:s", Strtotime ("+1 Week 2 Day 4 hours 2 Seconds")), "
      ";
    9. echo "Next week Four:", Date ("Y-m-d", Strtotime ("next Thursday")), "
      ";
    10. echo "Previous Monday:". Date ("Y-m-d", Strtotime ("Last Monday")). "
      ";
    11. echo "One months ago:". Date ("Y-m-d", Strtotime ("Last month"). "
      ";
    12. echo "One months later:". Date ("Y-m-d", Strtotime ("+1 Month")). "
      ";
    13. echo "Ten years later:". Date ("Y-m-d", Strtotime ("+10 Year")). "
      ";
Copy Code

2,mysql date operation, query by date and time

    1. #mysql查询今天, yesterday, 7 days, nearly 30 days, this month, last month data
    2. #今天
    3. SELECT * FROM table name where To_days (Time field name) = To_days (now ());
    4. #昨天
    5. SELECT * FROM table name WHERE to_days (now ()) –to_days (Time field name) <= 1
    6. #7天
    7. SELECT * FROM table name where Date_sub (Curdate (), INTERVAL 7 day) <= Date (Time field name)
    8. #近30天
    9. SELECT * FROM table name where Date_sub (Curdate (), INTERVAL Day) <= Date (Time field name)
    10. #本月
    11. SELECT * FROM table name WHERE date_format (Time field name, '%y%m ') = Date_format (Curdate (), '%y%m ')
    12. #上一月
    13. SELECT * FROM table name WHERE Period_diff (Date_format (now (), '%y%m '), Date_format (Time field name, '%y%m ')) =1
Copy Code
  • 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.