Skip to content
Advertisement

How to get the last 7 weeks, 7 months date range from current date PHP?

How can I get the date range of last seven weeks /months from the current date, i have already done with last seven days

fetch last seven days, weeks and month sales total grouped by 7 particular days, 7 particular weeks and 7 particular months

ex:

Last seven weeks

Last seven Months

The below query I have done for last seven days

Now as the same way, I am thinking a way to fetch last 7 weeks and last seven months.

Advertisement

Answer

If you have each single “day” summarized in a single table, then it is easy to summarize that over the last N days, whether there is several weeks or months.

If the summary table has sum_dollars, count_items, then

(Note that I did not have avg_price in the summary table because AVG(avg_..) is usually the wrong formula.)

So, build and maintain (nightly) a summary table by day. See http://mysql.rjweb.org/doc.php/summarytables

PS. “today” is CURDATE() in SQL. More specifically, that is midnight this morning. “7 months ago” is CURDATE() - INTERVAL 7 MONTH

Likely BUG: Don’t use BETWEEN; it is ‘inclusive’. Instead, do something like

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement