Page 1 of 1

problem with php calendar navigation

Posted: Thu Dec 31, 2009 7:07 am
by iamngk
I am designing calendar using PHP. I have problem with navigation designing. I am using following concept to find out next month, previous month, next year, previous year.

Code: Select all

function showNav($year, $month, $day) {
        $next_m = date ( "m", mktime ( 0, 0, 0, $month + 1, $day, $year ) );
        $next_y = date ( "Y", mktime ( 0, 0, 0, $month, $day, $year + 1 ) );
        $prev_m = date ( "m", mktime ( 0, 0, 0, $month - 1, $day, $year ) );
        $prev_y = date ( "Y", mktime ( 0, 0, 0, $month, $day, $year - 1 ) );
……
}
Initially, I am calling the above function with current year, month, and day. After that I am passing values based on navigation.
Basically, I am designing this navigation to have a links to say next month, previous month, next year, previous year. When I click on next month, it should take me the current date of next month which is similar to how windows system calendar works.
I am facing the problem, while accessing the script on month ending dates i.e. 31st. problem is: it get failed to provide the navigation for next immediate month. For example, if I am running on Dec 31, it’s giving Jan -> March -> May …. It failed to give month between that. When I am working in non month end date, it is working perfect.
Please let me know if you find some mistake in my calculation.

Re: problem with php calendar navigation

Posted: Thu Dec 31, 2009 2:45 pm
by manohoo
Try this:

Code: Select all

function showNav($y,$m,$d)
    {
    $res['prev_y'] = date("Y", mktime(0,0,0,$m,$d,$y-1));
    $res['prev_m'] = date("M Y", mktime(0,0,0,$m-1,$d,$y)); 
    $res['next_m'] = date("M Y", mktime(0,0,0,$m+1,$d,$y)); 
    $res['next_y'] = date("Y", mktime(0,0,0,$m,$d,$y+1)); 
 
    return $res;
    }
 
$links = showNav(1994,4,4);
  
$showLinks  = '[<a href="">'.$links['prev_y'].'</a>]';
$showLinks .= '[<a href="">'.$links['prev_m'].'</a>]';
$showLinks .= '[<a href="">'.$links['next_m'].'</a>]';
$showLinks .= '[<a href="">'.$links['next_y'].'</a>]';
 
echo $showLinks;
Will output 4 links:
[1993][Mar 1994][May 1994][1995]

If you want you can have the function display the links directly.

Re: problem with php calendar navigation

Posted: Sun Jan 03, 2010 10:16 pm
by iamngk
manohoo thanks for your response.
Here I am talking about month ending problem.
Have you called your function with following value?
$links = showNav(2010,1,28); this returns [2009][Dec 2009][Feb 2010][2011]. – it is correct.
But,
$links = showNav(2010,1,30); this call returns [2009][Dec 2009][Mar 2010][2011]
$links = showNav(2010,1,31); this call returns [2009][Dec 2009][Mar 2010][2011]
It is wrong.
Have you noticed this?

Re: problem with php calendar navigation

Posted: Sun Jan 31, 2010 11:10 pm
by iamngk
i still not get any solution for my issue. is there any one to look this issue.?

Re: problem with php calendar navigation

Posted: Tue Feb 02, 2010 7:26 am
by iamngk
At final, I have resolved the issue.
Whenever calling navigation function, I am calling for 1st day of month.
I have changed showNav(2010,1,31) to showNav(2010,1,1).
This gives a correct negation [2009][Dec 2009][May 2011][2011]

Re: problem with php calendar navigation

Posted: Fri Feb 12, 2010 1:39 pm
by manohoo
iamngk, sorry for my late response, I just noticed your reply. I corrected and improved the function, since we you are not interested in the day of the month:

Code: Select all

function showNav($y,$m)
    {
    $res['prev_y'] = date("Y", mktime(0,0,0,$m,1,$y-1));
    $res['prev_m'] = date("M Y", mktime(0,0,0,$m-1,1,$y));
    $res['date'] = date("M Y", mktime(0,0,0,$m,1,$y));
    $res['next_m'] = date("M Y", mktime(0,0,0,$m+1,1,$y));
    $res['next_y'] = date("Y", mktime(0,0,0,$m,1,$y+1));
 
    $showLinks  = '[<a href="">'.$res['prev_y'].'</a>]';
    $showLinks .= '[<a href="">'.$res['prev_m'].'</a>]';
    $showLinks .= ' '.$res['date'].' '; //optional
    $showLinks .= '[<a href="">'.$res['next_m'].'</a>]';
    $showLinks .= '[<a href="">'.$res['next_y'].'</a>]';
 
    echo $showLinks;
    return null;
    }
 
showNav(2010,12); // the function displays the result now
 
 
The above will output:
[2009][Nov 2010] Dec 2010 [Jan 2011][2011]

The code can be shortened, but I thought that this way is easier to understand.