Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
PHP/JS script that renders a calendar, off by one year.
Old 10-20-2006, 06:58 PM PHP/JS script that renders a calendar, off by one year.
Junior Talker

Posts: 3
Name: Dan
Trades: 0
This code I am using is meant to render a calender for this month, but it does it for October 2005. The calendar says "October 2006" at the top, but the way the dates line up with the days of week is wrong. For example, today is October 20th, a friday, not a thursday, as the calendar says.

I really hate to do something like this as a first post, but it is a little urgent. If needed may be able to offer some graphics help to anyone who helps me with this. I am a graphics guy. A thankyou for any help offered.


Anyway, here's the code:



Code:
<html>

<?php

  if($_GET[pass] == "threeday"):
    if($_GET[year]):
      $thisyear = $_GET[year];
    else:
      $thisyear = date("Y");
    endif;
    $function = "threeDay";
    $vari = ",'" . $thisyear . "'";
  elseif($_GET[sd]):
    $function = "eDate";
    $sd = $_GET[sd];
    $smon = $_GET[smon];
    $vari = ",'" . $sd . "','" . $smon . "'";
  else:
    if($_GET[year]):
      $thisyear = $_GET[year];
    else:
      $thisyear = date("Y");
    endif;
    $function = "sDate";
    $vari = ",'" . $thisyear . "'";
  endif;

?>

<head>

  <script language="javascript">
    function sDate(start,month,year)
      {
        numpass = "<?php echo $_GET[number] ?>"
        if(numpass > "")
          {
           location.href = "calendar.php?pass=multiday&number=" + numpass + "&sd=" + start + "&smon=" + month + "&month=" + month + "&year=" + year
          }
        else
          {
           opener.document.location.href = "../index.php?pass=multiday&num="
           self.close()
          }
      }  

    function eDate(end,month,start,smonth)
      {
        numpass = "<?php echo $_GET[number] ?>"
        if(numpass > "")
          {
           opener.document.location.href = "multipass.php?pass=multiday&number=" + numpass + "&ed=" + end + "&emon=" + month + "&sd=" + start + "&smon=" + smonth
           self.close()
          }
        else
          {
           opener.document.location.href = "../index.php?pass=multiday&num="
           self.close()
          }
      }
      
    function threeDay(start,month,year)
      {
        numpass = "<?php echo $_GET[number] ?>"
        if(numpass > "")
          {
            opener.document.location.href = "threedaypass.php?number=" + numpass + "&start=" + start + "&smon=" + month + "&year=" + year
            self.close()
          }
      }
  </script>
  
  <title>Multi-Day Pass Calendar</title>
  
  <style>
    a  {text-decoration: none;
        font-weight: bold;
        color: blue;
       }
    td {text-align: right;
       }
  </style>
  
</head>

<body style="text-align: center; margin: 0px; margin-top: 10px; font-family: arial;
             background-color: #003399;">
  <span style="color: #FFFF99; font-size: 14pt; line-height: 0.8;">
  
  <?php
  
    if($_GET[sd]):
      echo "Click the <span style=\"color: red;\">last</span> valid day of the pass.";
    else:
      echo "Click the <span style=\"color: lightgreen;\">first</span> valid day of the pass.";
    endif;
  ?>
    
    <br />
  </span>
  <br />

<?php

if($_GET[month]):
  $thisday = date("l", mktime(0,0,0,$_GET[month],1,$_GET[year]));
  if($thisday == 'Sunday'):
    $thisday = '1';
  elseif($thisday == 'Monday'):
    $thisday = '2';
  elseif($thisday == 'Tuesday'):
    $thisday = '3';
  elseif($thisday == 'Wednesday'):
    $thisday = '4';
  elseif($thisday == 'Thursday'):
    $thisday = '5';
  elseif($thisday == 'Friday'):
    $thisday = '6';
  elseif($thisday == 'Saturday'):
    $thisday = '7';
  endif;
  
  $today = date("j", mktime(0,0,0,$_GET[month],1,$_GET[year]));
  $firstday = "";
  while($firstday == "")
    {
      if($today > '7'):
        $today = $today - 7;
      else:
        $firstday = $thisday - $today + 1;
      endif;
      if($firstday == 0):
        $firstday = 7;
      endif;
    }
  if($firstday < '1'):
    $firstday = 7 + $firstday;
  endif;
