PHP显示使用print_r返回数组值

时间:2013-05-29 23:53:13

标签: php arrays calendar

我正在尝试使用我在SO上找到的日期函数来创建两个给定日期之间的日期数组。它看起来像:

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

由于某种原因,它不会打印数组。我知道我的$strDateFrom$strDateTo值很好,因为我可以在函数之前和之后回显它们。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您永远不会将$aryRange分配给任何内容。函数中具有此名称的变量是本地的,与print语句中的变量不同。你永远不会调用函数,所以永远不要从中获取值。

试试这个:

$strDateFrom = '2013-01-01';
$strDateTo = '2013-01-11';
$aryRange = createDateRangeArray($strDateFrom,$strDateTo);
print_r($aryRange);