Wern Ancheta
Wern Ancheta

Reputation: 23337

date and time query in php

I'm trying to select all the records that has the same month with the current month.

 $date=date('Y-m-d');

    $month=substr($date,5,2);


    $res=mysql_query("SELECT DATE FROM reports");
    while($row=mysql_fetch_assoc($res)){

    $months=substr($row['DATE'],5,2);

}

How can I do it?

Upvotes: 1

Views: 201

Answers (2)

MatTheCat
MatTheCat

Reputation: 18761

Why not

$query = 'SELECT DATE FROM reports WHERE MONTH(DATE) = MONTH(CURDATE())';

?

Upvotes: 2

edorian
edorian

Reputation: 38981

You could sql let handle that for you like this

$query = "SELECT DATE FROM reports WHERE MONTH(DATE) = ".date("m");

in case you want only everything of that month this year then the query would look like this:

$query = "SELECT DATE FROM reports WHERE YEAR(DATE) = ".date("Y")." MONTH(DATE) = ".date("m");

Upvotes: 3

Related Questions