1

I'm trying to create a calendar system that will show what jobs are booked in for certain days, I want it to be a horizontal calendar that has all of the days of the months as a table row and days of the week as the next row. I've got the first row sorted but I need to have the second row repeat the array of the days of the week until the month ends.

      //Establish How many days are in the current Month
  $currentMonthDays = date("t");




  $daysOfWeek = array('M','T','W','T','F','Sa','Su');

      echo '<table cellpadding="0" cellspacing="0" border="1" bordercolor="ffffff">
            <tr>';


                    for ($i = 1; $i <= $currentMonthDays; $i++)
                    {

                        echo '<td width="30" align="center">'.$i.'</td>';
                    }

      echo '</tr><tr>';


                    foreach($daysOfWeek as $day){
                    echo '<td width="30" align="center">'.$day.'</td>';
                    }


      echo '</tr>';

3 Answers 3

1

Try this :

 //Establish How many days are in the current Month
  $currentMonthDays = date("t");
  $daysOfWeek = array('M','T','W','T','F','Sa','Su');
      for ($i = 0; $i <= $currentMonthDays-1; $i++){
          $arrayWeekDays[] = $daysOfWeek[($i%7)];
      }
      echo '<table cellpadding="0" cellspacing="0" border="1" bordercolor="ffffff">
            <tr>';


                    for ($i = 1; $i <= $currentMonthDays; $i++)
                    {

                        echo '<td width="30" align="center">'.$i.'</td>';
                    }

      echo '</tr><tr>';


                    foreach($arrayWeekDays as $day){
                    echo '<td width="30" align="center">'.$day.'</td>';
                    }


      echo '</tr>';

Cheers :)

Sign up to request clarification or add additional context in comments.

3 Comments

Super @waren0809 awesome, Good Job
Thanks @Muthusamy :)
Thanks that's great, I suspected I would have to use modulus at some point but was breaking my brain going around in circles. Now I need to start on all the other little sticking points like establishing what day the month starts on, how many cells to offest that by etc.
0

Try below code

    $currentMonthDays = date("t");

    $daysOfWeek = array('M', 'T', 'W', 'T', 'F', 'Sa', 'Su');

    echo '<table cellpadding="0" cellspacing="0" border="1" bordercolor="ffffff">
        <tr>';

    for ($i = 1; $i <= $currentMonthDays; $i++) {

        if ($i % 7 == 1) 
            echo '</tr><tr>';
        echo '<td width="30" align="center">' . $i . '</td>';
    }

    echo '</tr><tr>';


    foreach ($daysOfWeek as $day) {
            echo '<td width="30" align="center">' . $day . '</td>';
    }


    echo '</tr>';

maybe helps you.

Comments

0

The easiest way to do this to use the DateTime class and formatting methods you used to get the number of days to also format the date for you.

$currentMonthDays = date('t');
$month = date('m');
$year = date( 'Y');

for ($i = 1; $i <= $currentMonthDays; $i++) {
    echo '<td width="30" align="center">'.$i.'</td>';
}
$day = new DateTime();
for ($i = 1; $i <= $currentMonthDays; $i++) {
    $day->setDate( $year, $month, $currentMonthDays);
    // this outputs first 3 letters of day, but you can truncate if you want..
    echo '<td width="30" align="center">'.$day->format('D').'</td>';
}

You could also avoid the need for 2 loops by merging the two tds and formatting the cell by either adding a br element between the day of the month and the day of the week, or using CSS formatting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.