problem with php calendar navigation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iamngk
Forum Commoner
Posts: 50
Joined: Mon Jun 29, 2009 2:20 am

problem with php calendar navigation

Post 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.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: problem with php calendar navigation

Post 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.
iamngk
Forum Commoner
Posts: 50
Joined: Mon Jun 29, 2009 2:20 am

Re: problem with php calendar navigation

Post 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?
iamngk
Forum Commoner
Posts: 50
Joined: Mon Jun 29, 2009 2:20 am

Re: problem with php calendar navigation

Post by iamngk »

i still not get any solution for my issue. is there any one to look this issue.?
iamngk
Forum Commoner
Posts: 50
Joined: Mon Jun 29, 2009 2:20 am

Re: problem with php calendar navigation

Post 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]
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: problem with php calendar navigation

Post 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.
Post Reply