简体   繁体   中英

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?

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

Why not

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

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM