Satch3000
Satch3000

Reputation: 49384

PHP SQL Query results issue with dates

I am having a problem with the code below.

For some reason it displays only dates from October, November and December.

The dates have the same format... see below:

DATE            RESULT

2013-02-01      Does Not Display
2013-03-01      Does Not Display
2013-04-01      Does Not Display
2013-05-01      Does Not Display
2013-06-01      Does Not Display
2013-07-01      Does Not Display
2013-08-01      Does Not Display
2013-09-01      Does Not Display
2013-10-01      Displays OK
2013-11-01      Displays OK
2013-12-01      Displays OK

Here is the query code:

$events = array();
$query = "SELECT title, DATE_FORMAT(date,'%Y-%m-%d') AS date FROM mytable WHERE user_id = '$session_user_id' AND date LIKE '$year-$month%' AND active = 1";
$result = mysql_query($query,$db_link) or die('cannot get results!');
while($row = mysql_fetch_assoc($result)) {

$events[$row['date']][] = $row;

}

Anyone have any ideas of why this could be happening?

Upvotes: 1

Views: 84

Answers (2)

Arnold Daniels
Arnold Daniels

Reputation: 16573

Based on @mellamokb's comment. Instead of using LIKE use BETWEEN. This also makes the query faster.

$query = "SELECT title, DATE_FORMAT(date,'%Y-%m-%d') AS date FROM mytable WHERE user_id = '$session_user_id' AND date BETWEEN '$year-$month-1' AND '" . date("Y-m-t", strtotime("$year-$month-1")) . "' AND active = 1";

Upvotes: 1

Augusto
Augusto

Reputation: 819

I guess you're using month = 1. You're getting 10, 11 and 12 only, since they are the only months that start with 1.

Upvotes: 2

Related Questions