Member-only story
Get the next 7 days from the current date in PHP -
Hi friends, in this tutorial I will explain how to get the next 7 days from the current date in PHP. This kind of feature is often required to develop dynamic web applications or ERP software to maintain the dates. In order to print the dates sequentially from a particular date or start date, you have to follow the below steps.
Also read, PHP difference between two dates in years, months and days
Required steps to get next 7 days from current date in PHP
- First of all, you must know how to increment a date by PHP +1 day.
- We have to run a for loop based on the range of days to echo each of the dates between the start date and end date. For example, if we want to print the dates for 7 days then the for loop will be executed upto 7 as shown below
for($i=1;$i<7;$i++)
PHP date +1 day
$date = date(‘Y-m-d’);
$nextday = date(‘Y-m-d’,strtotime(‘+1 day’,strtotime($date)));
Output:- 2022-02-27 (My current date was 2022-02-26)
Complete Code:-
<?php $date = date('Y-m-d'); //today date $weekOfdays = array(); for($i =1; $i <= 7; $i++)
{ $date = date('Y-m-d', strtotime('+1 day', strtotime($date))); $weekOfdays[] = date('l : Y-m-d', strtotime($date));
}
print_r($weekOfdays);
echo '<br>';
echo '<p>Next 7 days from the current date are as shown below</p>'; foreach($weekOfdays…