if you have
$_POST['period'] = "2012-02";
First find the first day of the month:
$first_day = $_POST['period'] . "-01"
Then this query will use an index by Date if you have one:
$q = " SELECT * FROM projects WHERE Date BETWEEN '$first_day' AND LAST_DAY( '$first_day' ) " ;
You can also use inclusive-exclusive intervals, which work pretty well (you don’t have to worry about the DATE , DATETIME or TIMESTAMP column or about precision:
$q = " SELECT * FROM projects WHERE Date >= '$first_day' AND Date < '$first_day' + INTERVAL 1 MONTH " ;
Safety warning:
You must correctly avoid these values or use prepared statements. In short, use any method recommended today in PHP to avoid problems with SQL injection.
ypercubeᵀᴹ Feb 01 2018-12-01T00: 00Z
source share