Re: In case this helps... (Calendar Date Help)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Out of curiosity.. have you thought about reworking your premise? Using
prev, current, and next is extremely sloppy IMHO. Additionally, it
prevents bookmarking a specific calendar date. Why don't you have a date
parameter that indicates the year and month? If no such field exists
then you fallback to current date. Then the situation becomes trivial
and doesn't require dependence on the session. Having looked at your
code it strikes me as very poorly thought out which makes it difficult
to ascertain your current problem. At any rate, it's obviously more
complex than is necessary.

<?php

$date = isset( $_GET['date'] ) ? $_GET['date'] : date( 'Y-m' );
list( $year, $month ) = explode( '-', $date );
$timestampCurr = mktime( 0, 0, 0, $month, 1, $year );

$timestampPrev = strtotime( '-1 month', date( 'Y-m-d', $timestamp ) );
$timestampNext = strtotime( '+1 month', date( 'Y-m-d', $timestamp ) );

echo '<a href="?date='.date( 'Y-m', $timestampPrev ).'">Prev</a><br />';
echo '<a href="?date='.date( 'Y-m', $timestampCurr ).'">Curr</a><br />';
echo '<a href="?date='.date( 'Y-m', $timestampNext ).'">Next</a><br />';

?>

I'm sure you could wire the above into your Calendar class easy enough.
FYI it's untested :)

Cheers,
Rob.



