ahmed_omar
ahmed_omar

Reputation: 49

php get data from mysql by date

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 ?

Upvotes: 0

Views: 1100

Answers (2)

B. Desai
B. Desai

Reputation: 16436

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'";

Upvotes: 0

Xype
Xype

Reputation: 68

$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.

Upvotes: 2

Related Questions