Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    262
    Thanks
    23
    Thanked 1 Time in 1 Post

    Post Coloring today and week-end in a calendar...

    Good day to you all,
    I'm working on a calendar script.
    I'm facing 2 questions;

    1. How, if is this month, can I have the day in a different color ?
    2. how could I have the Saturday and Sunday show in different color ?


    Here is my code:
    PHP Code:

    <?
    function showMonth($month$year)
    {
        
    $date mktime(1200$month1$year);
        
    $daysInMonth date("t"$date);
        
    // calculate the position of the first day in the calendar (sunday = 1st column, etc)
        
    $offset date("w"$date);
        
    $rows 1;

        echo 
    "<table border=\"1\" align=\"center\">\n";
        echo 
    "<tr><td colspan=\"7\"><h1>" date("F Y"$date) . "</h1></td></tr>";
        echo 
    "<tr><th>Sunday</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th></tr>";
        echo 
    "\n\t<tr>";
        for(
    $i 1$i <= $offset$i++)
        {
            echo 
    "<td></td>";
        }
        for(
    $day 1$day <= $daysInMonth$day++)
        {
            if( (
    $day $offset 1) % == && $day != 1)
            {
                echo 
    "</tr>\n\t<tr>";
                
    $rows++;
            }

          echo 
    "<td  valign=\"top\"><table border=\"0\" align=\"center\">";
          echo 
    "<tr><td width=\"100\" valign=\"top\" bgcolor=\"cccc99\">" $day "</td></tr>";
          echo 
    "<tr><td width=\"100\" height=\"100\" valign=\"top\"><br/></td></tr>";
          echo 
    "</table></td>";

        }
        while( (
    $day $offset) <= $rows 7)
        {
            echo 
    "<td></td>";
            
    $day++;
        }
        echo 
    "</tr>\n";
        echo 
    "</table>\n";
    }
    showmonth(122008);
    ?>
    Thanks !

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    1. How, if is this month, can I have the day in a different color ?
    PHP Code:
    date("n"); 
    would give the numeric representation of current month. So you could compare this value with the received value of $month and apply a style accordingly.
    2. how could I have the Saturday and Sunday show in different color ?
    Similar to the above, date("w"); would give the values from 0-6 representing Sunday-Saturday. So you could compare this value with your loop counter $day

    Refer the manual for date()
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    262
    Thanks
    23
    Thanked 1 Time in 1 Post
    Here is where I'm at whit your help...

    PHP Code:
    <?
    function showMonth($month$year)
    {
        
    $date mktime(1200$month1$year);
        
    $daysInMonth date("t"$date);
        
    // calculate the position of the first day in the calendar (sunday = 1st column, etc)
        
    $offset date("w"$date);
        
    $rows 1;

        echo 
    "<table border=\"1\" align=\"center\">\n";
        echo 
    "<tr><td colspan=\"7\"><h1>" date("F Y"$date) . "</h1></td></tr>";
        echo 
    "<tr><th>Sunday</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th></tr>";
        echo 
    "\n\t<tr>";
        for(
    $i 1$i <= $offset$i++)
        {
            echo 
    "<td></td>";
        }
        for(
    $day 1$day <= $daysInMonth$day++)
        {
            if( (
    $day $offset 1) % == && $day != 1)
            {
                echo 
    "</tr>\n\t<tr>";
                
    $rows++;
            }
    if (
    $month == date("m")){
      if (
    $day == || $day == 6){
          echo 
    "<td  valign=\"top\"><table border=\"0\" align=\"center\">";
          echo 
    "<tr><td width=\"100\" valign=\"top\" bgcolor=\"999966\">" $day "</td></tr>";
          echo 
    "<tr><td width=\"100\" height=\"100\" valign=\"top\"><br/></td></tr>";
          echo 
    "</table></td>";
          }
    }else{
          echo 
    "<td  valign=\"top\"><table border=\"0\" align=\"center\">";
          echo 
    "<tr><td width=\"100\" valign=\"top\" bgcolor=\"cccc99\">" $day "</td></tr>";
          echo 
    "<tr><td width=\"100\" height=\"100\" valign=\"top\"><br/></td></tr>";
          echo 
    "</table></td>";

    }
        }
        while( (
    $day $offset) <= $rows 7)
        {
            echo 
    "<td></td>";
            
    $day++;
        }
        echo 
    "</tr>\n";
        echo 
    "</table>\n";
    }
    showmonth(122008);
    ?>

  • #4
    Regular Coder
    Join Date
    Feb 2006
    Posts
    262
    Thanks
    23
    Thanked 1 Time in 1 Post
    I foud it thanks you !
    PHP Code:

    if ($month == date("m") && $day == date("d")){
          echo 
    "<td  valign=\"top\"><table border=\"0\" align=\"center\">";
          echo 
    "<tr><td width=\"100\" valign=\"top\" bgcolor=\"999966\">" $day "</td></tr>";
          echo 
    "<tr><td width=\"100\" height=\"100\" valign=\"top\"><br/></td></tr>";
          echo 
    "</table></td>";
          
    }else{
          echo 
    "<td  valign=\"top\"><table border=\"0\" align=\"center\">";
          echo 
    "<tr><td width=\"100\" valign=\"top\" bgcolor=\"cccc99\">" $day "</td></tr>";
          echo 
    "<tr><td width=\"100\" height=\"100\" valign=\"top\"><br/></td></tr>";
          echo 
    "</table></td>";


    Am i doing it right ?

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    This is the same thing, (but yours would work too).

    PHP Code:
    $bg="#cccc99";

    // Calculate Saturday or Sunday here, and change $bg accordingly.

       
    if ($month == date("m") && $day == date("d")){
       
    $bg="#999966";
       }
          echo 
    "
          <td  valign='top'><table border='0' align='center'>
          <tr><td width='100' valign='top' bgcolor='$bg'> $day </td></tr>
          <tr><td width='100' height='100' valign='top'><br/></td></tr>
          </table></td>
          "
    ;

    Last edited by mlseim; 12-02-2008 at 02:29 PM.

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    It'd be more effective, if you execute date() functions and the comparison outside the loop.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    I also wonder why they are trying to re-invent the millions of other PHP calendar scripts.
    Maybe it's a good learning exercise?

  • #8
    Regular Coder
    Join Date
    Feb 2006
    Posts
    262
    Thanks
    23
    Thanked 1 Time in 1 Post
    learning and to have your own...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •