PHP - 计算数组循环中行之间的日期(foreach)

i was searching how to calculate values in an array loop, a foreach preciselly.

The problem is that i have to calculate the days between dates of this result.

Example:

<ul>
 <?php
   $values[] = array('example'=>'example1','date'=>'2016-25-12');
   $values[] = array('example'=>'example2','date'=>'2016-26-12');
   $values[] = array('example'=>'example3','date'=>'2016-28-12');
   $values[] = array('example'=>'example4','date'=>'2016-30-12');

 foreach($values[] as $example){
   echo "<li>".$example['example']." - ".$example['date']." - ".$DAYS BETWEEN EACH DATE$."</li>";
}

?>
</ul>

To become something like this:

example1 - 2016-25-12 example1 - 2016-26-12 - 1 day(s) example1 - 2016-28-12 - 2 day(s) example1 - 2016-30-12 - 2 day(s)

How can i make this calc? *the first shows nothing because its the first register.. the others shows how many days passed between the actual row and the last one (above).

Ty.

I changed the formatting of your date in the array, as YYYY-DD-MM isn't recognized as a valid format, YYYY-MM-DD is.

Then it's just a matter of saving the previous value and calculating the difference in days.

<ul>
<?php
$values = array();
$values[] = array('example' => 'example1', 'date' => '2016-12-25');
$values[] = array('example' => 'example2', 'date' => '2016-12-26');
$values[] = array('example' => 'example3', 'date' => '2016-12-28');
$values[] = array('example' => 'example4', 'date' => '2016-12-30');

$previous = null; // Define the variable
foreach($values as $example){
    echo "<li>".$example['example']." - ".$example['date'];

    if ($previous !== null) { // If it's null, we're in the first loop
        $difference= strtotime($example['date'])-strtotime($previous);
        echo " - ".floor($difference/(60*60*24)); // $difference is in seconds, so convert to days by dividing by 60sec*60min*24hrs
    }

    $previous = $example['date']; // Set the current value, so it will be saved for the next iteration
    echo "</li>";
}

?>
</ul>

Output of the above would be

  • example1 - 2016-12-25
  • example2 - 2016-12-26 - 1
  • example3 - 2016-12-28 - 2
  • example4 - 2016-12-30 - 2

Demo

Use date_diff() function, you can get result in days as well

Documentation: http://php.net/manual/en/function.date-diff.php

$date = false;
foreach($values as $item) {
  if ($date == false) {
    $date = $item['date'];
  } else {
    $datetime1 = new DateTime($date);
    $datetime2 = new DateTime($item['date']);
    $interval = $datetime1->diff($datetime2);
    echo $interval->format('%R%a days');
    $date = $item['date'];
  }
}

Something like that. I did not check the syntax, just wrote on the fly

<ul>
    <?php
    $values[] = array('example'=>'example1','date'=>'2016-25-12');
    $values[] = array('example'=>'example2','date'=>'2016-26-12');
    $values[] = array('example'=>'example3','date'=>'2016-28-12');
    $values[] = array('example'=>'example4','date'=>'2016-30-12');

    $date_prev = 0;
    foreach($values as $example){
        $parts = explode( '-', $example[ 'date' ]);
        $date_current =  strtotime($parts[0] . '/' . $parts[2] . '/' . $parts[1]); // year-month-day format
        $diff = $date_current - $date_prev;
        $DAYS_BETWEEN_EACH_DATE = intval($diff / 86400); // 86400 seconds in one day
        echo "<li>" . $example['example'] . " - " . $example['date'] . ($date_prev ? " - ". $DAYS_BETWEEN_EACH_DATE . ' day(s)' : '' ) . "</li>";
        $date_prev = $date_current;
    }

    ?>
</ul>