Sarathlal N

Get upcoming N days - PHP

If you need upcoming N days in PHP, you can use the below code snippet.

function get_upcoming_n_days($days, $format = 'Y-m-d'){
	$m = date("m"); $de= date("d"); $y= date("Y");
	$dateArray = array();
	for($i=0; $i<=$days-1; $i++){
		$dateArray[] = date($format, mktime(0,0,0,$m,($de+$i),$y)); 
	}
	return $dateArray;
}

Example

$days = get_upcoming_n_days(7);

echo '<pre>';
print_r($days);
echo '</pre>';

Output

Array {
	[0]=> 2020-02-14
	[1]=> 2020-02-15
	[2]=> 2020-02-16
	[3]=> 2020-02-17
	[4]=> 2020-02-18
	[5]=> 2020-02-19
	[6]=> 2020-02-20
}

The default date format will be Y-m-d. To change that format, add format in the function call as the second parameter.

$days = get_upcoming_n_days(7, 'd/m/Y');

Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!

Recent Posts

  1. Automating Code Linting with GitHub Actions for WordPress Plugins
  2. Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer
  3. The Ultimate Guide to Indexing in Database Design
  4. Understanding 'update_meta_cache' in WordPress - When to Use It, When Not to, and Its Impact on Database Queries
  5. A Guide to Configuring JavaScript and SCSS Paths in WordPress Plugins with @wordpress/scripts

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.