PHP - Calculate date between rows in a array loop (foreach)

微笑、不失礼 提交于 2021-01-29 03:57:35

问题


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.


    回答1:


    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




    回答2:


    $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




    回答3:


    <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>
    



    回答4:


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

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



    来源:https://stackoverflow.com/questions/41349407/php-calculate-date-between-rows-in-a-array-loop-foreach

    标签
    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!