Search code examples
phpmysqldatemktime

find all months by mktime


I can select all records of recent month from database using,

$month = (mktime(0,0,0,date('n'),1)); 
$q="SELECT * FROM table WHERE timestamp>$month";

This query will generate August's(recent month) records. I have to select all records of each month(January/February../July).

How can I select all other month's details?

Thanks!


Solution

  • safety tips : change the column name bcoz timestamp is mySQL reserve keyword and it will lead to confusion , otherwise always use ` with column name.

    i asuume that dateColumn datatype is datetime

    <select name ="month" >
     <option value="1"> January </option>
     ...
    </select>
    

    now get the value of selected Month

    $month = $_POST['month']; // means integer value of month ( 1,2,3..12)
    

    then try with mySQL MONTH()

    SELECT * FROM tableName WHERE MONTH(`dateColumn` ) = $month
    

    edit

    if you store timestamp value in dateColumn then use this

    SELECT * FROM tableName WHERE MONTH(FROM_UNIXTIME(`dateColumn`)) = $month