mysql-php There is no way to compare the two-time date is equal

Source: Internet
Author: User
My database storage may contain time and seconds of the timestamp, now there is a time only years and days need to be compared if the equality of the query out, which God guidance how to judge the query, thank you very much.

Reply content:

My database storage may contain time and seconds of the timestamp, now there is a time only years and days need to be compared if the equality of the query out, which God guidance how to judge the query, thank you very much.

method or there are quite a variety of, but I see your needs, should be the need for fuzzy query, I will tell a better understanding of it
1. Convert only the "year-month-day" time using the Sorttime function into a timestamp, so you get 0 points of the day time1;
2. Still use the Sorttime function to get +1 of the time, or directly plus 1 days total number of seconds also line, variable name time2, so there are 2 time nodes
3. Use between query so that you can get all the data of the day
========= If I understand wrong, correct me, I'll change =========.

Introduce two ways to recommend the first type of

    • As long as the date is equal, it means to query the data within a certain day range, and actively control the time range, such as query ' 2016-10-28 ' data

$st = strtotime("2016-10-28");$et = strtotime("2016-10-28 23:59:59");select * from table where date_field between $st and $et
    • Directly with mysql built-in functionsFROM_UNIXTIME(date_field, '%Y%m%d')

select * from table where FROM_UNIXTIME(date_field,'%Y%m%d') ="2016-10-28"

The following is datetime the format of the saved fieldssql

select * from table where date_field between "2016-10-28" and "2016-10-28 23:59:59"
select * from table where DATE_FORMAT(date_field , '%Y%m%d') ="2016-10-28"

If the date format in the database is the same, then by the MySQL substr function interception to the day and then do the comparison

The landlord's problem is actually:
Query data for a day (such as October 28, 2016) based on the Time field:


  
   = $min and time < $max";// 输出 select * from t where time >= 1477584000 and time < 1477670400

If the time field is not a timestamp, but data in date (' Ymdhis ') format, you can:

select * from t where time >= 20161028000000 and time < 20161029000000
  • 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.