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. REST API Methods Explained with Best Practices for Building Clean and Secure APIs
  2. My 28-Day Plan to Master Modern WordPress Development Using AI Tools
  3. Scaling WordPress - How Custom Database Tables Solve the Post Meta Bottleneck
  4. WordPress Transients Explained - A Developer's Guide to Site Performance
  5. Behind the Click - The Hidden Journey of Your Web Requests

Your Questions / Comments

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