0

It easier to explain in example so let just dive into it:

I have a range of date, say, 1st of December 2019 - 1st of Feb 2020. So my requirement is to get :

Sunday - 01 December - 2019 
Monday - 02 December - 2019 
Tuesday - 03 December - 2019 
Wednesday - 04 December - 2019
...
...
...
...
...
...
Friday - 31 January - 2020
Saturday - 01 February - 2020

in an array of string in PHP.

Prajwol Onta
  • 1,448
  • 5
  • 21
  • 48
  • Possible duplicate of [PHP: Return all dates between two dates in an array](https://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array) – ascsoftw Oct 18 '19 at 04:54

3 Answers3

0

You can use strtotime() to increase your date by 1 date:

$start = strtotime("1 December 2019");
$end = strtotime("1 Feb 2020");
$dateArray = [];
while($start <= $end) {
    $dateArray[] = date("l - d F Y", $start);
    $start = strtotime("+1 day", $start);
}
catcon
  • 1,295
  • 1
  • 9
  • 18
0

I think you are looking for some thing like this

function getDatesFromRange($start, $end, $format = 'l - d F - Y') { 

    // Declare an empty array 
    $array = array(); 

    // Variable that store the date interval 
    // of period 1 day 
    $interval = new DateInterval('P1D'); 

    $realEnd = new DateTime($end); 
    $realEnd->add($interval); 

    $period = new DatePeriod(new DateTime($start), $interval, $realEnd); 

    // Use loop to store date into array 
    foreach($period as $date) {                  
        $array[] = $date->format($format);  
    } 

    // Return the array elements 
    return $array; 
} 

// Function call with passing the start date and end date 
$dates = getDatesFromRange('2010-10-01', '2010-10-05'); 
echo '<pre>';
print_r($dates);

Output :

Array
(
    [0] => Friday - 01 October - 2010
    [1] => Saturday - 02 October - 2010
    [2] => Sunday - 03 October - 2010
    [3] => Monday - 04 October - 2010
    [4] => Tuesday - 05 October - 2010
)
Vibha Chosla
  • 733
  • 5
  • 12
0

Please use the below code.

<?php 
    function getDatesFromRange($start, $end, $format = 'l-d-F-Y') { 

        // Declare an empty array 
        $array = array(); 

        // Variable that store the date interval 
        // of period 1 day 
        $interval = new DateInterval('P1D'); 

        $realEnd = new DateTime($end); 
        $realEnd->add($interval); 

        $period = new DatePeriod(new DateTime($start), $interval, $realEnd); 

        // Use loop to store date into array 
        foreach($period as $date) {                  
            $array[] = $date->format($format);  
        } 

        // Return the array elements 
        return $array; 
    } 

    // Function call with passing the start date and end date 
    $Date = getDatesFromRange('2019-12-01', '2020-02-01'); 

    var_dump($Date); 

 ?>

Thanks,Arun

Arun kumar
  • 42
  • 4