Home  >  Article  >  Backend Development  >  A small example of PHP date processing MySQL query by date and time

A small example of PHP date processing MySQL query by date and time

WBOY
WBOYOriginal
2016-07-25 08:56:00929browse
  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("18 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 "One week, two days, four hours and two seconds later:",date(" Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "
    ";
  9. echo "Next Thursday:",date("Y-m-d",strtotime("next Thursday")), "
    ";
  10. echo "Last Monday:".date("Y-m-d",strtotime("last Monday"))."
    ";
  11. echo "One month ago: ".date("Y-m-d",strtotime("last month"))."
    ";
  12. echo "One month 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 query today, yesterday, 7 days, last 30 days, this month, last month data
  2. #Today
  3. select * from table name where to_days (time field name) = to_days(now()) ;
  4. #Yesterday
  5. SELECT * FROM table name WHERE TO_DAYS( NOW( ) ) – TO_DAYS( time field name) <= 1
  6. #7 days
  7. SELECT * FROM table name where DATE_SUB(CURDATE(), INTERVAL 7 DAY) < ;= date(time field name)
  8. #Last 30 days
  9. SELECT * FROM table name where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(time field name)
  10. #This month
  11. SELECT * FROM table name WHERE DATE_FORMAT(time field name, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
  12. #Previous month
  13. SELECT * FROM table name WHERE PERIOD_DIFF( date_format( now( ) , '%Y %m' ) , date_format( time field name, '%Y%m' ) ) =1
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn