Re: SOLVED: Calendar Date Help

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

 



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')));
}


Just in case:
The solution was right in front of my all along. When setting the calendar object I was giving the class everything it needed except the {year} value. I _ass_-umed it was being set auto-magically; possibly by fairies or tree elves. Turns out I needed to set that parameter. Imagine that... :)
------
$cal->setCal(0,0,0,date('n',$now),1);
------
function setCal($h=0,$m=0,$s=0,$offset,$dayVal=1){
   $stamp = date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y')));
                                                       ^^^^^^^^
------

Solution:
setCal function from Calendar Class:
function setCal($h=0,$m=0,$s=0,$offset,$dayVal=1,$yrVal)

current: $cal->setCal(0,0,0,date('n',time()),1,date('Y',time()));

$prev value coming from session cookie
$now = $session['prev'];
prev: $cal->setCal(0,0,0,date('n',$now),1,date('Y',$now));

$next value coming from session cookie
$now = $session['next'];
next: $cal->setCal(0,0,0,date('n',$now),1,date('Y',$now));

It all works very nicely now.

Thank you Robert for the push to seriously scrutinize my code.

--
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)

--
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