frostdragon Posted March 5, 2014 Share Posted March 5, 2014 Hello all, I am working on a event calendar i am taking a basic event calendar and put it into a class so i can make it event's that last for more then just one day each but i got it all working except for i can create 1 event and works fine but if it's more then 1 event it only shows the last one. thank you in advanced. <?php putenv("TZ=America/Chicago"); $time_dif = 0; date_default_timezone_set('America/Chicago'); class Calendar{ private $events = array(); private $start, $end; public function __construct($month = NULL, $year = NULL){ $this->monthNames = array("January","Febuary","March","April","May","June","July","August","September","October","November","December"); $this->weekNames = array("S","M","T","W","T","F","S"); $this->cMonth = $month; $this->cYear = $year; $this->prev_year = $this->cYear; $this->next_year = $this->cYear; $this->prev_month = $this->cMonth-1; $this->next_month = $this->cMonth+1; if($this->prev_month == 0){ $this->prev_month = 12; $this->prev_year = $this->cYear - 1; } if($this->next_month == 13){ $this->next_month = 1; $this->next_year = $this->cYear + 1; } $this->currentmonth = date('n'); $this->currentyear = date('Y'); $this->date = getdate(mktime(0,0,0,$this->cMonth,1,$this->cYear)); $this->timestamp = mktime(0,0,0,$this->cMonth,1,$this->cYear); $this->maxday = date("t", $this->timestamp); $this->thismonth = getdate($this->timestamp); $this->startday = $this->thismonth['wday']; $this->startday2 = date('N', mktime(0,0,0,$this->currentmonth,1,$this->currentyear)) - 0; $this->today = date("j"); } public function addEvent(DateTime $eventStart, DateTime $eventEnd, $description = null){ if($description === NULL){ end($this->events); $description = "Event " . (key($this->events) === null ? 0 : key($this->events) + 1); } $this->events[] = array('start' => $eventStart->format('Y-m-d'), 'end' => $eventEnd->format('Y-m-d'), 'description' => $description); } public function printCalendar(){ $output = '<table width="196" border="1" cellpadding="0" cellspacing="0">'; $output .= ' <tr>'; $output .= ' <td colspan="7">'; $output .= ' <table width="100%" border="0" cellpadding="0" cellspacing="0">'; $output .= ' <tr>'; $output .= ' <td align="center" width="10%">«</td>'; $output .= " <td align='center' width='80%'><strong>{$this->monthNames[$this->cMonth - 1]} {$this->cYear}</strong></td>"; $output .= ' <td align="center" width="10%">»</td>'; $output .= ' </tr>'; $output .= ' </table>'; $output .= ' </td>'; $output .= ' </tr>'; $output .= ' <tr>'; foreach($this->weekNames as $v){ $output .= " <td align='center' width='28' class='txt' background='#999999'><strong>$v</strong></td>"; } $output .= ' </tr>'; for($i=0; $i<($this->maxday+$this->startday); $i++){ if($this->cMonth < 10){ $month_format = "0$this->cMonth"; }else{ $month_format = $this->cMonth; } $cali = ($i - $this->startday + 1); if($cali < 10){ $day_format = "0".$cali.""; }else{ $day_format = $cali; } if(($i % 7) == 0) $output .= "</tr>\n"; $ev = ''; foreach($this->events as $event){ $date = $this->cYear."-".$this->cMonth."-".$cali; if($event['start'] <= $date && $event['end'] >= $date){ $ev = ''; $ev .= "<td align='center' bgcolor='#00FF00' title='".$event['description']."'>$cali</td>"; }elseif($cali == $this->today && $this->currentmonth == $this->cMonth && $this->currentyear == $this->cYear){ $ev = ''; $ev .= "<td align='center' bgcolor='red' title='Today'>$cali</td>"; }elseif($i <$this->startday){ $ev = ''; $ev .= "<td> </td>"; }else{ $ev = ''; $ev .= "<td align='center' bgcolor='white'>$cali</td>"; } } $output .= $ev; if(($i % 7) == 6) $output .= "</tr>\n"; } $output .= '</table>'; unset($calendar); return $output; } } $cal = new Calendar('03', '2014'); $cal->addEvent(new DateTime('2014-03-12'), new DateTime('2014-03-15'), 'Testing 1'); $cal->addEvent(new DateTime('2014-03-17'), new DateTime('2014-03-19'), 'Testing 2'); $cal->addEvent(new DateTime('2014-03-20'), new DateTime('2014-03-22'), 'Testing 3'); echo $cal->printCalendar(); ?> Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted March 5, 2014 Solution Share Posted March 5, 2014 (edited) You need to move these conditions if($cali == $this->today && $this->currentmonth == $this->cMonth && $this->currentyear == $this->cYear){ $ev = ''; $ev .= "<td align='center' bgcolor='red' title='Today'>$cali</td>"; }elseif($i <$this->startday){ $ev = ''; $ev .= "<td> </td>"; }else{ $ev = ''; $ev .= "<td align='center' bgcolor='white'>$cali</td>"; } outside of the foreach loop so it is $date = $this->cYear."-".$this->cMonth."-".$cali; $ev = ''; foreach($this->events as $event){ if($event['start'] <= $date && $event['end'] >= $date){ $ev = "<td align='center' bgcolor='#00FF00' title='".$event['description']."'>$cali</td>"; } } if(empty($ev)) { if($cali == $this->today && $this->currentmonth == $this->cMonth && $this->currentyear == $this->cYear){ $ev = "<td align='center' bgcolor='red' title='Today'>$cali</td>"; }elseif($i <$this->startday){ $ev = "<td> </td>"; }else{ $ev = "<td align='center' bgcolor='white'>$cali</td>"; } } $output .= $ev; Edited March 5, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
frostdragon Posted March 5, 2014 Author Share Posted March 5, 2014 Thank you Quote Link to comment Share on other sites More sharing options...
frostdragon Posted March 5, 2014 Author Share Posted March 5, 2014 one last question how can i add a blank td at the end of the the calendar instead of it ending the table after the last day. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted March 5, 2014 Share Posted March 5, 2014 You need to calculate how many days are leftover. Change for($i=0; $i<($this->maxday+$this->startday); $i++){ to $totalDays = $this->maxday+$this->startday; // total days to iterate over $leftOverDays = ceil($totalDays / 7) * 7 - $totalDays; // days left to iterate over for($i=0; $i< $totalDays; $i++) Next change $output .= '</table>'; to // add empty table cells to complete calendar if($leftOverDays > 0) { $output .= str_repeat('<td></td>', $leftOverDays); $output .= '</tr>'; } $output .= '</table>'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.