else:
  $thisday = date("l");
  if($thisday == 'Sunday'):
    $thisday = '1';
  elseif($thisday == 'Monday'):
    $thisday = '2';
  elseif($thisday == 'Tuesday'):
    $thisday = '3';
  elseif($thisday == 'Wednesday'):
    $thisday = '4';
  elseif($thisday == 'Thursday'):
    $thisday = '5';
  elseif($thisday == 'Friday'):
    $thisday = '6';
  elseif($thisday == 'Saturday'):
    $thisday = '7';
  endif;
  
  $today = date("j");
  $firstday = "";
  while($firstday == "")
    {
      if($today > 7):
        $today = $today - 7;
      else:
        $firstday = $thisday - $today + 1;
      endif;
      if($firstday == 0):
        $firstday = 7;
      endif;
    }
  if($firstday < 1):
    $firstday = 7 + $firstday;
  endif;
endif;
?>

<table style="width: 100%; background-color: #DDDDDD" border="1">

<tr>

  <?php
    
    if($_GET[month]):
      $nextmon = $_GET[month] + 1;
      if($nextmon < 10):
        $nextmon = '0' . $nextmon;
      endif;
      if($nextmon > 12):
        $nextmon = '01';
      endif;
      $lastmon = $_GET[month] - 1;
      if($lastmon < 10):
        $lastmon = '0' . $lastmon;
      endif;
      if($lastmon < '01'):
        $lastmon = '12';
      endif;
    else:
      $nextmon = date("m") + 1;
      if($nextmon > 12):
        $nextmon = '01';
      elseif($nextmon < 10):
        $nextmon = '0' . $nextmon;
      endif;
      $lastmon = date("m") - 1;
      if($lastmon < 1):
        $lastmon = '12';
      elseif($lastmon < 10):
        $lastmon = '0' . $lastmon;
      endif;
    endif;
    
    if($_GET[year]):
      if($_GET[month] == 12):
        $nextyear = $_GET[year] + 1;
      else:
        $nextyear = $_GET[year];
      endif;
      if($_GET[month] == '01'):
        $lastyear = $_GET[year] - 1;
      else:
        $lastyear = $_GET[year];
      endif;
    else:
      if($nextmon == '01'):
        $nextyear = date("Y") + 1;
      else:
        $nextyear = date("Y");
      endif;
      if($lastmon == 12):
        $nextyear = date("Y") - 1;
      else:
        $lastyear = date("Y");
      endif;
    endif;
    
    $thismonth = $nextmon - 1;
    if($thismonth < 10):
      $thismonth = '0' . $thismonth;
    endif;
    if($thismonth == '00'):
      $thismonth = '12';
    endif;
    
    if($_GET[pass] == "threeday"):
      $nextmonth = "calendar.php?pass=threeday&month=" . $nextmon . "&year=" . $nextyear . "&number=" . $_GET[number] . "&sd=" . $_GET[sd] . "&smon=" . $_GET[smon];
      $lastmonth = "calendar.php?pass=threeday&month=" . $lastmon . "&year=" . $lastyear . "&number=" . $_GET[number] . "&sd=" . $_GET[sd] . "&smon=" . $_GET[smon];
    else:
      $nextmonth = "calendar.php?month=" . $nextmon . "&year=" . $nextyear . "&number=" . $_GET[number] . "&sd=" . $_GET[sd] . "&smon=" . $_GET[smon];
      $lastmonth = "calendar.php?month=" . $lastmon . "&year=" . $lastyear . "&number=" . $_GET[number] . "&sd=" . $_GET[sd] . "&smon=" . $_GET[smon];
    endif;
      
  ?>
  
  <td style="text-align: left;">
  <a href="
  
  <?php 
  
  echo $lastmonth ?>
  
  "> &nbsp&nbsp < &nbsp </a>
  </td>
  <td colspan="5" style="text-align: center; font-weight: bold; font-size: 14pt;">
    <?php 
      
      if($_GET[month]):
        if($_GET[month] == '01'):
          echo "January";
        elseif($_GET[month] == '02'):
          echo "February";
        elseif($_GET[month] == '03'):
          echo "March";
        elseif($_GET[month] == '04'):
          echo "April";
        elseif($_GET[month] == '05'):
          echo "May";
        elseif($_GET[month] == '06'):
          echo "June";
        elseif($_GET[month] == '07'):
          echo "July";
        elseif($_GET[month] == '08'):
          echo "August";
        elseif($_GET[month] == '09'):
          echo "September";
        elseif($_GET[month] == '10'):
          echo "October";
        elseif($_GET[month] == '11'):
          echo "November";
        elseif($_GET[month] == '12'):
          echo "December";
        endif;
        echo "&nbsp" . $_GET[year];
      else:
        echo date("F Y"); 
      endif;
    
    ?>
    
  </td>
  <td>
  <a href="
  
  <?php 
  
  echo $nextmonth ?>
  
  "> &nbsp > &nbsp </a>
  
  </td>
