Loop through dates with strtotime() function

This simple method allows you to start at a date and iterate by either days or months until you reach the end date using my favourite PHP function, strtotime().

<?php
	// Set timezone
	date_default_timezone_set('UTC');
 
	// Start date
	$date = '2009-12-06';
	// End date
	$end_date = '2020-12-31';
 
	while (strtotime($date) <= strtotime($end_date)) {
		echo "$date\n";
		//use +1 month to loop through months between start and end dates
		$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
	}
 
?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s