PHP Calendar: is there one style of calender, dynamic ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

PHP Calendar: is there one style of calender, dynamic ?

Post by simonmlewis »

Didn't know how to 'subject' this.

I've seen countless Calendars online where you see the date of the month is highlighted a color if something is found in the database for it. Or actual words are in the field.

Is there some specific PHP built in calendar you can call upon, and query on with a database?

Maybe I could build my own, but is there one out there that's genius??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP Calendar: is there one style of calender, dynamic ?

Post by Christopher »

(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: PHP Calendar: is there one style of calender, dynamic ?

Post by simonmlewis »

Thanks.
I texted easy php calendar, but the requirements of it, to run tests are so lengthy it's almost not work it.
www.php-calendar.com doesn't look terribly straight forward either.

I was hoping someone would have used one of these, or any others, and given some advice on the best and simplest to use.
I liked the one where you can fit text into the date boxes themselves. I assume it would then query the DB based on the date of that month.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: PHP Calendar: is there one style of calender, dynamic ?

Post by x_mutatis_mutandis_x »

Most of the calendar apps, I'm guessing are front-end heavy. Storing info in DB is straightforward, once you can design your database properly to your needs. I would recommend looking into front-end libraries, built using AJAX, Javascript, JQuery etc. first, so that you don't have to re-invent the wheel.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: PHP Calendar: is there one style of calender, dynamic ?

Post by simonmlewis »

Ok, I've got this working but not with any kind of DB interaction.

I can see where the sectinos of my code would fit in, but it never goes in there.

So how I do enter a DB script to query that date in question??

Code: Select all

<link href="/source/calendar.css" rel="stylesheet" type="text/css" />
<?php
/* draws a calendar */
function draw_calendar($month,$year){

	/* draw table */
	$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

	/* table headings */
	$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

	/* days and weeks vars now ... */
	$running_day = date('w',mktime(0,0,0,$month,1,$year));
	$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
	$days_in_this_week = 1;
	$day_counter = 0;
	$dates_array = array();

	/* row for week one */
	$calendar.= '<tr class="calendar-row">';

	/* print "blank" days until the first of the current week */
	for($x = 0; $x < $running_day; $x++):
		$calendar.= '<td class="calendar-day-np">&nbsp;</td>';
		$days_in_this_week++;
	endfor;

	/* keep going with days.... */
	for($list_day = 1; $list_day <= $days_in_month; $list_day++):
		$calendar.= '<td class="calendar-day">';
					
					/* add in the day number */
			$calendar.= '<div class="day-number">'.$list_day.'</div>';

			/** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/
			$calendar.= str_repeat('<p>&nbsp;</p>',2);
			
		$calendar.= '</td>';
		if($running_day == 6):
			$calendar.= '</tr>';
			if(($day_counter+1) != $days_in_month):
				$calendar.= '<tr class="calendar-row">';
			endif;
			$running_day = -1;
			$days_in_this_week = 0;
		endif;
		$days_in_this_week++; $running_day++; $day_counter++;
	endfor;

	/* finish the rest of the days in the week */
	if($days_in_this_week < 8):
		for($x = 1; $x <= (8 - $days_in_this_week); $x++):
			$calendar.= '<td class="calendar-day-np">&nbsp;</td>';
		endfor;
	endif;

	/* final row */
	$calendar.= '</tr>';

	/* end the table */
	$calendar.= '</table>';
	
	/* all done, return result */
	return $calendar;
}

/* sample usages */
echo '<h2>April 2012</h2>';
echo draw_calendar(4,2012);


?>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: PHP Calendar: is there one style of calender, dynamic ?

Post by simonmlewis »

I don't understand this code.

I see this near the bottom:

Code: Select all

$events = array();
$query = "SELECT entrytime, entrydate AS event_date FROM diary WHERE entrydate LIKE '$year-$month%'";
$result = mysql_query($query) or die('cannot get results!');
while($row = mysql_fetch_assoc($result)) 
{
  $events[ $row['event_date'] ] [] = $row;
}
... but don't see how this is meant to place details on the page. I tried adding $row['entrytime'] into the [] box, and hoped that would place it on the diary.

It doesn't add anything to the diary. Plus, the form does not update the month shows to March - it remains in May.

Help!!!

Code: Select all

<link href="/source/calendar.css" rel="stylesheet" type="text/css" />
<?php
include "dbconn.php";
/* draws a calendar */
function draw_calendar($month,$year,$events = array()){

  /* draw table */
  $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

  /* table headings */
  $headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  $calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

  /* days and weeks vars now ... */
  $running_day = date('w',mktime(0,0,0,$month,1,$year));
  $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
  $days_in_this_week = 1;
  $day_counter = 0;
  $dates_array = array();

  /* row for week one */
  $calendar.= '<tr class="calendar-row">';

  /* print "blank" days until the first of the current week */
  for($x = 0; $x < $running_day; $x++):
    $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
    $days_in_this_week++;
  endfor;

  /* keep going with days.... */
  for($list_day = 1; $list_day <= $days_in_month; $list_day++):
    $calendar.= '<td class="calendar-day"><div style="position:relative;height:100px;">';
      /* add in the day number */
      $calendar.= '<div class="day-number">'.$list_day.'</div>';
      
      $event_day = $year.'-'.$month.'-'.$list_day;
      if(isset($events[$event_day])) {
        foreach($events[$event_day] as $event) {
          $calendar.= '<div class="event">'.$event['title'].'</div>';
        }
      }
      else {
        $calendar.= str_repeat('<p>&nbsp;</p>',2);
      }
    $calendar.= '</div></td>';
    if($running_day == 6):
      $calendar.= '</tr>';
      if(($day_counter+1) != $days_in_month):
        $calendar.= '<tr class="calendar-row">';
      endif;
      $running_day = -1;
      $days_in_this_week = 0;
    endif;
    $days_in_this_week++; $running_day++; $day_counter++;
  endfor;

  /* finish the rest of the days in the week */
  if($days_in_this_week < 8):
    for($x = 1; $x <= (8 - $days_in_this_week); $x++):
      $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
    endfor;
  endif;

  /* final row */
  $calendar.= '</tr>';
  

  /* end the table */
  $calendar.= '</table>';

  /** DEBUG **/
  $calendar = str_replace('</td>','</td>'."\n",$calendar);
  $calendar = str_replace('</tr>','</tr>'."\n",$calendar);
  
  /* all done, return result */
  return $calendar;
}

function random_number() {
  srand(time());
  return (rand() % 7);
}

/* date settings */
$month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
$year = (int)  ($_GET['year'] ? $_GET['year'] : date('Y'));

/* select month control */
$select_month_control = '<select name="month" id="month">';
for($x = 1; $x <= 12; $x++) {
  $select_month_control.= '<option value="'.$x.'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>';

/* select year control */
$year_range = 7;
$select_year_control = '<select name="year" id="year">';
for($x = ($year-floor($year_range/2)); $x <= ($year+floor($year_range/2)); $x++) {
  $select_year_control.= '<option value="'.$x.'"'.($x != $year ? '' : ' selected="selected"').'>'.$x.'</option>';
}
$select_year_control.= '</select>';

/* "next month" control */
$next_month_link = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control">Next Month >></a>';

/* "previous month" control */
$previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control"><<   Previous Month</a>';


/* bringing the controls together */
$controls = '<form method="get">'.$select_month_control.$select_year_control.'&nbsp;<input type="submit" name="submit" value="Go" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$previous_month_link.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$next_month_link.' </form>';

/* get all events for the given month */
$events = array();
$query = "SELECT entrytime, entrydate AS event_date FROM diary WHERE entrydate LIKE '$year-$month%'";
$result = mysql_query($query) or die('cannot get results!');
while($row = mysql_fetch_assoc($result)) 
{
  $events[ $row['event_date'] ] [$row['entrytime']] = $row;
}

echo '<h2 style="float:left; padding-right:30px;">'.date('F',mktime(0,0,0,$month,1,$year)).' '.$year.'</h2>';
echo '<div style="float:left;">'.$controls.'</div>';
echo '<div style="clear:both;"></div>';
echo draw_calendar($month,$year,$events);
echo '<br /><br />';

?>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply