1
I have a field date
in my Mysql database and it stores the date of type y-m-d
which is already used in other queries, but now I need to capture only the month to perform a sum.
The code I have is this
date_default_timezone_set('America/Sao_Paulo');
$datamensal = date('m-d');
$stw = mysql_query("SELECT SUM(valortotal) as total_mensal FROM pedidos WHERE data = '$datamensal'");
But it didn’t work.... Does anyone know the right way to carry out this action?
But then type, in this case it returns equal to 11 which function I use to capture the current date of my computer and make the comparison.
– Alfredo Lima
@Alfredolima you are already using the
date()
php, just ask for the month for it. instead ofm-d
– rray
Okay, I get it, check for me. 
date_default_timezone_set('America/Sao_Paulo');
 $datamensal = date('m');
 $stw = mysql_query("SELECT SUM(valortotal) as total_mensal FROM pedidos 
 WHERE month(data) = '$datamensal'");
– Alfredo Lima
It is to be that @Alfredolima, will add up all the records of the month of November, it is good to specify the year tbm ;), you do not want to add up the records of previous years. I’ll edit the answer.
– rray
I did it the way you said, but it didn’t work, it doesn’t display anything.
– Alfredo Lima
@Alfredolima, with the year() stopped working or was already right? appeared some error?
– rray
No it was no longer working, no mistakes.
– Alfredo Lima
Let’s go continue this discussion in chat.
– rray
@Alfredolima decided not to forget to accept the answer :)
– rray