PHP循环几个月和几个月

时间:2012-02-18 10:55:57

标签: php loops while-loop

我正在尝试构建一个简单的日历,并使用嵌套的while获取输出,但它似乎无法工作,我得到“PHP限制资源已达到”错误。在“我自己的逻辑”中,我看不出有什么不对,呵呵。有谁能指出我的错误在哪里?

//Starting month
$mes = '2012-01-01';
//Counter months
$m = 1;
//LOOP month
while($m <= 12){
  //Variables days
  $data = $mes;
  //Counter days
  $x = 1;
  //LOOP days
  while($x <= 31){
    echo "$data";
    //adding one day
    $data = strtotime("$data + 1 days");
    $data = date("Y-m-d",$data);
    ++$x;
  }
  //adding one month
  $mes = strtotime("$mes + 1 month");
  $mes = date("Y-m-d",$mes);
  ++$m;
}

** * ** * *** 编辑: *

对不起家伙,代码ACTUALLY正常工作,而且我遇到了JOOMLA问题。

使用Joomla并获得同样错误的解决方案是:

  • 转到您网站根目录中的configuration.php;
  • 插入最后一行(在'}'之后)comand

ini_set('pcre.backtrack_limit', - 1);

非常感谢

2 个答案:

答案 0 :(得分:0)

错误的行是:$data = $mes;你应该把它放在第一个while循环之前。

答案 1 :(得分:0)

基本日历(日期循环):

$start = strtotime("01/01/2010");
$i_max = (date("L",$start)?366:365)-1;

for($i = 0;$i <= $i_max;$i++)
    $calendar
        [strftime("%B",$loop = strtotime("+$i day",$start))]
        [strftime("%V",$loop)]
        [strftime("%a",$loop)] = strftime("%e",$loop);

print_r($calendar);

高级,这很容易打印,并在几个月内显示日期重叠:

$calendar_year = 2010;
$start = strtotime("01-01-$calendar_year");
if(strftime("%u",$start) != "1")
    $start = strtotime("last Monday",$start);

$end = strtotime("31-12-$calendar_year");   
if(strftime("%u",$end) != "7")
    $end = strtotime("next Sunday",$end);


$loop = $start;
$d_names = $m_names= array();
while($loop<=$end)
{
    $data = explode(",",strftime("%d,%u,%V,%m,%Y,%a,%B",$loop));
    list($d,$d_nr,$w,$m,$y,$d_name,$m_name) = $data;
    $m = (int)$m;
    $w = (int)$w;
    $d = (int)$d;

    if(!isset($d_names[$d_nr]))$d_names[$d_nr] = $d_name;
    if(!isset($m_names[$m]))$m_names[$m] = $m_name;

    if($y!=$calendar_year)
        $m = $m==12?0:13;

    $calendar[$m][$w][$d_nr] = array($d,true);

    $loop = strtotime("+1 day",$loop);  
}
for($m = 1;$m<=13;$m++)
    foreach($calendar[$m] as $w =>&$days)
        if(count($days)<7)
        {
            for($i = 1; $i<=7;$i++)
                if(!isset($days[$i]))
                    $days[$i] = isset($calendar[$m-1][$w][$i])?
                        array($calendar[$m-1][$w][$i][0],false):
                        array($calendar[$m+1][$w][$i][0],false);
            ksort($days);               
        }
unset($calendar[0],$calendar[13]);

echo "<table>";
echo "<tr><th COLSPAN=8>$calendar_year</th></tr>";
foreach($calendar as $month => $weeks)
{
    echo "<tr><th COLSPAN=8>$m_names[$month]</th></tr>";
    $first_week = true; 
    foreach($weeks as $week => $days)
    {
        if($first_week)
        {
            echo "<tr><th></th>";
            foreach($days as $day_nr => $day_data)
                echo "<th>$d_names[$day_nr]</th>";
            echo "</tr>";

            $first_week = false;
        }
        echo "<tr><th>$week</th>";
        foreach($days as $day_nr => $day_data)
            echo "<td".($day_data[1]?"":" style='color:gray;'").">$day_data[0]</td>";
        echo "</tr>";
    }
}           
echo "</table>";

均基于ISO8601标准,并支持语言设置:

  

setlocale(LC_TIME,“en_EN.utf8”);