Jump to content

php issue


justinh

Recommended Posts

I'm trying to make my calender able to cycle through the months. But for some reason my function doesn't update the calender.. but it does work.

 

you can see the calender here.http://www.wmptest.com/HuntReserver/calender.php

 

here is the code in question i have:

 

<?php
function NavigateCalender($month){

if (isset($_GET['direction'])){



switch($_GET['direction']){

    case "1":
    
    $month--;

    echo $month;
    break;
    
    case "2":
    
    $month++;
    echo $month;
    

    break;
    }
    } else {
    }
    }
$date = time();
$day = date('d',$date);
$month = date('m',$date);
NavigateCalender($month);
$year = date('y',$date);
$firstday = mktime(0,0,0, $month, 1, $year);
$title = date('F', $firstday);
$dayofweek = date('D', $firstday);


switch($dayofweek){

    case "Sun":
    $blank = 0;
    break;
    case "Mon":
    $blank = 1;
    break;
    case "Tue":
    $blank = 2;
    break;
    case "Wed":
    $blank = 3;
    break;
    case "Thu":
    $blank = 4;
    break;
    case "Fri":
    $blank = 5;
    break;
    case "Sat":
    $blank = 6;
    break;
    }
    $daysinmonth = cal_days_in_month(0, $month, $year);
    echo "<table border=1 width=294>";
    echo "<tr><th colspan=7><a href=\"calender.php?direction=1\"><</a> $month - $year <a href=\"calender.php?direction=2\">></a></th></tr>";
    echo"<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td>
    <td width=42>S</td></tr>";

 

Thanks abunch!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/130878-php-issue/
Share on other sites

i tried your suggestion but it still isnt working. 

 

http://www.wmptest.com/HuntReserver/calender.php

 

 

<?php
function NavigateCalender($month){

if (isset($_GET['direction'])){



switch($_GET['direction']){

    case "1":
    
    $month - 1;

    echo $month;
    break;
    
    case "2":
    
    $month + 1;
    echo $month;
    

    break;
    }
    } else {
    }
    }
$date = time();
$day = date('d',$date);
$month = date('m',$date);
NavigateCalender($month);
$year = date('y',$date);
$firstday = mktime(0,0,0, $month, 1, $year);
$title = date('F', $firstday);
$dayofweek = date('D', $firstday);


switch($dayofweek){

    case "Sun":
    $blank = 0;
    break;
    case "Mon":
    $blank = 1;
    break;
    case "Tue":
    $blank = 2;
    break;
    case "Wed":
    $blank = 3;
    break;
    case "Thu":
    $blank = 4;
    break;
    case "Fri":
    $blank = 5;
    break;
    case "Sat":
    $blank = 6;
    break;
    }
    $daysinmonth = cal_days_in_month(0, $month, $year);
    echo "<table border=1 width=294>";
    echo "<tr><th colspan=7><a href=\"calender.php?direction=1\"><</a> $month - $year <a href=\"calender.php?direction=2\">></a></th></tr>";
    echo"<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td>
    <td width=42>S</td></tr>";

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/130878-php-issue/#findComment-679350
Share on other sites

I took a look at your code. You had a few problems.

 

1) your function NavigateCalender() never returned the new month

2) You need to pass the month that's current being displayed on the URL so the script knows which month to start from

3) the function NavigateCalender() really needs to figure out and return both the month and year.

 

Here's my take on your code (it does seem to work):

<?php
function NavigateCalender($month, $year){
$cur = strtotime($year . '-' . $month . '-01');
if (isset($_GET['direction'])){
	switch($_GET['direction']){
	    case "1":
		    list($year,$month) = explode('-',date('Y-m',strtotime('-1 month',$cur)));
		    break;
	    case "2":
		    list($year,$month) = explode('-',date('Y-m',strtotime('+1 month',$cur)));
		    break;
	    }
}
return(array($month,$year));
}
$date = time();
$day = date('d',$date);
$month = (isset($_GET['month']))?$_GET['month']:date('m',$date);
$year = (isset($_GET['year']))?$_GET['year']:date('Y',$date);
list($month,$year) = NavigateCalender($month,$year);
$firstday = mktime(0,0,0, $month, 1, $year);
$title = date('F', $firstday);
$dayofweek = date('D', $firstday);


switch($dayofweek){

    case "Sun":
    $blank = 0;
    break;
    case "Mon":
    $blank = 1;
    break;
    case "Tue":
    $blank = 2;
    break;
    case "Wed":
    $blank = 3;
    break;
    case "Thu":
    $blank = 4;
    break;
    case "Fri":
    $blank = 5;
    break;
    case "Sat":
    $blank = 6;
    break;
    }
    $daysinmonth = date('t',strtotime($year . '-' . $month . '-01'));
    echo "<table border=1 width=294>";
    echo '<tr><th colspan=7><a href="?direction=1&month=' . $month . '&year=' . $year . '">< </a>' . $month . ' - '. $year . '<a href="?direction=2&month=' . $month . '&year=' . $year . '"> ></a></th></tr>';
    echo"<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td>
    <td width=42>S</td></tr>";
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/130878-php-issue/#findComment-679511
Share on other sites

The function strtotime() takes two parameters. The first is the date/time string to be converted. The second, which most people don't use, is the starting timestamp that the first parameter uses.

 

So that statement gets the time stamp for the 1st day of the year/month that's passed to the function and uses that to go forward or back one month.

 

You can shorten the function a little by doing:

<?php
function NavigateCalender($month, $year){
$cur = strtotime($year . '-' . $month . '-01');
if (isset($_GET['direction'])){
                $diff = ($_GET['direction'] == 1)?'-':'+';
                list($year,$month) = explode('-',date('Y-m',strtotime($diff . '1 month',$cur)));
}
return(array($month,$year));
}
?>

 

Ken

 

 

Link to comment
https://forums.phpfreaks.com/topic/130878-php-issue/#findComment-679951
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.