PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Dec 20, 2014 1:35 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Php Calendar
PostPosted: Tue Feb 07, 2012 5:52 am 
Offline
Forum Commoner

Joined: Fri Nov 04, 2011 9:04 am
Posts: 28
Hi,

I have written a code to generate a calendar in php. But my calendar isn't perfect enough as much as I am expected. It show current month, year but not the current day. I wish highlight the current day.

Here is the code for the calendar:

Syntax: [ Download ] [ Hide ]
<?php
  $monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

        if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
        if (!isset($_REQUEST["year"]))  $_REQUEST["year"]  = date("Y");
       
  $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>
 


How to make the current day being highlighted automatically as time day passed. I appreciate any help offered. Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Php Calendar
PostPosted: Sat Mar 17, 2012 6:53 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Apr 14, 2010 4:45 pm
Posts: 372
Location: UK
Hi,

You can get today's date like this:

Syntax: [ Download ] [ Hide ]
$todays_date = date("Y-m-d");


and to parse it into variables for today's day, month and year you then just need to do this:

Syntax: [ Download ] [ Hide ]
$todays_year = substr($todays_date,0,4);
$todays_month = substr($todays_date,5,2);
$todays_day = substr($todays_date,8,2);


You then just need to add a test somewhere in your script that looks like this:

Syntax: [ Download ] [ Hide ]
if ($todays_year == $year && $todays_month == $month && $todays_day == $day_num) {
    // echo() out your table cell with a custom class
} else {
    // echo() out your table cell without a custom class
}


You'll obviously need to update that code to suit the variable names in your script.

HTH,

M_G


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group