Lewis Day Lewis Day - 6 months ago 8
PHP Question

Defining Array Values

Here is my array;

$date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28',
'16-04-14','16-04-28','16-05-14','16-05-28','16-06-14','16-06-28','16-07-14',
'16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28',
'16-11-14','16-11-28','16-12-14','16-12-28');


What I'm trying to achieve is create a
foreach
statement that creates a similar output to the picture below.

Picture Example

enter image description here

Example

So for each line in the example the
start_date
and
end_date
are defined as variable which I can refer back to. The pattern goes like;

01
12
23
34
45...

What I'm basically trying to achieve is perform an action on each one of the lines in the picture using both the
start date
and
end date
and have the action repeated for each line in the picture.

Answer

Iterate through the array, and echo each value into the start date and the next value as the end date. Note that you will need logic for the last start date - is it the second to last array value? if so - you will need to reduce the count by 1.

$date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28',
  '16-04-14','16-04-28','16-05-14','16-05-28','16-06-14','16-06-28','16-07-14',
  '16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28',
    '16-11-14','16-11-28','16-12-14','16-12-28');

echo "<table>";

for($i=0;$i<count($date)-1;$i++){
   $x=$i+1;
   echo"<tr><td>".$date[$i]."</td><td>".$date[$x]."</td></tr>";
}

echo "</table>";

and this was the outcome:

Start Date  End Date
16-01-14    16-01-28
16-01-28    16-02-14
16-02-14    16-02-28
16-02-28    16-03-14
16-03-14    16-03-28
16-03-28    16-04-14
16-04-14    16-04-28
16-04-28    16-05-14
16-05-14    16-05-28
16-05-28    16-06-14
16-06-14    16-06-28
16-06-28    16-07-14
16-07-14    16-07-28
16-07-28    16-08-14
16-08-14    16-08-28
16-08-28    16-09-14
16-09-14    16-09-28
16-09-28    16-10-14
16-10-14    16-10-28
16-10-28    16-11-14
16-11-14    16-11-28
16-11-28    16-12-14
16-12-14    16-12-28