Page 1 of 1

Calendar

Posted: Mon Jun 23, 2008 9:58 am
by DJG7777
I have a calendar code, It works fine, but I want to hightlight the current day. I'm new to PHP:<?php
//This gets today's date
$date =time () ;

//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date) ;
$year = date('Y', $date) ;

//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;

//This gets us the month name
$title = date('F', $first_day) ;

//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}

//We then determine how many days are in the current month
$days_in_month = date("t,") ;


//Current day
$current_day = date("j");

if ($day == $current_day) { //is this day the current day
echo "$current_day^\t"; //if so put an indicator
} else {
echo "$current_day\t"; //otherwise just print it
}

//Here we start building the table heads
echo "<table class=cal>";
echo "<tr><th class=calHeader colspan=7> $title $year </th></tr>";
echo "<tr><td class=dayHeader width=42>Sun</td><td class=dayHeader width=42>Mon</td><td class=dayHeader width=42>Tue</td><td class=dayHeader width=42>Wed</td><td class=dayHeader width=42>Thu</td><td class=dayHeader width=42>Fri</td><td class=dayHeader width=42>Sat</td></tr>
";



//This counts the days in the week, up to 7
$day_count = 1;

echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td class=day></td>";
$blank = $blank-1;
$day_count++;
}

//sets the first day of the month to 1
$day_num = 1;

//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td class=day> $day_num </td>";
$day_num++;
$day_count++;

//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
}


//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td class=day> </td>";
$day_count++;
}

echo "</tr></table>";
?>

Re: Calendar

Posted: Mon Jun 23, 2008 10:15 am
by tecktalkcm0391
First rap your code with the tags [ ph p] and [ / ph p] (removing the spaces)... when posting... it makes it a lot easier for us to help you...

Code: Select all

<?php
//This gets today's date
$date =time () ;
 
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date) ;
$year = date('Y', $date) ;
 
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
 
//This gets us the month name
$title = date('F', $first_day) ;
 
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
 
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
 
//We then determine how many days are in the current month
$days_in_month = date("t,") ;
 
 
//Current day
$current_day = date("j");
 
if ($day == $current_day) { //is this day the current day
echo "$current_day^\t"; //if so put an indicator
} else {
echo "$current_day\t"; //otherwise just print it
}
 
//Here we start building the table heads
echo "<table class=cal>";
echo "<tr><th class=calHeader colspan=7> $title $year </th></tr>";
echo "<tr><td class=dayHeader width=42>Sun</td><td class=dayHeader width=42>Mon</td><td class=dayHeader width=42>Tue</td><td class=dayHeader width=42>Wed</td><td class=dayHeader width=42>Thu</td><td class=dayHeader width=42>Fri</td><td class=dayHeader width=42>Sat</td></tr>
";
 
 
 
//This counts the days in the week, up to 7
$day_count = 1;
 
echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td class=day></td>";
$blank = $blank-1;
$day_count++;
}
 
//sets the first day of the month to 1
$day_num = 1;
 
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td class=day> $day_num </td>";
$day_num++;
$day_count++;
 
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
}
 
 
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td class=day> </td>";
$day_count++;
}
 
echo "</tr></table>";
?>

Re: Calendar

Posted: Mon Jun 23, 2008 10:18 am
by tecktalkcm0391
With the lines

Code: Select all

while ( $blank > 0 )
{
echo "<td class=day></td>";
$blank = $blank-1;
$day_count++;
}
All you need to do is add something like this, so you can style the <td> class. Note: I didn't do any styling:

Code: Select all

while ( $blank > 0 )
{
if($blank ==$current_day){ // the current_day is from a line above....
   // echo special <td> you need to edit the class or style
   echo "<td class=day></td>";
} else {
   echo "<td class=day></td>";
}
$blank = $blank-1;
$day_count++;
}

Re: Calendar

Posted: Mon Jun 23, 2008 10:39 am
by DJG7777
I added the code you gave me, but it still doesn't hightlight the day.

Re: Calendar

Posted: Mon Jun 23, 2008 10:52 am
by tecktalkcm0391
DJG7777 wrote:I added the code you gave me, but it still doesn't hightlight the day.
Did you edit the background color of the TD where i noted.

Re: Calendar

Posted: Mon Jun 23, 2008 11:11 am
by DJG7777
Yes, I have a class name "actday" that has the style, see below. Is this where you wanted me to put the <td> style.

Code: Select all

 
while ( $blank > 0 )
{
if($blank ==$current_day){ // the current_day is from a line above....
   // echo special <td> you need to edit the class or style
   echo "<td class=actday></td>";
} else {
   echo "<td class=actday></td>";
}
$blank = $blank-1;
$day_count++;
}

Re: Calendar

Posted: Mon Jun 23, 2008 12:32 pm
by tecktalkcm0391
Well the first one only needs the highlight, the second one is for the regular day... I change it (HTML) in the below code.. look

