Hi Guys, I would like to highlight the current day on this php script, can you help me figure out a simple way to do this?

<?php

$monthNames = Array("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");

if (!isset($_GET["m"])) $_GET["m"] = date("n");
if (!isset($_GET["y"])) $_GET["y"] = date("Y");

$currentMonth = $_GET["m"];
$currentYear = $_GET["y"];

$p_year = $currentYear;
$n_year = $currentYear;
$p_month = $currentMonth-1;
$n_month = $currentMonth+1;

if ($p_month == 0 ) {
    $p_month = 12;
    $p_year = $currentYear - 1;
}

if ($n_month == 13 ) {
    $n_month = 1;
    $n_year = $currentYear + 1;
}
$days=array('1'=>"S",'2'=>"M",'3'=>"T",'4'=>"W",'5'=>"T",'6'=>"F",'7'=>"S");

?>

<table width="100%">
<tr align="center">
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="left">  <a href="<?php echo $_SERVER["PHP_SELF"] . "?m=". $p_month . "&y=" . $p_year; ?>" style="color:#FFFFFF">Prev</a></td>
<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?m=". $n_month . "&y=" . $n_year; ?>" style="color:#FFFFFF"> Next</a></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%"  border="0" cellpadding="1" cellspacing="1">
<tr align="center">
<td colspan="7"><B><?php echo $monthNames[$currentMonth-1].' '.$currentYear; ?></B></td>
</tr>
<tr >
<?php for($i=1;$i<=7;$i++){ ?>

<td align="center"><B><?php echo $days[$i]; ?></B></td>

<?php } ?>
</tr>
<?php
$timestamp = mktime(0,0,0,$currentMonth,1,$currentYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
for ($i=0; $i<($maxday+$startday); $i++) {
    if(($i % 7) == 0 ) echo "<tr>";
    if($i < $startday) echo "<td ></td>";
    else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>";
    if(($i % 7) == 6 ) echo "</tr>";
}
?>
</table>
</td>
</tr>
</table
Member Avatar for diafol

My take...

<style>
    table tr td{
        text-align: center;
        height: 20px;
        vertical-align: middle;
    }
    .today{
        background-color: #cccccc;
        color: #000000;
    }
</style>

$thisMonth = date('n');
$thisDay = date('j');
$output = '';

for ($i=0; $i<($maxday+$startday); $i++) {

    $displayDay = $i - $startday + 1;
    $today = ($thisMonth == $currentMonth && $thisDay == $displayDay) ? " class='today'" : '';

    if(($i % 7) == 0 ) $output .= "<tr>";

    $output .= ($i < $startday) ? "<td > </td>" : "<td$today>$displayDay</td>";

    if(($i % 7) == 6 ) $output .= "</tr>";
}

echo $output;

Just a word of advice - your html is full of inline attributes/properties - consider tidying this up by using CSS - you'll find it much easier to maintain. In addition, you could perk it up a little as well, for example...

td:hover{
        background-color: #ff0000;
}

Also you have nested tables, which probably isn't appropriate here - use <th> tags for headings, preferrably splitting your table with <thead> and <tbody>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.