1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How To Highlight Date Calendar?

Discussion in 'PHP' started by sangeetha2013, Feb 10, 2013.

  1. #1
    Hello ecxeryone, I have a Php code for calendar.The calender is working fine.but i m not getting how to highlight today's date in that calendar...Please Help me to sort out this problem.Below I uploaded my Code......

    <?php
    $monthNames = Array("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "August", "September", "October", "November", "December");

    if (!isset($_REQUEST["day"])) $_REQUEST["day"] = date("d");
    if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("M");
    if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
    $cdate = $_REQUEST["day"];
    $cMonth = $_REQUEST["month"];
    $cYear = $_REQUEST["year"];
    $prev_year = $cYear;
    $next_year = $cYear;

    $prev_month = $cMonth-1;
    $next_month = $cMonth+1;
    if ($prev_month == 0 )
    {
    $prev_month = 12;
    $prev_year = $cYear - 1;
    }
    if ($next_month == 13 ) {
    $next_month = 1;
    $next_year = $cYear + 1;

    }
    ?>
    <div id="calendar_div" name="calendar_div">
    <table width="200">
    <tr align="center">
    <td bgcolor="#999999" style="color:#FFFFFF">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="50%" align="left">&nbsp;&nbsp;<a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>
    <td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a>&nbsp;&nbsp;</td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td align="center">
    <table width="100%" border="0" cellpadding="2" cellspacing="2">
    <tr align="center">
    <td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong>
    <?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
    </tr>
    <tr>
    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>M</strong></td>
    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>W</strong></td>
    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>F</strong></td>
    <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
    </tr>

    <?php

    $timestamp = mktime(0,0,0,$cMonth,1,$cYear);
    $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 echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>\n";
    if(($i % 7) == 6 ) echo "</tr>\n";
    }

    ?>

    </table>
    </td>
    </tr>
    </table>
    </div>
     
    sangeetha2013, Feb 10, 2013 IP
  2. rchurchill

    rchurchill Greenhorn

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #2
    Easiest way to do this is to do a check .. with an if statement..

    First you need to declare today's date
    Something like this..
    $today = date("d");

    Then using your script you will need to find the date that matches and if it matches then set the cell's back ground to a different color.

    Something similar to this.

    if($today == $i){
    print'<td bgcolor="#999999">';
    }else{
    print'<td bgcolor="#555555">';
    }
    PHP:
    This can be done a few ways. You can set the bgcolor in the <td> tag or you can control it via the css.

     
    rchurchill, Feb 11, 2013 IP
    mokah likes this.
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Just an observation -- that's some horrifyingly bad markup. You've got a colspanned TD doing CAPTION's job, name on a DIV when there's NO reason to ever do that -- DIV doesn't even HAVE a name attribute, it's saying the same values over and over again on elements when that should be in the stylesheet, table nested in the table for christmas only knows what, TD+STRONG doing TH's job, attributes like ALIGN or even worse BGCOLOR that have zero business in any HTML written after 1997...

    Here's a quick rewrite with some better markup:
    <?php
    
    	$todayMonth = date('m');
    	$todayYear = date('Y');
    	
    	$month = isset($_REQUEST['month']) ? $_REQUEST['month'] : $todayMonth;
    	$year = isset($_REQUEST['year']) ? $_REQUEST['year'] : $todayYear;
    	$today = (
    		($month == $todayMonth) && ($year == $todayYear) ?
    		date('d') : 0
    	);
    	
    	$firstOfMonth = mktime(0,0,0,$month,1,$year);
    	$lastDay      = date("t",$firstOfMonth);
    	$thisMonth    = getdate ($firstOfMonth);
    	$startDay  = $thisMonth['wday'];
    	
    	$prevLink = $nextLink = $_SERVER["PHP_SELF"].'?month=';
    	
    	$prevLink .= (
    		$month == 1 ?
    		'12&amp;year='.($year-1) :
    		($month-1).'&amp;year='.$year
    	);
    	
    	$nextLink .= (
    		$month == 12 ?
    		'1&amp;year='.($year+1) :
    		($month+1).'&amp;year='.$year
    	);
    	
    	
    echo '
    	<div class="calendar">
    		<div class="prevNext">
    			<a href="',$prevLink,'" class="prev">Previous</a>
    			<a href="',$nextLink,'">Next</a>
    		<!-- .prevNext --></div>
    	
      <table>
    		<caption>',date("F",$firstOfMonth),' ',$year,'</caption>
    		<thead>
    			<tr>
    				<th scope="col">S</th>
    				<th scope="col">M</th>
    				<th scope="col">T</th>
    				<th scope="col">W</th>
    				<th scope="col">T</th>
    				<th scope="col">F</th>
    				<th scope="col">S</th>
    			</tr>
    		</thead><tbody>
    			<tr>';
    		
    $i=0;
    while ($i < $startDay) {
    	echo '
    				<td></td>';
    	$i++;
    }
    
    for ($day = 1; $day <= $lastDay; $day++) {
    
    	if (($i % 7) == 0) echo '
    			<tr>';
    			
    	echo '
    				<td',(
    					$day == $today ? ' class="today"' : ''
    				),'>',$day,'</td>';
    	
    	if ((++$i % 7) == 0) echo '
    			</tr>';
    			
    }
    
    if (($i % 7) > 0) {
    	while ($i++ % 7 > 0) echo '
    				<td></td>';
    	echo '
    			</tr>';
    }
    		
    echo '
    		</tbody>
    	</table>
    <!-- .calendar --></div>';
    
    ?>
    Code (markup):
    ... and the CSS that should be applied to it:
    .calendar {
    	width:12.5em;
    	color:#FFF;
    	background:#999;
    }
    
    .calendar .prevNext {
    	overflow:hidden; /* wrap floats */
    	zoom:1; /* trip haslayout, wrap floats IE */
    	padding:0.25em 1em;
    	text-align:right;
    }
    
    .calendar .prevNext a {
    	color:#FFF;
    }
    
    .calendar .prevNext .prev {
    	float:left;
    }
    
    .calendar table {
    	width:100%;
    	border-collapse:collapse;
    	empty-cells:show;
    	color:#FFF;
    }
    
    .calendar caption {
    	font-weight:bold;
    	padding:0.25em;
    	background:#888;
    }
    
    .calendar th,
    .calendar td {
    	padding:0.1em;
    	text-align:center;
    	border:1px solid #AAA;
    }
    
    .calendar .today {
    	font-weight:bold;
    }
    Code (markup):
    Should work well.
     
    deathshadow, Feb 13, 2013 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    deathshadow, Feb 13, 2013 IP
  5. sangeetha2013

    sangeetha2013 Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #5
    Ya its working well. Thanku:)..I planned to add above calendar Php file into my wordpress site as Plugin.Its working well ,But I want create Plugin that will change the header logo of theme when my date in the calender changes...Pls help me sort out this. or provide me any link to sort out this problem......
     
    sangeetha2013, Feb 13, 2013 IP