Is there a way for mysql-Php to compare whether the two time periods are equal?

Source: Internet
Author: User
My data inventory may contain timestamp of hour, minute, and second. now there is only one time for comparison between year, month, and day. if the comparison is equal, I will query it. Which of the following experts will guide you how to judge the query? thank you very much. My data inventory may contain timestamp of hour, minute, and second. now, we only need to compare the time every year, month, and day. if they are the same, we will query it. Which of the following experts will guide you how to judge the query? thank you very much.

Reply content:

My data inventory may contain timestamp of hour, minute, and second. now, we only need to compare the time every year, month, and day. if they are the same, we will query it. Which of the following experts will guide you how to judge the query? thank you very much.

There are many methods, but I think you need fuzzy queries. I 'd like to explain a better understanding.
1. use the sorttime function to convert the time that is only "year-month-day" to the timestamp, so that you can get time1 at of the current day;
2. still use the sorttime function to get the + 1 time, or simply add the total number of seconds in one day, the variable name is time2, so there are two time nodes.
3. use between to query all the data of the day.
========== If I understand the error, correct it and change it again ==========

Two methods are introduced. The first method is recommended.

  • If the year, month, and day are the same, it means to query the data within a certain day and take the initiative to control the time range, for example, to query the data of '2017-10-28 '.

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

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

Below is the save fielddatetimeFormatsql

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, use the substr function of MySQL to intercept the data to the day for comparison.

The author's problem is:
Query data of a certain day (such as January 1, 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 stored as a timestamp but in date ('ymdhis ') format, you can do this:

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.