Get next 7 days from the current date in PHP


7 days from the current date in PHP

Hi friends, in this tutorial I will explain how to get 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 as $days){

      echo $days.'<br>';
  }

?>

Explanation of the output:

  • First, we will print the current date using the PHP built in function date(‘Y-m-d’)
  • Second, we will declare an array and each date is incremented by one inside the forloop and stored in the array.
  • Third, if we print the array outside the forloop then the output will be shown as follows

Output:-

Array ( [0] => 2022-02-27 [1] => 2022-02-28 [2] => 2022-03-01 [3] => 2022-03-02 [4] => 2022-03-03 [5] => 2022-03-04 [6] => 2022-03-05 )

  • Next, we will run a foreach loop to print the desired dates and the output will be shown as given below.

Output:-

Next 7 days from the current date are as shown below

2022-02-27
2022-02-28
2022-03-01
2022-03-02
2022-03-03
2022-03-04
2022-03-05

Also, you can echo dates based on your target date. In my case, it is 7.

Conclusion:- I hope this tutorial will help you to understand the concept and print the future dates.


Leave a Comment