将日期放入数组中

时间:2012-03-04 17:39:57

标签: php arrays date

好的,目前我有一个数组..它看起来像:

array(
    ['march'] => array(
        '2', '3', '4', '5', '6', '23', '24', '25', '30'
    );
);

仅作为一个例子。我将如何在那个游行阵列中度过那些日子,做到这一点:

03/02/2012 - 03/06/2012 03/23/2012 - 03/25/2012 二零一二年三月三十日

任何?

2 个答案:

答案 0 :(得分:1)

您可以通过循环遍历数组找到连续的时间跨度:

    $months = array(
        'march' => array(
            '2', '3', '4', '5', '6', '23', '24', '25', '30'
        )
    );

    $current_day = 0;
    $span_start = 0;
    $span_end = 0;

    foreach($months['march'] as $day) {

      if ($day > ++$current_day) {
        $current_day = $day;

        if ($span_start) {
          // print it out:
          echo "$span_start..$span_end\n";
        }

        $span_start = $day;
      }

      $span_end = $day;

    }

    // print the last one:
    echo "$span_start..$span_end";

然而,对于格式化/解析/打印日期,其他人'建议使用内置的timedate函数绝对正确。

答案 1 :(得分:0)

$array = array(
    '2', '3', '4', '5', '6', '23', '24', '25', '30'
);

foreach($array as $date){
     echo '03/'.$date.'/2012;
}

您可能需要在单个日期之前添加零!