1

I want to display dates between two dates in foreach loop. Suppose the date starts May 1 2014 and ends May 3, 2014 .I want to display like this

//With for loop alone this will display what I wanted

for($i=$dateFrom;$i<=$dateTo;$i++) {
 echo $i."<br>";
 }
May 1,2014
May 2, 2014
May 3, 2014

But this code returns when using foreach loop to display it is template

May 3,2014
May 3, 2014
May 3, 2014

This is my code in action.php

$dateTo = $r->getParameter("date_to", date('Y-m-d'));
$dateFrom = $r->getParameter("date_from", date('Y-m-d', strtotime('7 days ago', strtotime($dateTo))));
$this->data=array();
foreach($this->deposits as $d) {
    $obj = new stdClass();
    $obj->created_by = $d->created_by;
    $obj->date_created = $d->date_created;
  for($i =$dateFrom;$i <= $dateTo;$i++) {
    $date=$i;//I think the problem is here.I am not sure how to make this work
  }
  $obj->amount = $d->amount;
  $obj->dateCovered = $date;
  $this->data[] = $obj;
}

template.php

<?php foreach ($data as $i => $d): ?>
        <tr class="<?php echo ($i%2==0)?'even':'odd' ?>">
            <td><?php echo $d->id ?></td>   
            <td>
            <?php echo $d->dateCovered ?>//this will display the last date only
            </td>
            <td>
                <?php $amountTotal += $d->amount ?>
                PHP <?php echo number_format($d->amount, 2) ?>
            </td>
            <td><?php echo $d->created_by ?></td>
            <td><?php echo date("F d, Y",strtotime($d->date_created)) ?></td>
        </tr>
    <?php endforeach ?>

My problem here is it does not display all dates between two dates.Instead it will only display the last date between two dates.What's wrong with this code?

  • possible duplicate of [PHP: Return all dates between two dates in an array](http://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array) – Narendrasingh Sisodia May 28 '15 at 05:11

2 Answers2

1

you can do like this

$date_from = strtotime("1 May 2015");
$date_to = strtotime("15 May 2015");

$oneDay = 60*60*24;

for($i=$date_from; $i<=$date_to; $i=$i+$oneDay)
{
    echo date("F j, Y", $i) . "<br>";
}

Now you can try with this..

$dateTo = $r->getParameter("date_to", date('Y-m-d'));
$dateFrom = $r->getParameter("date_from", date('Y-m-d', strtotime('7 days ago', strtotime($dateTo))));

$oneDay = 60*60*24;
$date_time = strtotime($dateFrom);

$this->data=array();
foreach($this->deposits as $d) {
    $obj = new stdClass();
    $obj->created_by = $d->created_by;
    $obj->date_created = $d->date_created;

    $date= date("F j, Y", $date_time);
    $date_time += $oneDay;
    $obj->amount = $d->amount;
    $obj->dateCovered = $date;
   $this->data[] = $obj;
}
user3419778
  • 856
  • 3
  • 8
  • 11
  • well that will work, but I have a template that will call the action.php.Using foreach loop.Any Idea?I tried your code but it will always display the last date instead of all dates between two dates –  May 28 '15 at 05:59
  • Please try with 2nd part – user3419778 May 28 '15 at 06:16
0
<?php
$date_from = strtotime("10 September 2000");
$date_to = strtotime("15 September 2000");


$day_passed = ($date_to - $date_from); //seconds
$day_passed = ($day_passed/86400); //days

$counter = 1;
$day_to_display = $date_from;
while($counter < $day_passed){
    $day_to_display += 86400;
    echo date("F j, Y \n", $day_to_display);
    $counter++;
}
?>
ram obrero
  • 197
  • 10
  • yes I know your code works but I want to do it base on my code in action.php –  May 28 '15 at 05:24