How to resolve query problems when the time format from the front end does not match the time format in the MySQL table

Source: Internet
Author: User

The time format passed by the front end is "2016-07-11 11:13:10", and the format of the corresponding field ' Add_time ' in the datasheet is "2016-7-11", when SQL cannot be used directly with "where ' add_time ' = ' 2016-07-11 11:13:10 '. This can be done in the following ways:

1. Use PHP's Strtotime () and date () to format the data before it is used for SQL queries, to become the format that is ultimately needed, and then use the data for the query:

1 $t = ' 2015-03-02 12:04:12 '; 2 $t Date Strtotime ($t)); 3 Echo $t;

Input:

2016-07-11

2. You can use the MySQL keyword "like" + "%", where the section is as follows:

1 select * from Test where ' add_time ' like ' 2016-07-11% '

Note: Because '% ' is behind a string, the index can be used if an index is established on the ' Add_time ' field.

If there is any mistake, please correct me.

  

How to resolve query problems when the time format from the front end does not match the time format in the MySQL table

Related Article

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.