Code: Select all

 
while ( $blank > 0 )
{
if($blank ==$current_day){ // the current_day is from a line above....
   // echo special <td> you need to edit the class or style
   echo "<td class=actday></td>";
} else {
   //normal day goes here
   echo "<td class=day></td>";
}
$blank = $blank-1;
$day_count++;
}

Re: Calendar

Posted: Mon Jun 23, 2008 12:36 pm
by tecktalkcm0391
wooha... sorry I looked at your coding again and orginally i copied the wrong thing... replace the section:

Code: Select all

 
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td class=day></td>";
$blank = $blank-1;
$day_count++;
}
 
//sets the first day of the month to 1
$day_num = 1;
 
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td class=day> $day_num </td>";
$day_num++;
$day_count++;
 
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
}
 


with this:

Code: Select all

//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td class=day></td>";
$blank = $blank-1;
$day_count++;
}
 
//sets the first day of the month to 1
$day_num = 1;
 
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
if($blank ==$current_day){ // the current_day is from a line above....
      // echo out the current day code
      echo "<td class=actday> $day_num </td>";
} else {
      // echo out the regular day code
      echo "<td class=day> $day_num </td>";
}
 
$day_num++;
$day_count++;
 
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
}
 

Re: Calendar

Posted: Mon Jun 23, 2008 12:54 pm
by DJG7777
It still is not working. Here is the whole html page with styles. Maybe this will help.

Code: Select all

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Untitled Document</title><style type="text/css">/*Calendar*/ table.cal {    width:350px;    border:0px solid #000000;        border-collapse:collapse;}/*Calendar*/ td.dayHeader {    width:50px;    border-collpase:collpase;    border:1px solid #000000;    text-align:right;    padding-right:5px; text-align:center; font-size:9pt}/*Calendar*/td.day {    width:50px; height:50px;    border-collpase:collpase;    border:1px solid #000000;    text-align:right;    padding-right:5px; vertical-align:text-top; font-size:9pt}/*Calendar*/  th.calHeader {    border-collpase:collpase;    border:1px solid #000000;    background-color: #E9ECEF; font-size:12pt}/*Calendar*/ .actday{    background-color:#CCCCCC;    font-weight:bold;    width:50px; height:50px;} </style></head> <body>  <?php//This gets today's date $date =time () ; //This puts the day, month, and year in seperate variables $day = date('d', $date) ; $month = date('m', $date) ; $year = date('Y', $date) ; //Here we generate the first day of the month $first_day = mktime(0,0,0,$month, 1, $year) ;  //This gets us the month name $title = date('F', $first_day) ;  //Here we find out what day of the week the first day of the month falls on $day_of_week = date('D', $first_day) ;  //Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zeroswitch($day_of_week){ case "Sun": $blank = 0; break; case "Mon": $blank = 1; break; case "Tue": $blank = 2; break; case "Wed": $blank = 3; break; case "Thu": $blank = 4; break; case "Fri": $blank = 5; break; case "Sat": $blank = 6; break; } //We then determine how many days are in the current month$days_in_month = date("t,") ;   //Current day$current_day = date("j"); //Here we start building the table heads echo "<table class=cal>";echo "<tr><th class=calHeader colspan=7> $title $year </th></tr>";echo "<tr><td class=dayHeader width=42>Sun</td><td class=dayHeader width=42>Mon</td><td class=dayHeader width=42>Tue</td><td class=dayHeader width=42>Wed</td><td class=dayHeader width=42>Thu</td><td class=dayHeader width=42>Fri</td><td class=dayHeader width=42>Sat</td></tr>";   //This counts the days in the week, up to 7$day_count = 1; echo "<tr>";//first we take care of those blank days//first we take care of those blank dayswhile ( $blank > 0 ){echo "<td class=day></td>";$blank = $blank-1;$day_count++;} //sets the first day of the month to 1$day_num = 1; //count up the days, untill we've done all of them in the monthwhile ( $day_num <= $days_in_month ){if($blank ==$current_day){ // the current_day is from a line above....      // echo out the current day code      echo "<td class=actday> $day_num </td>";} else {      // echo out the regular day code      echo "<td class=day> $day_num </td>";} $day_num++;$day_count++; //Make sure we start a new row every weekif ($day_count > 7){echo "</tr><tr>";$day_count = 1;}}//Finaly we finish out the table with some blank details if neededwhile ( $day_count >1 && $day_count <=7 ) { echo "<td class=day> </td>"; $day_count++; }  echo "</tr></table>";?>   </body></html>

Re: Calendar

Posted: Mon Jun 23, 2008 2:00 pm
by tecktalkcm0391
first in CSS, you can do, and I normally use

Code: Select all

background: #CCCCCC;
instead of

Code: Select all

background-color:#CCCCCC;
Update line

Code: Select all

if($blank == $current_day){ // the current_day is from a line above....
with

Code: Select all

if($day_num == $current_day){ // the current_day is from a line above....

Re: Calendar

Posted: Mon Jun 23, 2008 2:21 pm
by DJG7777
Thanks it worked