</tr>

<tr style="font-weight: bold;">

  <td>
    Sun
  </td>
  <td>
    Mon
  </td>
  <td>
    Tue
  </td>
  <td>
    Wed
  </td>
  <td>
    Thu
  </td>
  <td>
    Fri
  </td>
  <td>
    Sat
  </td>

</tr>

  <tr>
    
    <?php
    
    if($firstday == '2'):
      echo "<td width=\"14%\">&nbsp</td>";
    endif;
    if($firstday == '3'):
      echo "<td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>";
    endif;
    if($firstday == '4'):
      echo "<td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>";
    endif;
    if($firstday == '5'):
      echo "<td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>";
    endif;
    if($firstday == '6'):
      echo "<td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>";
    endif;
    if($firstday == '7'):
      echo "<td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>
            <td width=\"14%\">&nbsp</td>";
    endif;
    
    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('01','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp1&nbsp</a></td>
    
    <?php
    
      if($firstday == 7):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('02','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp2&nbsp</td>
    
    <?php
    
      if($firstday == 6):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('03','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp3&nbsp</td>
    
    <?php
    
      if($firstday == 5):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('04','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp4&nbsp</td>
    
    <?php
    
      if($firstday == 4):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('05','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp5&nbsp</td>
    
    <?php
    
      if($firstday == 3):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('06','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp6&nbsp</td>
    
    <?php
    
      if($firstday == 2):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('07','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp7&nbsp</td>
    
    <?php
    
      if($firstday == 1):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('08','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp8&nbsp</td>
    
    <?php
    
      if($firstday == 7):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('09','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp9&nbsp</td>
    
    <?php
    
      if($firstday == 6):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('10','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp10&nbsp</td>
    
    <?php
    
      if($firstday == 5):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('11','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp11&nbsp</td>
    
    <?php
    
      if($firstday == 4):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('12','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp12&nbsp</td>
    
    <?php
    
      if($firstday == 3):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('13','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp13&nbsp</td>
    
    <?php
    
      if($firstday == 2):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('14','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp14&nbsp</td>
    
    <?php
    
      if($firstday == 1):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('15','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp15&nbsp</td>
    
    <?php
    
      if($firstday == 7):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('16','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp16&nbsp</td>
    
    <?php
    
      if($firstday == 6):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('17','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp17&nbsp</td>
    
    <?php
    
      if($firstday == 5):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('18','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp18&nbsp</td>
    
    <?php
    
      if($firstday == 4):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('19','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp19&nbsp</td>
    
    <?php
    
      if($firstday == 3):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('20','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp20&nbsp</td>
    
    <?php
    
      if($firstday == 2):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('21','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp21&nbsp</td>
    
    <?php
    
      if($firstday == 1):
        echo "</tr>
              <tr>";
    
      endif;

    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('22','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp22&nbsp</td>
    
    <?php
    
      if($firstday == 7):
        echo "</tr>
              <tr>";
      endif;
      
    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('23','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp23&nbsp</td>
    
    <?php
    
      if($firstday == 6):
        echo "</tr>
              <tr>";
      endif;
      
    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('24','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp24&nbsp</td>
    
    <?php
    
      if($firstday == 5):
        echo "</tr>
              <tr>";
      endif;
      
    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('25','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp25&nbsp</td>
    
    <?php
    
      if($firstday == 4):
        echo "</tr>
              <tr>";
      endif;
      
    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('26','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp26&nbsp</td>
    
    <?php
    
      if($firstday == 3):
        echo "</tr>
              <tr>";
      endif;
      
    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('27','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp27&nbsp</td>
    
    <?php
    
      if($firstday == 2):
        echo "</tr>
              <tr>";
      endif;
      
    ?>
    
    <td width="14%"><a href="#" onClick="<?php echo $function ?>('28','<?php echo $thismonth ?>'<?php echo $vari ?>)">&nbsp28&nbsp</td>
    
    <?php
    
      if($firstday == 1):
        echo "</tr>
              <tr>";
      endif;
      
    ?>
    
    <?php
    
    if(date("t", mktime(0,0,0,$_GET[month],1,$_GET[year])) > '28'):
      echo "<td width=\"14%\"><a href=\"#\" onClick=\"" . $function . "('29','" . $thismonth . "'" . $vari .")\">&nbsp29&nbsp</td>";
    endif;
    if(date("t", mktime(0,0,0,$_GET[month],1,$_GET[year])) > '28' AND $firstday == 7):
      echo "</tr>
            <tr>";
    endif;
      
    ?>
    
    <?php
    
    if(date("t", mktime(0,0,0,$_GET[month],1,$_GET[year])) > '29'):
      echo "<td width=\"14%\"><a href=\"#\" onClick=\"" . $function . "('30','" . $thismonth . "'" . $vari .")\">&nbsp30&nbsp</td>";
    endif;
    if(date("t", mktime(0,0,0,$_GET[month],1,$_GET[year])) > '29' AND $firstday == 6):
      echo "</tr>
            <tr>";
    endif;
      
    ?>
    
    <?php
    
    if(date("t", mktime(0,0,0,$_GET[month],1,$_GET[year])) > '30'):
      echo "<td width=\"14%\"><a href=\"#\" onClick=\"" . $function . "('31','" . $thismonth . "'" . $vari .")\">&nbsp31&nbsp</td>";
    endif;
    if(date("t", mktime(0,0,0,$_GET[month],1,$_GET[year])) > '30' AND $firstday == 5):
      echo "</tr>";
    endif;
      
    ?>
    
</table>

</body>
</html>
bunchof6 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-22-2006, 05:12 AM Re: PHP/JS script that renders a calendar, off by one year.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
this
Code:
if($thisday == 'Sunday'):
    $thisday = '1';
is the start of the problem
Day numbers start with 0 for Sunday.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-31-2006, 06:38 PM Re: PHP/JS script that renders a calendar, off by one year.
Junior Talker

Posts: 3
Name: Dan
Trades: 0
Okay, I changed that to what you suggested, but got the same result.

I guess the problem is that I am unable to see where the script gets the data it uses to align the days correctly. Whatever month it is, it starts on Saturday.

Any ideas? Thanks again.
bunchof6 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP/JS script that renders a calendar, off by one year.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25898 seconds with 12 queries