Adding dates within a date range

Hi,

It’s been a while since doing any PHP code, so I’m a bit rusty. I need to create a multi-dimensional array that contains:

  1. Date
  2. Description
  3. Amount

The array will be added to, several times. That is, once for a number of 14 day intervals, monthly intervals, 5 weekly intervals, etc,etc

So, I need the PHP code setup so that I can simply parse it the ‘start date’, ‘end date’ and the interval number of days (7,14,etc)

Once this array is built, it then needs to be sorted to date and description order. Then some code to go through the array and create a CSV file, which is later used in a spreadsheet.

I have been using this code …

<?php

date_default_timezone_set('Australia/Melbourne');
$startDate = "2014-07-01";
$endDate = "2015-06-30";
$daysBetween = 14;
$finalResult = array();

function addDayswithdate($date,$days, $endDate, &$finalResult){
  $tempDate = strtotime($date);
  $tempDate += 3600*24*$days;

  if ($tempDate < strtotime($endDate)) {
    $finalResult[] = date("Y-m-d", $tempDate);
    addDayswithdate(date("Y-m-d", $tempDate), $days, $endDate, $finalResult);
  } else {
    return true;	
  }
}

addDayswithdate($startDate,$daysBetween, $endDate, $finalResult);
print_r($finalResult);

?>

and the output is …

Array
(
[0] => 2014-07-15
[1] => 2014-07-29
[2] => 2014-08-12
[3] => 2014-08-26
[4] => 2014-09-09
[5] => 2014-09-23
[6] => 2014-10-07
[7] => 2014-10-21
[8] => 2014-11-04
[9] => 2014-11-18
[10] => 2014-12-02
[11] => 2014-12-16
[12] => 2014-12-30
[13] => 2015-01-13
[14] => 2015-01-27
[15] => 2015-02-10
[16] => 2015-02-24
[17] => 2015-03-10
[18] => 2015-03-24
[19] => 2015-04-06
[20] => 2015-04-20
[21] => 2015-05-04
[22] => 2015-05-18
[23] => 2015-06-01
[24] => 2015-06-15
[25] => 2015-06-29
)

so, this only creates the date. I need to extend it to include the “Description” and the “Amount”. I’m not entirely sure how to extend the array to be a multi-dimensional array. Also, in the code above, I don’t understand why the function calls itself ?

Once I have the multi-dimasional array sorted out, and how to reference each key/index and value, then I should be okay to extend it for the other iterations. I see there are some easy examples at http://php.net/manual/en/function.fputcsv.php for writing an array to a CSV file.

If someone can assist please. :slight_smile:

to add “Description” and “Amount” in array you can replace this line

$finalResult[] = date("Y-m-d", $tempDate); 

with

$finalResult[] = array("date" => date("Y-m-d", $tempDate, "description" => $description, "amount" => $amount); 

also for function calling itself, google “recursive function”, you will get the answer

i hope this will help you

Thanks for that code, it’s now working okay …

<?php

date_default_timezone_set('Australia/Melbourne');
$startDate = "2014-07-01";
$endDate = "2015-06-30";
$daysBetween = 14;
$finalResult = array();

$description = "some desrciption";
$amount = "208.93";

function addDayswithdate($date,$days, $endDate, &$finalResult, $description, $amount){
  $tempDate = strtotime($date);
  $tempDate += 3600*24*$days;

  if ($tempDate < strtotime($endDate)) {
    $finalResult[] = array("date" => date("Y-m-d", $tempDate), "description" => $description, "amount" => $amount);
    addDayswithdate(date("Y-m-d", $tempDate), $days, $endDate, $finalResult, $description, $amount);

  } else {
    return true;	
  }
}
 
addDayswithdate($startDate,$daysBetween, $endDate, $finalResult, $description, $amount);
print_r($finalResult);

?>

and the ouput is …

Array
(
[0] => Array
(
[date] => 2014-07-15
[description] => some desrciption
[amount] => 208.93
)

[1] =&gt; Array
    (
        [date] =&gt; 2014-07-29
        [description] =&gt; some desrciption
        [amount] =&gt; 208.93
    )
  etc,etc

Yes, I know what recursive means. My question was, why was there need for the function to call itself ? Is there a better way to do this. I suppose if the function didn’t call itself, the code would have to be within a “while” loop, is that correct ?

I prefer to use a while loop …

<?php

date_default_timezone_set('Australia/Melbourne');
$startDate = "2014-07-01";
$endDate = "2015-06-30";
$daysBetween = 14;
$finalResult = array();

$description = "some description";
$amount = "208.93";

function addDayswithdate($date,$days, $endDate, &$finalResult, $description, $amount){
  $tempDate = strtotime($date);

  while ($tempDate < strtotime($endDate)) {
    $tempDate += 3600*24*$days;
    $finalResult[] = array("date" => date("Y-m-d", $tempDate), "description" => $description, "amount" => $amount);
  }
}

addDayswithdate($startDate,$daysBetween, $endDate, $finalResult, $description, $amount);
print_r($finalResult);

?>