Re: [PHP] SOLVED: Calendar Date Help
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
Re: [PHP] SOLVED: Calendar Date Help
On Thu, 2008-05-29 at 07:37 -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'))); } 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'))); I don't really want to say I told you so, but that's one of the two lines I denoted as incorrect in my first response ;) 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. It's far more important to learn to build complex code from simple code, than to try and understand complex code and then refactor it to simple code. Just a thought for the future since as it stands you have a very convoluted system of moving back and forth between months. Cheers, Rob. -- 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
Re: [PHP] SOLVED: Calendar Date Help
Robert Cummings wrote: function setCal($h=0,$m=0,$s=0,$offset,$dayVal=1){ $stamp = date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y'))); I don't really want to say I told you so, but that's one of the two lines I denoted as incorrect in my first response ;) :P indeed you did... it just took me a while before it dawned on me. I just wasn't seeing it until the Mule got tired of trying to climb a vertical cliff! Thank you Robert for the push to seriously scrutinize my code. It's far more important to learn to build complex code from simple code, than to try and understand complex code and then refactor it to simple code. Just a thought for the future since as it stands you have a very convoluted system of moving back and forth between months. Cheers, Rob. The particular block of code you're referring to definitely needs to be re factored, however at the moment I don't know enough about php to accomplish that. I think one of the bad habits I got into early on was coding verbosely so I wouldn't have to comment as much. That definitely doesn't serve very well at times. -- 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