Saturday 13 April 2013

PHP Basic Calendar Tutorial By UnknownGhost03 Fix!

<html>

 <head>
  
  <title>PHP Basic Calendar</title>
  
  <script>
   function goLastMonth(month, year){
    if(month == 1) {
     --year;
     month = 13;
    }
    --month
    var monthstring = ""+month+"";
    var monthlength = monthstring.length;
    if(monthlength <=1){
     monthstring = "0" + monthstring;
    }
    document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
   }
   function goNextMonth(month, year){
    if(month == 12) {
     ++year;
     month = 0;
    }
    ++month
    var monthstring = ""+month+"";
    var monthlength = monthstring.length;
    if(monthlength <=1){
     monthstring = "0" + monthstring;
    }
    document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
   }
  </script>
  
 </head>
 
 <body>
 
  <?php
   //get current date or specific month and year
   if (isset($_GET['day'])){
    $day = $_GET['day'];
   } else {
    $day = date("d");
   }
   if(isset($_GET['month'])){
    $month = $_GET['month'];
   } else {
    $month = date("m");
   }
   if(isset($_GET['year'])){
    $year = $_GET['year'];
   }else{
    $year = date("Y");
   }
   
   //get date data for display such as month name
   $currentTimeStamp = strtotime( "01-$month-$year");
   $monthName = date("F", $currentTimeStamp);
  ?>
  
  <table border="1px" cellpadding="0px" cellspacing="0px">
   <tr>
    <td align="center">
     <input style='width:50px;' type='button' value='<'name='previousbutton' onclick ="goLastMonth(<?php echo $month.','.$year?>)">
    </td>
    <td align="center" colspan="5"><?php echo $monthName." ".$year; ?></td>
    <td align="center">
     <input style='width:50px;' type='button' value='>'name='nextbutton' onclick ="goNextMonth(<?php echo $month.','.$year?>)">
    </td>
   </tr>
   <tr>
    <td align="center" width='100px'>Sunday</td>
    <td align="center" width='100px'>Monday</td>
    <td align="center" width='100px'>Tuesday</td>
    <td align="center" width='100px'>Wednesday</td>
    <td align="center" width='100px'>Thursday</td>
    <td align="center" width='100px'>Friday</td>
    <td align="center" width='100px'>Saturday</td>
   </tr>
   <tr>
    <?php
     //get the number of days in the current month and year
     $numDays = date("t", $currentTimeStamp);
     
     //keep track of the number of cell created
     $counter = 0;
     
     for($i = 1; $i < $numDays+1; $i++, $counter++){
      $timeStamp = strtotime("$year-$month-$i");
      
      //create empty cell until first day of the month
      if($i == 1) {
       $firstDay = date("w", $timeStamp);
       for($j = 0; $j < $firstDay; $j++, $counter++) {
        echo "<td>&nbsp;</td>";
       }
      }
      
      //create new row
      if($counter % 7 == 0) {
       echo"</tr><tr>";
      }
      
      //print day number
      echo "<td>$i</td>";
     }
     
     //fill up the leftover cells of the table
     while($counter % 7 != 0) {
      echo "<td>&nbsp;</td>";
      $counter++;
     }
    ?>
   </tr>
  </table>

 </body>
 
</html>

12 comments:

  1. Hi, can you please tell me how to use this? I just pasted it all into a .php and uploaded it to my domain.. will that work?

    ReplyDelete
    Replies
    1. Hi, this source code is basically a fix version of my previous UnknownGhost03 PHP event calendar tutorial:

      http://www.youtube.com/watch?v=xjrVf_kMFl8

      Yes it will work when you pasted it all into a .php and uploaded it to the domain as this is only the Basic Calendar with previous & next button.

      Delete
  2. Do you have any idea how can I change the code such that it will show continuous date? Such as if 1st falls on wed, tue will be 31th, etc..
    and if 31th falls on tue.. wed will be 1st..

    ReplyDelete
    Replies
    1. Hi, I not sure if this is what you want, but here you go:
      http://dinocaspero03.blogspot.sg/2013/06/php-basic-calendar-with-additional.html

      Delete
  3. Good information for me. please keep updating us with new posts. thank you so much .

    ReplyDelete
  4. i learn so many thingsfrom this blog.keep updating.thank you so much.

    ReplyDelete
  5. Hi, how can I add a DELETE event?

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. hi! Muhammad, thanks and congratulations for the code!!
    how i can change monday like first week day?

    ReplyDelete
    Replies
    1. Hi, I know this is very late to reply but here is the trick to change it:
      http://dinocaspero.blogspot.sg/2015/01/php-basic-calendar-start-week-with.html

      Delete
  8. Muhammad, really like this fix. Anyway to get it so it highlights todays date? Also like the old one where you could add events any way of updating this so you can add events?
    Kevin

    ReplyDelete
  9. I am sorry man, but your fix and his give me the same error. These lines are simply written out on page qith just one line of table

    ReplyDelete