Calculate date difference and print these days

Asked

Viewed 73 times

2

I would like to calculate the difference of two dates and print out all the dates between them, for example:

$data_inicio = new DateTime("08-02-2018");
$data_fim = new DateTime("10-03-2018");
($dateInterval = $data_inicio->diff($data_fim);
echo $dateInterval->days;

my return is : 30.

What I’d like to have is the days that are in that interval.

Example: 09-02-2018, 10-02-2018 ..... 09-03-2018 and etc.

How do I recover those values?

5 answers

3

You can take a look at this class Dateperiod in php.net:

$periodo = new DatePeriod(
     new DateTime('2018-02-08'),
     new DateInterval('P1D'),
     new DateTime('2018-03-10')
);

It will return you an array of Datetimes.

To iterate on them:

foreach ($periodo as $key => $value) {
    var_dump($value->format('d-m-Y'));
}

Example in Ideone

Source: www.php.net/manual/en/class.dateperiod.php

  • David, if you took this response from the OS in another language put the link as reference please.

3


You can interact on the two dates with a simple for, the class Datetime, there is a way add which may be inserted a Dateinterval for a new date with a value in your constructor P1D, that is, one more day on the date, example:

<?php

$data_inicio = new DateTime("2018-02-08");
$data_fim = new DateTime("2018-03-10");
$dateInterval = $data_inicio->diff($data_fim);
echo $dateInterval->days;
$datas = array();
for($i = $data_inicio; $i < $data_fim; $i = $data_inicio->add(new DateInterval('P1D')) )
{
    $datas[] = $i->format("d/m/Y");

}

print_r($datas);

Ideone Online Example

References:

2

You can use a repeat loop. Just capture the return in days and use a for, for example:

$data_inicio = new DateTime("08-02-2018");
$data_fim = new DateTime("10-03-2018");
$dateInterval = $data_inicio->diff($data_fim);


for ($i = 1; $i < $dateInterval->days; $i++) {

    /* Cria um intervalo de 1 dia */
    $interval = date_interval_create_from_date_string("+1 days");

    /* Adiciona esse intervalo na variável $data_inicio e imprime na tela o resultado */
    echo $data_inicio->add($interval)
        ->format("d/M/Y"), PHP_EOL;
}

Demonstration in Ideone

  • I thought about doing this with the return of 30, but then I wouldn’t know how to skip the month, adding another DAY, he understands this dynamic of skipping the month?

  • @Gustavosouza Yes. He will identify if the day x is the last of the month, if so, passes to the first day of the following month. On the demo link you can see that it "skipped" the day 28/Feb/2018 for 01/Mar/2018

0

Gustavo, I think Voce needs to perform the following code

<?php

$d1 = '2018-02-09';
$d2 = '2018-03-09';

$timestamp1 = strtotime( $d1 );
$timestamp2 = strtotime( $d2 );

$cont = 1;
while ( $timestamp1 <= $timestamp2 )
{
echo $cont . ' - ' . date( 'd/m/Y', $timestamp1 ) . PHP_EOL;
$timestamp1 += 86400;
$cont++;
}
?>

-1

You can use the class DateTime PHP and the simple algorithm below.

  • First you instantiate the dates

  • Use the method diff to calculate the difference in days

  • Call the public attribute d to obtain the quantity of days in number

  • Create a loop that counts and frames the smaller date to the larger date

  • Print the days by calling the method format

  • Prints the amount of days difference

    $data1 = new DateTime('01-02-2021');
    $data2 = new DateTime('05-02-2021');
    
    $quantidadeDias = $data2->diff($data1)->d;
    
    for ($data = $data1; $data <= $data2; $data->modify('+1 day')) {
      echo $data->format('l') . "\n";
    }
    
    echo 'Quantidade em dias:' . $quantidadeDias;
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.