Saturday, 10 January 2015

PHP Basic Calendar start the week with Monday instead of Sunday

This tutorial is just an update to my previous tutorial:
PHP Basic Calendar Tutorial By UnknownGhost03 Fix!

Highlights is the trick to start the week with Monday instead of Sunday.

Source code:
<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'>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>
    <td align="center" width='100px'>Sunday</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) {
       //0=sun, 1=mon, 2=tue, ...
       $firstDay = date("w", $timeStamp);
       
       //if sunday change the firstDay to 7
       if($firstDay == 0)
       $firstDay = 7;

       //decrement firstDay by 1
       $firstDay--;

       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>

Sunday, 30 June 2013

PHP Basic Calendar with Additional Feature Tutorial

This tutorial is just an add on to my previous tutorial:
PHP Basic Calendar Tutorial By UnknownGhost03 Fix!

Replacing the empty cells, I add previous & next month, days to the current month that user are viewing.

Source code:
<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 previous month
     $prevMonth = $month - 1;
     $prevYear = $year;
     if($prevMonth <= 0){
       $prevMonth = 12;
       $prevYear--;
     }
     
     $pMonthStr = "" + $prevMonth;
     if(strlen($pMonthStr) <= 1)
       $pMonthStr = "0" + $pMonthStr;

     //get num of day for previous month
     $previousTimeStamp = strtotime( "01-$pMonthStr-$prevYear");
     $prevNumDays = date("t", $previousTimeStamp);

     //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 previous month day
        $prevDay = $prevNumDays-$firstDay+$j+1;
        echo "<td>$prevDay</td>";
       }
      }
       
      //create new row
      if($counter % 7 == 0) {
       echo"</tr><tr>";
      }
       
      //print day number
      echo "<td>$i</td>";
     }
     
     //fill counter
     $fillCounter = 1;
     //fill up the leftover cells of the table
     while($counter % 7 != 0) {
      //echo next month day
      echo "<td>$fillCounter</td>";
      $fillCounter++;
      $counter++;
     }
    ?>
   </tr>
  </table>
 
 </body>
  
</html>