Check only the current month


I have an invoice which will be filled in monthly, the records with the date do it correctly at Hora de Caracas/Venezuela , but in the following script it shows the values of example 05/agosto/17 al 05/septiembre/2017 , which would clash with an invoice which has been made the previous month, that is, only shows the current month in this case September, if October shows only the records that were made in October and so on.

$sql = "SELECT * FROM gasto_g WHERE  'create_at' BETWEEN DATE_SUB( NOW( ) , INTERVAL 1 MONTH ) AND NOW( ) + INTERVAL 4 HOUR ";

I read about curdate and it works. I take today's date, but if a record was made yesterday it does not show it, I do not know if I did it wrong.

This is the structure of the table:

asked by Anderson Rey 05.09.2017 в 06:46

1 answer


To get the records of the current year and month you can use CURRENT_DATE() :

$sql = "SELECT * FROM gasto_g 
        WHERE YEAR(create_at) = YEAR(CURRENT_DATE()) 
        AND MONTH(create_at)  = MONTH(CURRENT_DATE())";

If only the current month is of interest, regardless of the year:

$sql = "SELECT * FROM gasto_g 
        WHERE MONTH(create_at) = MONTH(CURRENT_DATE())";
answered by 05.09.2017 / 06:55