On Wed, 2008-05-28 at 16:18 -0400, Mark Weaver wrote:
> Mark Weaver wrote:
> > Hi all,
> > 
> > I've put this off as long as possible, however I think I've reached an 
> > impasse.
> > 
> > I've got an application that I've been writing. One of the modules for 
> > this app is an event calendar. I've got the calendar to the place where 
> > it displays the current month as well as previous and future months. The 
> >  place I'm stuck is it will only show months in the past or the future 
> > that are months in the current year.
> > 
> > Basically the method I'm using to move backward and forward is with Unix 
> > timestamps.
> > 
> > 1. When the calendar first loads the "what" is checked for;
> >     // passed in via $_GET
> >     $what == current, prev, or next
> >   a. "current" is the default
> >     $now = time()
> >     $prev = date('n',$now)-1
> >     $next = date('n',$now)+1
> >   b. Timestamp values are then stored in an array and then
> >     sent to client in session cookie which is then accessed
> >     upon each subsequent request to display the event calendar.
> > 
> > My question/boggle is why, when the calendar advances to 
> > December(current year) it will display January, but of the current year. 
> > The same happens in reverse.
> > 
> > Once I reach the end of the year either in the past or future the month 
> > increases or decreases accordingly, but the year doesn't change. Since 
> > the year value isn't changing the month calendar days that are displayed 
> > simply repeat themselves.
> > 
> > I know there's something I'm missing, but I am definitely not seeing 
> > what it is...
> > 
> > /********************** code below ************************/
> > 
> > $cal = new Calendar;
> > $calpos = array();
> >            
> > // check incoming values
> > if ($what === "current"){
> >     $cal->setCal(0,0,0,date('n'),1);
> >     $now = time();
> >     $prev = $cal->getStamp(date('n',$now)-1,1);
> >     $next = $cal->getStamp(date('n',$now)+1,1);
> >     $calpos['curr'] = $now;
> >     $calpos['prev'] = $prev;
> >     $calpos['next'] = $next;
> >     $_SESSION['calendar'] = $calpos;
> > }   
> > elseif($what === "prev"){
> >     $peek = $_SESSION['calendar'];
> >     $now = $peek['prev'];
> >     $cal->setCal(0,0,0,date('n',$now),1);
> >     $prev = $cal->getStamp(date('n',$now)-1,1);
> >     $next = $cal->getStamp(date('n',$now)+1,1);
> >     $calpos['curr'] = $now;
> >     $calpos['prev'] = $prev;
> >     $calpos['next'] = $next;
> >     $_SESSION['calendar'] = $calpos;
> > }
> > elseif($what === "next"){
> >     $peek = $_SESSION['calendar'];
> >     $now = $peek['next'];
> >     $cal->setCal(0,0,0,date('n',$now),1);
> >     $prev = $cal->getStamp(date('n',$now)-1,1);
> >     $next = $cal->getStamp(date('n',$now)+1,1);
> >     $calpos['curr'] = $now;
> >     $calpos['prev'] = $prev;
> >     $calpos['next'] = $next;
> >     $_SESSION['calendar'] = $calpos;
> > }
> > 
> > <================================================================>
> > function setCal($h=0,$m=0,$s=0,$offset,$dayVal=1){       
> >   $stamp = date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y')));
> >   // Using the stamp the various necessary properties are set for the
> >   // object
> > 
> > function getStamp($dateStr,$dayVal=1){
> >   return date('U',mktime(0,0,0, $dateStr,$dayVal,date('Y')));
> > }
> > 
> 
> After scratching my head on this for a few hours I'm still no closer to 
> the solution, but essentially what I need to do in the getStamp function 
> is test the {year} of the stamp that results from the incoming $dateStr 
> against the actual {year} value of the current year's timestamp time().
> 
> In case it helps below is the code being used in it's entirety.
> The first block of code is the Calendar class and the second is the code 
>   for the page which displays the event calendar.
> 
> my appologies if it's all jumbled up.
> 
> /********************* Calendar Class ***********************/
> class Calendar{
> 	
> 	public 
> $month,$weekday,$dayname,$monthname,$numdays,$year,$mday,$dbdate,$today,$fday;
> 	
> 	public function Calendar(){
> 		@$this->month = $month;
> 		@$this->weekday = $weekday;
> 		@$this->dayname = $dayname;
> 		@$this->monthname = $monthname;
> 		@$this->numdays = $numdays;
> 		@$this->year = $year;
> 		@$this->mday = $mday;
> 		@$this->dbdate = $dbdate;
> 		@$this->fday = $fday;
> 	}
> 	
> 	public function setCal($h=0,$m=0,$s=0,$offset,$dayVal=1){
> 		$stamp = date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y')));
> 		
> 		$month = date('m',$stamp);		// month value with leading zero
> 		$mday = date('d',$stamp);		// day value with leading zero
> 		$dbdate = date('Y-m-d',$stamp);	
> 		$dayname = date('l',$stamp);
> 		$monthname = date('F',$stamp);
> 		$numdays = date('t',$stamp);
> 		$year = date('Y',$stamp);
> 		$weekday = date('w',$stamp);
> 		$fday = date('w', mktime(0, 0, 0, date('n',$stamp), 1, date('Y',$stamp)));
> 		
> 		$this->month = $month;
> 		$this->weekday = $weekday;
> 		$this->dayname = $dayname;
> 		$this->monthname = $monthname;
> 		$this->numdays = $numdays;
> 		$this->year = $year;
> 		$this->mday = $mday;
> 		$this->dbdate = $dbdate;
> 		$this->fday = $fday;
> 	}
> 	
> 	public function getTodayNum(){
> 		return date('j',time());
> 	}
> 	
> 	public function getStamp($dateStr,$dayVal=1){
> 		return date('U',mktime(0,0,0, $dateStr,$dayVal,date('Y')));
> 	}
> 		
> 	public function getMonthName(){
> 		return $this->monthname;
> 	}
> 	
> 	public function getTodayName(){
> 		return $this->dayname;
> 	}
> 	
> 	public function getDay(){
> 		return $this->weekday;
> 	}
> 	
> 	public function getMday($stamp){
> 		return date('j',$stamp);
> 	}
> 
> }
> /**********************************************************/
> 
> /********************* Event Calendar Page ****************/
> function display_calendar($what){
> 	include 'config.php';
> 	require_once 'inc/MyDB.inc';
> 	require_once 'inc/forms.inc';
> 	require_once 'inc/Calendar.inc';
> 	@session_start();
> 	
> 	if ( isset($_SESSION['TSUser']) ){
> 		// set userObj from Session
> 		$userObj = $_SESSION['TSUser'];
> 		$cal = new Calendar;
> 		$calpos = array();
> 		
> 		// check incoming values
> 		if ($what === "current"){
> 			$cal->setCal(0,0,0,date('n'),1);
> 			$now = time();
> 			$prev = $cal->getStamp(date('n',$now)-1,1);
> 			$next = $cal->getStamp(date('n',$now)+1,1);
> 			$calpos['curr'] = $now;
> 			$calpos['prev'] = $prev;
> 			$calpos['next'] = $next;
> 			$_SESSION['calendar'] = $calpos;
> 		}	
> 		elseif($what === "prev"){
> 			$peek = $_SESSION['calendar'];
> 			$now = $peek['prev'];
> 			$cal->setCal(0,0,0,date('n',$now),1);
> 			$prev = $cal->getStamp(date('n',$now)-1,1);
> 			$next = $cal->getStamp(date('n',$now)+1,1);
> 			$calpos['curr'] = $now;
> 			$calpos['prev'] = $prev;
> 			$calpos['next'] = $next;
> 			$_SESSION['calendar'] = $calpos;
> 		}
> 		elseif($what === "next"){
> 			$peek = $_SESSION['calendar'];
> 			$now = $peek['next'];
> 			$cal->setCal(0,0,0,date('n',$now),1);
> 			$prev = $cal->getStamp(date('n',$now)-1,1);
> 			$next = $cal->getStamp(date('n',$now)+1,1);
> 			$calpos['curr'] = $now;
> 			$calpos['prev'] = $prev;
> 			$calpos['next'] = $next;
> 			$_SESSION['calendar'] = $calpos;
> 		}		
> 					
> 		$totalweeks = ($cal->numdays/7);
> 		pageHeader("Calendar: {$userObj['user']}","","");
> 		pageContainers();
> 		echo "<BR>\n";
> 			
> 		echo "<table style=\"border-left: 1px solid #000000; border-top: 1px 
> solid #000000;\" border=0 cellpadding=0 cellspacing=0>\n";
> 		echo "<tr><td colspan=7 height=30 align=center><h1>";
> 		echo $cal->monthname . "&nbsp;&nbsp;&nbsp;&nbsp;" . $cal->year;
> 		echo "</h1></td></tr>\n";
> 		$todaysMonthNum = $cal->getTodayNum();
> 			
> 		$back = "<a href=\"index.php?page=show_calendar&cal=prev\">< < Back</a>";
> 		$forward = "<a href=\"index.php?page=show_calendar&cal=next\">Forward 
>  > ></a>";
> 			
> 		echo "<tr align=center><td colspan=2>{$back}</td><td colspan=3><a 
> href=\"index.php?page=show_calendar&cal=current\">Current 
> Month</a></td><td colspan=2>{$forward}</td></tr>\n";
> 		echo "<tr 
> align=center><td>Sunday</td><td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>Thursday</td><td>Friday</td><td>Saturday</td>\n";
> 		$totalweeks = ($cal->numdays/7);
> 		$cday = 1;
> 		$carr = array(array());
> 		$bkgrnd = '';
> 		
> 		for($i=0;$i<$totalweeks;$i++){
> 			echo "<tr>\n";
> 			for ($d=0;$d<7;$d++){
> 				if ( $d<$cal->fday && $i==0){
> 					echo "<td valign=top width=145 height=110 style=\"border-right: 1px 
> solid #000000; border-bottom: 1px solid #000000;\">";
> 					$carr[$i][$d] = "&nbsp;";
> 					echo $carr[$i][$d];
> 					echo "</td>";
> 				}
> 				else{
> 					if ( $cday == $cal->getTodayNum() ){
> 					$bkgrnd = "#cccccc";
> 					}
> 					else{
> 						$bkgrnd = '';
> 					}
> 						echo "<td bgcolor=\"{$bkgrnd}\" valign=top width=145 height=110 
> style=\"border-right: 1px solid #000000; border-bottom: 1px solid 
> #000000;\">";
> 				$carr[$i][$d] = $cday;
> 				$cday++;
> 				echo $carr[$i][$d];
> 				echo "</td>";
> 				}
> 				if($cday > $cal->numdays){
> 					break;
> 				}
> 			}
> 			echo "</tr>\n";
> 			$d = 0;
> 		}
> 		
> 		echo "</table>\n";
> 		pageFooter();
> 	}
> 	else{
> 		noValidSession();
> 	}
> }
> 
> -- 
> 
> Mark
> -------------------------
> the rule of law is good, however the rule of tyrants just plain sucks!
> Real Tax Reform begins with getting rid of the IRS.
> ==============================================
> Powered by CentOS5 (RHEL5)
> 
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux