K
K
KRHD2019-02-28 13:15:25
PHP
KRHD, 2019-02-28 13:15:25

How to make an array of dates?

There is a starting point. Let's say 2019-01-10.
It is necessary to display all the dates between 2019-01-10and in an array 2019-02-10.
How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2019-02-28
@FanatPHP

For more than 10 years, PHP has been providing very convenient methods for working with dates, which are unknown to the authors of video courses and - as a result - to the vast majority of users. For example, there is a DatePeriod
class suitable for this case

$begin = new DateTime( '2019-01-10' );
$end = new DateTime( '2019-02-10'); 
$end = $end->modify( '+1 day' ); // если надо включительно
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){
    echo $date->format("Y-m-d") . "\n";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question