SitePoint Sponsor

User Tag List

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

    PHP Calendar Skipping Feb

    Hi All
    I have been using a PHP calendar on my site since last year, all fine except today I noticed if i click next month it skips feb and goes to march, I moved through till next jan and the same thing
    happend again. Any help would be great as I'm lost as to why its doing it and its causing big problems!!

    Head Code:
    Code:
    <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+"&x=true&locationID=<? echo $locationID;?>&staffID=<? echo $staffID;?>&clientID=<? echo $clientID;?>";
    }
    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+"&x=true&locationID=<? echo $locationID;?>&staffID=<? echo $staffID;?>&clientID=<? echo $clientID;?>";
    }
    //-->
    </script>

    Body Code:
    PHP Code:
    // build up mini calender 
    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;
    ?>  
    </p><table width="500" border="1" align="center" cellpadding="5" cellspacing="0">
            <tr><td width="479">
          <table border='1' align="center" cellpadding="3" cellspacing="0" bordercolor="#000000">
    <tr>
    <td bordercolor="#000000"><input style='width:50px;' type='button' value='<'name='previousbutton' onclick ="goLastMonth(<?php echo $month.",".$year?>)"></td>
    <td colspan='5' span class='grey14'><?php echo $monthName.", ".$year?></span></td>
    <td><input style='width:50px;' type='button' value='>'name='nextbutton' onclick ="goNextMonth(<?php echo $month.",".$year?>)"></td>
    </tr>
    <tr bgcolor='#25B8DA' span class='create_slots_header' align='center'>
    <td width='25px' bgcolor="#F6851F" class="style1">Mon</td>
    <td width='25px' bgcolor="#F6851F">Tue</td>
    <td width='25px' bgcolor="#F6851F">Wed</td>
    <td width='25px' bgcolor="#F6851F">Thu</td>
    <td width='25px' bgcolor="#F6851F">Fri</td>
    <td width='25px' bgcolor="#F6851F">Sat</td>
    <td width='25px' bgcolor="#F6851F">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 bgcolor='#E5E5E5'>&nbsp;</td>";
                                 }
                            }
                            else
                            {
                                for(
    $j 1$j $firstDay$j++, $counter++)
                                 {
                                    echo 
    "<td bgcolor='#E5E5E5'>&nbsp;</td>";
                                 }
                            }
                    }

    if(
    $counter == 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' ";

    $queryDate "$year-$monthstring-$daystring";

    $todaysDateCheck time();
    $todaysDateCheck date('Y-m-d',$todaysDateCheck);

    $query "SELECT recuringID FROM tblServicesGroups WHERE date='$queryDate' AND date>='$todaysDateCheck' AND clientID='$clientID'";
    if(!
    $row mysql_fetch_array($result))
        {
                if (
    $todaysDate == $dateToCompare)
                {
                echo 
    "class ='today'";
        
                }
        echo 
    "><a href='".$_SERVER['PHP_SELF']."?clientID=".$clientID."&locationID=".$locationID."&month=".$monthstring."&day=".$daystring."&year=".$year."&v=true'>".$i."</a></td>";
        }
    else
        {
        echo 
    "class = 'event'";
        
        echo 
    "><a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true'>".$i."</a></td>";
        }


    }
    echo 
    "</tr>";
    ?>
    </table>
    Last edited by cpradio; Jan 30, 2013 at 12:05. Reason: Added code tags

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,808
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Yep, see this thread for a better explanation, but in short when trying to load February 29/30/31, of 2013 or any other year (except for 29 on leap years) you will get March 02, 03 and 04th respectively.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that, any chance anyone could alter the code for me. I'm a total novice and not sure what I need to do.

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,808
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    This is a guess, as I'd really have to spend some time looking at your code more, but I would change this line
    PHP Code:
    $currentTimeStamp strtotime"$day-$month-$year"); 
    To
    PHP Code:
    $currentTimeStamp strtotime"01-$month-$year"); 
    Hard coding 01 for the day removes the problem, and since this looks to only be used for getting the month name and number of days in the month, it shouldn't cause any functional problems.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that mate, really appreciate it. You've saved my ass!!

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, didn't realise $currentTimeStamp = strtotime( "$day-$month-$year");
    particularly the $day is used further on in my code to show the current 7 day week for that time period. Any other workarounds so I can keep the $day part ?

    Steve

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,808
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Can you show me that code? As I don't see that in the existing code you have in this thread...
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $previous_week = $currentTimeStamp - 604800;
    $previous_year = date('Y',$previous_week);
    $previous_month = date('m',$previous_week);
    $previous_day = date('d',$previous_week);

    $next_week = $currentTimeStamp + 604800;
    $next_year = date('Y',$next_week);
    $next_month = date('m',$next_week);
    $next_day = date('d',$next_week);

    $today_week = time();
    $today_year = date('Y',$today_week);
    $today_month = date('m',$today_week);
    $today_day = date('d',$today_week);

    Theres other code, but it uses the $currentTimeStamp as basis for the 7 day week display and option to move to next or previous week

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,808
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Ah, okay.

    So let's take this code
    PHP Code:
    $currentTimeStamp strtotime"01-$month-$year");
    $monthName date("F"$currentTimeStamp);
    $numDays date("t"$currentTimeStamp);
    $counter 0
    And now I'm going to assume the previous* and next* code comes after the above. So long as that is true, you can do the following:
    PHP Code:
    $currentTimeStamp strtotime"01-$month-$year");
    $monthName date("F"$currentTimeStamp);
    $numDays date("t"$currentTimeStamp);
    $currentTimeStamp strtotime"$day-$month-$year"); // added this to reset this value for prev* and next* code
    $counter 0
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats great all seems to be working fine, thanks for all your help!!


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
  •