-1

I'm trying to build a simple calendar and to get the output with nested whiles but it seems not to work and I'm getting a "PHP limit resouce reached" error. In "my own logic" I can't see what's wrong, hehe. Does anyone can point where is my mistake?

//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;
}

********* EDIT :*

Sorry guys, the code ACTUALLY works, and I run in a JOOMLA problem instead.

The solution for who uses Joomla and get this same error is:

  • goes to configuration.php in your site root;
  • insert last row (after '}') the comand

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

Thanks a lot

Adry
  • 307
  • 1
  • 4
  • 21
  • Well, what *goes* wrong? At which point does it fail? – Pekka Feb 18 '12 at 10:58
  • I don't see any problem with that code, I even ran it locally with no problems. Are you sure you don't have any other PHP code in the script? – Optimist Feb 18 '12 at 11:04
  • possible duplicate of [I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?](http://stackoverflow.com/questions/3207749/i-have-2-dates-in-php-how-can-i-run-a-foreach-loop-to-go-through-all-of-those-d) – Gordon Feb 18 '12 at 11:08
  • If you find an answer yourself, you can post it in the answers below and then later accept it. – Relequestual Feb 18 '12 at 11:17
  • @Relequestual thanks for the comment, I'm a newbee, I actually tryied to do what you said, but the sistem says I still don't have enough pontuation to answer questions myself! >;( – Adry Feb 25 '12 at 13:48
  • In which case, wait till you do then come back =] – Relequestual Feb 25 '12 at 21:47

2 Answers2

0

Basic calendar (day loop):

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

Advanced, this is easily printable, and shows day overlap in months:

$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>";

both are based on ISO8601 standard, and supports langauge setting:

setlocale(LC_TIME, "en_EN.utf8");

0

The wrong line is : $data = $mes; you should put it before the first while loop.

Toto
  • 89,455
  • 62
  • 89
  • 125