SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calendar Days Of Week Error

    Hi All, I've grabbed a php calendar script which runs sun to sat, I need it to run mon - sun but cant seem to get the changes right for it to work. Anyone give me some help. Many thanks


    table width="1162" border="1" cellpadding="0" cellspacing="0">
    <tr>
    <td width="265" valign="top"><?php
    if (isset($_GET['day'])){
    $day = $_GET['day'];
    } else {
    $day = date("j");
    }
    if(isset($_GET['month'])){
    $month = $_GET['month'];
    } else {
    $month = date("n");
    }
    if(isset($_GET['year'])){
    $year = $_GET['year'];
    }else{
    $year = date("Y");
    }
    $currentTimeStamp = strtotime( "$day-$month-$year");
    $monthName = date("F", $currentTimeStamp);
    $numDays = date("t", $currentTimeStamp);
    $counter = 0;
    ?>
    <br />
    <table border='1'>
    <tr>
    <td><input style='width:50px;' type='button' value='<'name='previousbutton' onclick ="goLastMonth(<?php echo $month.",".$year?>)"></td>
    <td colspan='5'><?php echo $monthName.", ".$year; ?></td>
    <td><input style='width:50px;' type='button' value='>'name='nextbutton' onclick ="goNextMonth(<?php echo $month.",".$year?>)"></td>
    </tr>
    <tr>
    <td width='25px'>Mon</td>
    <td width='25px'>Tue</td>
    <td width='25px'>Wed</td>
    <td width='25px'>Thu</td>
    <td width='25px'>Fri</td>
    <td width='25px'>Sat</td>
    <td width='25px'>Sun</td>
    </tr>
    <?php
    echo "<tr>";
    for($i = 1; $i < $numDays+1; $i++, $counter++){
    $timeStamp = strtotime("$year-$month-$i");
    if($i == 1) {
    $firstDay = date("w", $timeStamp);
    for($j = 1; $j < $firstDay; $j++, $counter++) {
    echo "<td>&nbsp;</td>";
    }
    }
    if($counter % 7 == 0) {
    echo"</tr><tr>";
    }
    $monthstring = $month;
    $monthlength = strlen($monthstring);
    $daystring = $i;
    $daylength = strlen($daystring);
    if($monthlength <= 1){
    $monthstring = "0".$monthstring;
    }
    if($daylength <=1){
    $daystring = "0".$daystring;
    }
    $todaysDate = date("m/d/Y");
    $dateToCompare = $monthstring. '/' . $daystring. '/' . $year;
    echo "<td align='center' ";
    if ($todaysDate == $dateToCompare){
    echo "class ='today'";
    }
    echo "><a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true'>".$i."</a></td>";
    }
    echo "</tr>";
    ?>
    </table>

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    That's... cluttered. But i suppose it works.
    Basically, the answer is "add 6 to the weekday of the first date of the month, and modulo it by 7".

    See if you can figure out where that goes.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for the tip, Im really stumped, it all works fine until 1st July which is falling on a Monday and not Sunday which it should be. I'm guessing the answer is in
    for($j = 1; $j < $firstDay; $j++, $counter++)
    {
    echo "<td>&nbsp;</td>";
    }

    but nothing works!

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Almost. That loop uses the first day of the month, but it isnt the line that's deciding what weekday the first day of the month is.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fixed that will teach me to modify someones code and not write from scratch

  6. #6
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    @SteveWhea

    The script you supplied would be a lot better wrapped in PHP tags.

    I tried your script locally and the calendar displays correctly starting at Sunday but the missing script did not allow the month to be changed.

    Please supply the source where you downloaded the script.

    Perhaps Google for another script and with a bit of luck may take less time to install than to find the errors in the existing script.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    I downloaded and modified it several years ago on another project, so made a few adjustments on the current version as needed days of week Mon - Sun.
    Heres the full script below can't remember where I downloaded it from if your interested

    <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+"&v=true";
    }
    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+"&v=true";
    }
    </script>
    <style>
    .today{
    background-color: #00ff00;
    }
    .event{
    background-color: #FF8080;
    }
    </style>

    if (isset($_GET['day'])){
    $day = $_GET['day'];
    } else {
    $day = date("j");
    }
    if(isset($_GET['month'])){
    $month = $_GET['month'];
    } else {
    $month = date("n");
    }
    if(isset($_GET['year'])){
    $year = $_GET['year'];
    }else{
    $year = date("Y");
    }
    $currentTimeStamp = strtotime( "$day-$month-$year");
    $monthName = date("F", $currentTimeStamp);
    $numDays = date("t", $currentTimeStamp);
    $counter = 0;
    ?>
    <br />
    <table border='1'>
    <tr>
    <td><input style='width:50px;' type='button' value='<'name='previousbutton' onclick ="goLastMonth(<?php echo $month.",".$year?>)"></td>
    <td colspan='5'><?php echo $monthName.", ".$year; ?></td>
    <td><input style='width:50px;' type='button' value='>'name='nextbutton' onclick ="goNextMonth(<?php echo $month.",".$year?>)"></td>
    </tr>
    <tr>
    <td width='25px'>Mon</td>
    <td width='25px'>Tue</td>
    <td width='25px'>Wed</td>
    <td width='25px'>Thu</td>
    <td width='25px'>Fri</td>
    <td width='25px'>Sat</td>
    <td width='25px'>Sun</td>
    </tr>
    <?php
    echo "<tr>";
    for($i = 1; $i < $numDays+1; $i++, $counter++)
    {
    $timeStamp = strtotime("$year-$month-$i");
    if($i == 1)
    {
    $firstDay = date("w", $timeStamp);


    if($firstDay==0)
    {
    for($j = 1; $j < 7; $j++, $counter++)
    {
    echo "<td>&nbsp;</td>";
    }
    }
    else
    {
    for($j = 1; $j < $firstDay; $j++, $counter++)
    {
    echo "<td>&nbsp;</td>";
    }
    }
    }

    if($counter % 7 == 0)
    {
    echo"</tr><tr>";
    }

    $monthstring = $month;
    $monthlength = strlen($monthstring);
    $daystring = $i;
    $daylength = strlen($daystring);
    if($monthlength <= 1){
    $monthstring = "0".$monthstring;
    }
    if($daylength <=1){
    $daystring = "0".$daystring;
    }
    $todaysDate = date("m/d/Y");
    $dateToCompare = $monthstring. '/' . $daystring. '/' . $year;
    echo "<td align='center' ";
    if ($todaysDate == $dateToCompare){
    echo "class ='today'";
    }
    echo "><a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true'>".$i."</a></td>";
    }
    echo "</tr>";
    ?>
    </table>

  8. #8
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    Many thanks for the script, I will try it tomorrow.

    Please take the time to learn how to wrap your script in code tags - left square bracket php right square bracket.

    I use CodeIgniter which has a very flexible Calendar library, so much easier to implement and adjust.

    http://codeigniter.com/user_guide/li.../calendar.html


    PHP Code:

    $this
    ->load->library('calendar');

     
    $data = array
    (
     
    3  => 'http://example.com/news/article/2006/03/',
     
    7  => 'http://example.com/news/article/2006/07/',
     
    13 => 'http://example.com/news/article/2006/13/',
     
    26 => 'http://example.com/news/article/2006/26/'
    );

     echo 
    $this->calendar->generate(20066$data); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •