php - Order mySQL results based of the current time and the chosen time by the user? -


In my page, I show some entries based on the time limit I selected, to be chosen to contact the user , And it is saved in mySQL

  09: 00-11: 00 11: 00-13: 00 13: 00-15: 00 15: 00-17: 00   

For now, I show results according to the time selected

  $ sql = "Select from table order based on the time selected";   

I want to sort the results based on the present time.

For example, if the time is 10:30, then the first rows selected are between 09: 00-11: 00, if it is 11:00, then the first 11:00 - 13:00 result Show.

I am thinking about a between , but I do not know how to move forward with it <.

Assume that selected time characters Type is, in a definite and authentic format, it is not difficult at all.

We currently need to format the format hh: mm (like 10:30) to match the archived format.

If we want to get the current time from the database in that format, then it is a simple expression:

  DATE_FORMAT (NOW (), '% h:% i ')   

The ugly part is to split the character string. Looking at a fixed format hh: mm-hh: mm , we can use simple SUBSTR functions:

  SUBSTR (ct, 1,5) - Start SUBSTR (ct, 7,5) - end   

You can definitely use a different comparator, but note that at the borders, when the current time is on the edge ( Like 13:00), we will get a "match" on both the 11: 00-13: 00 and 13: 00-15: 00 duration. DATE_FORMAT (NOW (), '% h:% i') by ORDER SUBSTR (CT, 1,5) and SUBSTR (CT, 7,5) DESC, CT, ... Between ...

Note that the first expression is evaluated as a boolean, and either 1 (true), 0 (wrong), or NULL (unknown) We add the keyword DESC so that the true conditions (back as the value of 1) are sorted before the false Will go.


Use this to deal with the time values ​​we do not have the usual pattern, but it will work for the time chosen in a determined and validate format. (It works with 24 hour clock, but can not work with 12 hour clock, or one digit hour, etc. This is to work, we have to guarantee that a string Comparison is equivalent to a time comparison.)

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -