0

if i have data in mysql like this

----------------------------------
id - title - date
1  - samy  - 2017-08-03 10:38:58 AM
2  - sara  - 2017-08-03 05:38:58 AM
3  - susan -2017-08-04 04:38:58 AM
-----------------------------------

using php how can i call what data between 05:00 am at 03-08-2017 and 05:00 am at 04-08-2017 i used

$sql = "select id,title,date from orders 
where Day(date)='03' AND Month(date)='08' AND Year(date)='2017'";

but it's only display all data in 03-08-2017 i also need to get data in 04-08-2017 from 12:00 am to 05:00 am just all data added in those five hours and if i used

$sql = "select id,title,date from orders 
where Day(date)='04' AND Month(date)='08' AND Year(date)='2017'";

i need to hide all data added between 12:00 am To 05:am in that day and get data between 12:00 am and 05:am in the next day how can i do that ?

4
  • what is your datatype in table for column date ? Commented Aug 5, 2017 at 4:48
  • yes it's datetime Commented Aug 5, 2017 at 4:56
  • Then how can you store dates with am and pm in mysql datetime? Commented Aug 5, 2017 at 4:59
  • i didn't i just wrote them above to clear my point Commented Aug 5, 2017 at 5:01

2 Answers 2

2
$sql = "SELECT id,title,date FROM orders WHERE date BETWEEN '2017-08-03 05:00:00' AND '2017-08-04 05:00:00'"

That should get you in the right place. Now if you want to automate that with php, you could set the string using variables and update the day using a function when it is time to do it the next day.

Sign up to request clarification or add additional context in comments.

2 Comments

i'm using my sql above in search filter and between not working with months or years
Something is wrong somewhere else then. Either maybe old old version of MySQL and syntax has changed, or outdated php, but my snippet works, just tried it. sqlfiddle.com/#!9/8d3f4/1/0
0

You need to simply use between to get data between two dates. Change your query to

$sql = "SELECT id,title,date FROM orders
        WHERE date BETWEEN '2017-08-03 05:00:00' AND '2017-08-04 05:00:00'";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.