日历生成奇怪失败

时间:2010-10-20 09:40:53

标签: php date

我开始生成一个包含从这个(现在)月开始的12个月日历的数组。这是针对特定应用程序的,需要特殊代码,因此我无法使用日历库。

这是我的代码:

header('Content-type: text/plain');

$Cal1 = array();

$now = new fTimestamp('now');
$now = $now->modify('Y-m-1 00:00:00');

for ( $i = 0; $i < 12; $i++ ) {
 if ( $i > 1 ) {
  $then = $now->adjust("+$i months");
 }
 elseif ( $i == 1 ) {
  $then = $now->adjust("+1 month");
 }
 else {
  $then = $now;
 }
 $thisMonth = $then->format('F');
 $Cal1[$thisMonth] = array();
 $thisMonthDays = $then->format('t');
 for ( $j = 0; $j < $thisMonthDays; $j++ ) {
  if ( $i > 1 ) {
   $then = $then->adjust("+$i days");
  }
  elseif ( $i == 1 ) {
   $then = $then->adjust("+1 day");
  }
  $thisDate = $then->format('j');
  $thisDay = $then->format('l');
  $Cal1[$thisMonth][$thisDate] = $thisDay;
 }
}

var_dump($Cal1);

这应该生成一个形式的数组:

array {
    ["Month_Name"] => array {
        [Day_Number] => "Day_Name"
        etc...
    }
    etc...
}

脚本输出正确的月数,但不是正确的天数......完全转储非常冗长,所以我只发布10月,2月和3月:

array(12) {
  ["October"]=>
  array(1) {
    [1]=>
    string(6) "Friday"
  }
  ["February"]=>
  array(22) {
    [5]=>
    string(8) "Saturday"
    [9]=>
    string(9) "Wednesday"
    [13]=>
    string(6) "Sunday"
    [17]=>
    string(8) "Thursday"
    [21]=>
    string(6) "Monday"
    [25]=>
    string(6) "Friday"
    [1]=>
    string(7) "Tuesday"
    [29]=>
    string(7) "Tuesday"
    [2]=>
    string(8) "Saturday"
    [6]=>
    string(9) "Wednesday"
    [10]=>
    string(6) "Sunday"
    [14]=>
    string(8) "Thursday"
    [18]=>
    string(6) "Monday"
    [22]=>
    string(6) "Friday"
    [26]=>
    string(7) "Tuesday"
    [30]=>
    string(8) "Saturday"
    [4]=>
    string(9) "Wednesday"
    [8]=>
    string(6) "Sunday"
    [12]=>
    string(8) "Thursday"
    [16]=>
    string(6) "Monday"
    [20]=>
    string(6) "Friday"
    [24]=>
    string(7) "Tuesday"
  }
  ["March"]=>
  array(19) {
    [6]=>
    string(6) "Sunday"
    [11]=>
    string(6) "Friday"
    [16]=>
    string(9) "Wednesday"
    [21]=>
    string(6) "Monday"
    [26]=>
    string(8) "Saturday"
    [31]=>
    string(8) "Thursday"
    [5]=>
    string(8) "Thursday"
    [10]=>
    string(7) "Tuesday"
    [15]=>
    string(6) "Sunday"
    [20]=>
    string(6) "Friday"
    [25]=>
    string(9) "Wednesday"
    [30]=>
    string(6) "Monday"
    [4]=>
    string(6) "Monday"
    [9]=>
    string(8) "Saturday"
    [14]=>
    string(8) "Thursday"
    [19]=>
    string(7) "Tuesday"
    [24]=>
    string(6) "Sunday"
    [29]=>
    string(6) "Friday"
    [3]=>
    string(9) "Wednesday"
  }

现在,怎么回事?

1 个答案:

答案 0 :(得分:1)

不能直接回答你的问题,但我会这样做的更简单:

$cursor = mktime(0, 0, 0, date('m'), 1);
$end = strtotime('+1 year', $cursor);

$out = array();

while ($cursor < $end) {
    $out[date('F', $cursor)][date('j', $cursor)] = date('l', $cursor);
    $cursor = strtotime('+1 day', $cursor);
}

var_dump($out);

要直接回答你的问题,你总是会以越来越大的数字相对于自己调整$then,因此你会跳过几天并且得到时髦的数字。

 for ( $j = 1; $j < $thisMonthDays; $j++ ) {
     $then = $then->adjust("+$i days");
 }
  1. $then是第1个,你加1(第一次迭代)
  2. $then是第二个,你加2(第二次迭代)
  3. $then是第4个,你加3个
  4. $then是第7个,你加4个
  5. etc pp。