I have a website that was launched a few months ago. I would like to write some code that will look through every month, since launch, and grab metrics for that month.
My question is: What is the best way to specify a start date, and then iterate by month, all the way up until the current month (or to another specified date).
Does anyone have any suggestions? I'm using PHP and a mySQL database.
You could use strtotime
, to increment the date by +1 month
:
$date1 = strtotime('2009-01-01');
$date2 = strtotime('2010-01-01');
while ($date1 <= $date2) {
echo date('Y-m-d', $date1) . "\n";
$date1 = strtotime('+1 month', $date1);
}
And if you have PHP >= 5.3.0, you can use DateTime::add
with an DateInterval
object
Check the above example here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With