In this article, we will see how to get current month records in MySQL query. For data analysis and reporting, we need more data or records. So, we will learn how to get current month data in MySQL PHP. For getting data for the current month in MySQL we use the month() function. MySQL month() returns the month number for a given date. The return value is from 1 to 12.
So, let's see SQL query to get current month data and how to get current month data in PHP from MySQL Database.
Get Current Month Data In MySQL Example:
In this example, we will see an SQL query to get data for the current month in MySQL.
In your table, the record needs to be like date and month.
select * from users where MONTH(order_date) = MONTH(now()) and YEAR(order_date) = YEAR(now());
In the above query, we use the now() function to get the current date, and the month() function to get the current month number of date values.
How to Get New Users in the Current Month in MySQL
Using the below MySQL query you can get the new users in the current month with date and count.
select date(created_at), count(*) from users where MONTH(created_at) = MONTH(now()) and YEAR(created_at) = YEAR(now()) group by date(created_at);
Get the current month using the MONTH() function.
Current Month: 9