[PHP] Calendar Date Help
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'))); } -- 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] Calendar Date Help
On Wed, 2008-05-28 at 14:27 -0400, 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'))); ^ ^ ^ You don't want the current year. Fix this to a request year. // 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'))); } ^ ^ ^ Similarly. 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] Calendar Date Help
Robert Cummings wrote: function getStamp($dateStr,$dayVal=1){ return date('U',mktime(0,0,0, $dateStr,$dayVal,date('Y'))); } ^ ^ ^ Similarly. Cheers, Rob. Hi Rob, Changing: function setCal($h=0,$m=0,$s=0,$offset,$dayVal=1){ $stamp = date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y'))); To: function setCal($h=0,$m=0,$s=0,$offset,$dayVal=1){ if (date('Y') date('Y',date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y') { $stamp = date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y')+1)); } else { $stamp = date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y'))); } Makes no change. Strange condition still exists. I was rather hoping that if date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y'))) is smart enough to know to increase or decrease the month value that it would do the same to the year value accordingly. $offset contain either date('n',$timestamp)-1 or date('n',$timestamp)+1 The $timestamp value comes from the array being stored in the $_SESSION['calendar'] as either 'current', 'prev', or 'next'. -- 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