Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php Mysql Search Between Two Dates

Tags:

php

mysql

I've mysql table like this:

id      start_date         username
1       2013-04-04         18
2       2013-03-31         19
3       2013-04-04         19
4       2013-04-02         19 
5       2013-04-03         18

I'm trying to get username where start_date is between 2013-03-31 to 2013-05-01 with following query:

// $from = 2013-03-31 and $to = 2013-03-01 (example)

$search = mysql_query("SELECT username FROM oc_calendar WHERE start_date >'$from' AND 
start_date < '$to'"); 
$re_search = mysql_fetch_array($search);
echo $search_p_id = $re_search['username']; 

But It's just print username = 18, It's should be print 18 and 19 number username. why it's doesn't show? Any idea?

like image 435
Babu Ahmed Avatar asked Mar 15 '13 17:03

Babu Ahmed


People also ask

How can I get data between two dates in MySQL using PHP?

Use the DATEDIFF() function to retrieve the number of days between two dates in a MySQL database.

How do I query between two dates using MySQL?

To count the difference between dates in MySQL, use the DATEDIFF(enddate, startdate) function. The difference between startdate and enddate is expressed in days.

How do I select a date range in MySQL?

If you need to select rows from a MySQL database' table in a date range, you need to use a command like this: SELECT * FROM table WHERE date_column >= '2014-01-01' AND date_column <= '2015-01-01';


1 Answers

Query:

$search = mysql_query("SELECT username FROM oc_calendar WHERE 
start_date between '$from' AND '$to'");

And you need a while-loop to display more that one username and a correct SQL-query (see above):

while($re_search = mysql_fetch_array($search)) {
  $re_search['username'] . '<br>';
}
like image 126
Treps Avatar answered Nov 14 '22 05:11

Treps