How do I start the week on monday?

539 views Asked by At

I'm working on a website that displays a year calendar of 2015. I use https://www.hscripts.com/scripts/php/yearly-calendar.php. You can see a demo on the page. On the demo you can see the week start on monday. I want to shift it the week start on monday. Does somebody know how I can alter the code below so the tablerow with days starts with monday and that the calender still counts correct.

<?php
$year = 2015;

echo "<table cellspacing=10 border=0>";
echo "<tr><td align=center colspan=3 class='topic'>$year Yearly Calendar</td></tr>";
for($ti=0;$ti<12;$ti++) {
          $month = $ti+1;
          if($ti%3==0) echo "<tr>";
            echo "<td valign='top'>";
        echo "<span class='monthnames'><b>".date( 'M', mktime(0, 0, 0, $month,1,0) )."</b></span>";
                  echo "<table border=0 class='tab'>";
                          echo "<tr class='daynames'><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>";
                                    $timestamp = mktime(0,0,0,$month,1,$year);
                                    $maxday = date("t",$timestamp); 
                                    $thismonth = getdate ($timestamp);
                                    $startday = $thismonth['wday'];
                                    for($i=0; $i<($maxday+$startday); $i++) {
                                    if(($i % 7) == 0 ) {
                                                  echo "<tr>\n";

                                        }
                           if($i < $startday) echo "<td></td>\n";
                                           else{

                          if($i - $startday + 1 == date('d') && date( 'F', mktime(0, 0, 0, $month,1,0) ) == date('F') && $year == date('Y'))
                            echo "<td align='center' valign='middle' class='curdate'>".($i - $startday + 1)."</td>\n";      
                          else
                            echo "<td align='center' valign='middle'>".($i - $startday + 1)."</td>\n";
                                                } 
                                            if(($i % 7) == 6 ) echo "</tr>\n";
                                    }
                  echo "</table>";
            echo "</td>";
          if($ti!=0 && ($ti+1)%3==0) echo "</tr>";
}
echo "</table>";
2

There are 2 answers

0
Glavić On BEST ANSWER

Try this:

$year = 2015;

echo '<table border="1">';
echo '<tr><th colspan="3">' . $year . ' Yearly Calendar</th></tr>';
for ($month = 1; $month <= 12; $month++) {
    if ($month - 1 % 3 === 0) echo '<tr>';
    $f = new DateTime("$year-$month-1");
    $l = new DateTime($f->format('Y-m-t'));
    echo '<td valign="top">';
    echo '<table border="1">';
    echo '<tr><th colspan="7">' . $f->format('F') . '</th></tr>';
    echo '<tr><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th><th>S</th></tr>';
    while ($f->format('n') === $l->format('n')) {
        echo '<tr>';
        for ($N = 1; $N < $f->format('N'); $N++) {
            echo '<td></td>';
        }
        for ($N = $f->format('N'); $N <= 7; $N++) {
            if ($f->format('n') === $l->format('n')) {
                echo '<td>' . $f->format('j') . '</td>';
                $f->modify('+1 day');
            } else {
                echo '<td></td>';
            }
        }
        echo '<tr>';
    }
    echo '</table>';
    echo '</td>';
    if ($month % 3 === 0) echo '</tr>';
}
echo '</table>';

It will return:

enter image description here

2
alphawow On

Here you go man - week start with monday and days are correct.

<?php
$year = 2015;

echo "<table cellspacing=10 border=0>";
echo "<tr><td align=center colspan=3 class='topic'>$year Yearly Calendar</td></tr>";
for($ti=0;$ti<12;$ti++) {
      $month = $ti+1;
      if($ti%3==0) echo "<tr>";
        echo "<td valign='top'>";
    echo "<span class='monthnames'><b>".date( 'M', mktime(0, 0, 0, $month,1,0) )."</b></span>";
              echo "<table border=0 class='tab'>";
                      echo "<tr class='daynames'><td>M</td><td>T</td><td>W</td><td>T</td>   <td>F</td><td>S</td><td>S</td></tr>";

                                $timestamp = mktime(0,0,0,$month,1,$year);
                                $maxday = date("t",$timestamp); 
                                $thismonth = getdate ($timestamp);
                                $startday = $thismonth['wday']-1;

                                for($i=0; $i<($maxday+$startday); $i++) {
                                if(($i % 7) == 0 ) {
                                              echo "<tr>\n";

                                    }


                       if($i < $startday) echo "<td></td>\n";
                                       else{



if($i - $startday + 1 == date('d') && date( 'F', mktime(0, 0, 0, $month,1,0) ) == date('F') && $year == date('Y'))
                        echo "<td align='center' valign='middle' class='curdate'>".($i - $startday + 1)."</td>\n";      
                      else
                        echo "<td align='center' valign='middle'>".($i - $startday + 1)."</td>\n";








                                            } 
                                        if(($i % 7) == 6 ) echo "</tr>\n";
                                }
              echo "</table>";
        echo "</td>";
      if($ti!=0 && ($ti+1)%3==0) echo "</tr>";
}
echo "</table>";
?>