Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to use a date function I found on SO to create an array of dates from between two given dates. It looks like:

function createDateRangeArray($strDateFrom,$strDateTo) {
  // takes two dates formatted as YYYY-MM-DD and creates an
  // inclusive array of the dates between the from and to dates.

  // could test validity of dates here but I'm already doing
  // that in the main script

  $aryRange=array();

  $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
  $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

  if ($iDateTo>=$iDateFrom) {
    array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry

    while ($iDateFrom<$iDateTo) {
      $iDateFrom+=86400; // add 24 hours
      array_push($aryRange,date('Y-m-d',$iDateFrom));
    }
  }
  return $aryRange;
}
$print_r($aryRange);

For some reason, it won't print the array. I know my $strDateFrom and $strDateTo values are good as I can echo them before and after the function. Any help is much appreciated!

share|improve this question
1  
Because you don't have $aryRange defined. It is a local variable within function which is never called. print_r(createDateRangeArray($strDateFrom,$strDateTo)) –  sashkello May 29 at 23:55
 
You're doing it wrong...$print_r($aryRange); should be print_r(createDateRangeArray('your start date', 'your end date')); –  cryptic ツ May 29 at 23:55
 
Yeah! I see! Awesome, thanks folks! I'm a newb with functions in php, obv, but I've used some OO languages before so this make perfect sense. –  RobDubya May 29 at 23:57
 
Make your comment an answers so I can mark it as solved? –  RobDubya May 29 at 23:58

closed as too localized by cryptic ツ, PeeHaa, Danack, Jocelyn, hjpotter92 May 30 at 0:19

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 1 down vote accepted

You never assign $aryRange to anything. The variable with this name within function is local and not the same as in print statement. You never call function, so never get the value from it.

Try this:

$strDateFrom = '2013-01-01';
$strDateTo = '2013-01-11';
$aryRange = createDateRangeArray($strDateFrom,$strDateTo);
print_r($aryRange);
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.