Jump to content

PHP XML XSLT Calendar


kvnmck18

Recommended Posts

I did this today, it's in php4 but easily changed to 5(ps - tell me what you think...or if you think of anyways to shorten it)the php (4):

<?php								/*CREATED BY KEVIN MACK - MACK DESIGNS AUG 2007*/		$THISdateYYMMDD = date("ymD");		$year2 = date('y');		$year4 = date('Y');		$month1 = date('n');		$month2 = date('m');		$monthWORD = date('F');		$day1 = date('j');		$day2 = date('d');		$dayWORD = date('l');		$daysInMonth = date("t",mktime(0,0,0,$month1,1,$year4));		$firstDay = date("w", mktime(0,0,0,$month1,1,$year4));		$tempDays = $firstDay + $daysInMonth;		$weeksInMonth = ceil($tempDays/7);		$xmlFile = "x.xml";		$xsltFile = "s.xsl";				$args = array("pageName" => $_GET['pageName'],  "date" => $date, "week" => $week, "week1" => $week1, "week2" => $week2, "week3" => $week3, "week4" => $week4, "week5" => $week5, "THISdateYYMMDD" => $THISdateYYMMDD, "year2" => $year2, "year4" => $year4, "month1" => $month1, "month2" => $month2, "monthWORD" => $monthWORD, "day1" => $day1, "day2" => $day2, "dayWORD" => $dayWORD, "daysInMonth" => $daysInMonth, "firstDay" => $firstDay, "tempDays" => $tempDays, "weeksInMonth" => $weeksInMonth );				$engine = xslt_create();				$output = xslt_process($engine, $xmlFile, $xsltFile, NULL, NULL, $args);				print $output;		xslt_free($engine);?>

xslt:

<?xml version="1.0" encoding="iso-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="iso-8859-1"/><xsl:param name="THISdateYYMMDD" /> <xsl:param name="year2" /><xsl:param name="year4" /> <xsl:param name="month1" /> <xsl:param name="month2" /> <xsl:param name="monthWORD" /> <xsl:param name="day1" /> <xsl:param name="day2" /><xsl:param name="dayWORD" /> <xsl:param name="daysInMonth" /> <xsl:param name="firstDay" /> <xsl:param name="tempDays" /> <xsl:param name="weeksInMonth" /><xsl:template match="/">	<xsl:apply-templates /></xsl:template><xsl:template match="/">	<br />	<xsl:call-template name="calender"/></xsl:template><xsl:template name="calender">	<h1>		<xsl:choose>			<xsl:when test="$month1 = '1'">				January			</xsl:when>			<xsl:when test="$month1 = '2'">				February			</xsl:when>			<xsl:when test="$month1 = '3'">				March			</xsl:when>			<xsl:when test="$month1 = '4'">				April			</xsl:when>			<xsl:when test="$month1 = '5'">				May			</xsl:when>			<xsl:when test="$month1 = '6'">				June			</xsl:when>			<xsl:when test="$month1 = '7'">				July			</xsl:when>			<xsl:when test="$month1 = '8'">				August			</xsl:when>			<xsl:when test="$month1 = '9'">				September			</xsl:when>			<xsl:when test="$month1 = '10'">				October			</xsl:when>			<xsl:when test="$month1 = '11'">				November			</xsl:when>			<xsl:when test="$month1 = '12'">				December			</xsl:when>		</xsl:choose>		<xsl:value-of select="$year4"/> 	</h1>	<table>		<tr>			<th>				Sunday			</th>			<th>				Monday			</th>			<th>				Tuesday			</th>			<th>				Wednesday			</th>			<th>				Thursday			</th>			<th>				Friday			</th>			<th>				Saturday			</th>		</tr>		<xsl:if test="$firstDay = '1'">			<td>			</td>		</xsl:if>		<xsl:if test="$firstDay = '2'">			<td>			</td>			<td>			</td>		</xsl:if>		<xsl:if test="$firstDay = '3'">			<td>			</td>			<td>			</td>			<td>			</td>		</xsl:if>		<xsl:if test="$firstDay = '4'">			<td>			</td>			<td>			</td>			<td>			</td>			<td>			</td>		</xsl:if>		<xsl:if test="$firstDay = '5'">			<td>			</td>			<td>			</td>			<td>			</td>			<td>			</td>			<td>			</td>		</xsl:if>		<xsl:if test="$firstDay = '6'">			<td>			</td>			<td>			</td>			<td>			</td>			<td>			</td>			<td>			</td>			<td>			</td>		</xsl:if>		<xsl:for-each select="dates/date[@num <= $daysInMonth]">					<xsl:variable name="sunday1" select="((7)-$firstDay)"/> 			<xsl:variable name="sunday2" select="((7)-$firstDay)+7"/>			<xsl:variable name="sunday3" select="((7)-$firstDay)+14"/>			<xsl:variable name="sunday4" select="((7)-$firstDay)+21"/>			<xsl:variable name="sunday5" select="((7)-$firstDay)+28"/>			<td align="center">				<xsl:value-of select="@num"/>			</td>			<xsl:if test="@num = $sunday1">				<tr/>			</xsl:if>			<xsl:if test="@num = $sunday2">				<tr/>			</xsl:if>			<xsl:if test="@num = $sunday3">				<tr/>			</xsl:if>			<xsl:if test="@num = $sunday4">				<tr/>			</xsl:if>			<xsl:if test="@num = $sunday5">				<tr/>			</xsl:if>				</xsl:for-each>	</table></xsl:template></xsl:stylesheet>

XML:

<?xml version="1.0" encoding="iso-8859-1"?><dates>	<date num="1"/>	<date num="2"/>	<date num="3"/>	<date num="4"/>	<date num="5"/>	<date num="6"/>	<date num="7"/>	<date num="8"/>	<date num="9"/>	<date num="10"/>	<date num="11"/>	<date num="12"/>	<date num="13"/>	<date num="14"/>	<date num="15"/>	<date num="16"/>	<date num="17"/>	<date num="18"/>	<date num="19"/>	<date num="20"/>	<date num="21"/>	<date num="22"/>	<date num="23"/>	<date num="24"/>	<date num="25"/>	<date num="26"/>	<date num="27"/>	<date num="28"/>	<date num="29"/>	<date num="30"/>	<date num="31"/></dates>

Link to comment
Share on other sites

Errr.... exactly what is the idea?What do you mark up in the XML and what do you get in the end with the PHP/XSLT duo? Any sample use case?[edit]OK. After actually trying it out, I see it's a calendar of the current month in the form of a table. But I still don't see... the big deal. I mean, it looks pretty, but nothing more.[/edit]BTW, this is the PHP5 code:

<?php								/*CREATED BY KEVIN MACK - MACK DESIGNS AUG 2007*/		$THISdateYYMMDD = date("ymD");		$year2 = date('y');		$year4 = date('Y');		$month1 = date('n');		$month2 = date('m');		$monthWORD = date('F');		$day1 = date('j');		$day2 = date('d');		$dayWORD = date('l');		$daysInMonth = date("t",mktime(0,0,0,$month1,1,$year4));		$firstDay = date("w", mktime(0,0,0,$month1,1,$year4));		$tempDays = $firstDay + $daysInMonth;		$weeksInMonth = ceil($tempDays/7);		$xmlFile = "x.xml";		$xsltFile = "s.xsl";				$args = array("pageName" => $_GET['pageName'],  "date" => $date, "week" => $week, "week1" => $week1, "week2" => $week2, "week3" => $week3, "week4" => $week4, "week5" => $week5, "THISdateYYMMDD" => $THISdateYYMMDD, "year2" => $year2, "year4" => $year4, "month1" => $month1, "month2" => $month2, "monthWORD" => $monthWORD, "day1" => $day1, "day2" => $day2, "dayWORD" => $dayWORD, "daysInMonth" => $daysInMonth, "firstDay" => $firstDay, "tempDays" => $tempDays, "weeksInMonth" => $weeksInMonth );		//This is the only stuff that's really changed		$engine = new XSLTProcessor;		$engine->importStylesheet(DOMDocument::load($xsltFile));		$engine->setParameter(null, $args);	 		$output = $engine->transformToXML(DOMDocument::load($xmlFile));				echo $output;?>

Link to comment
Share on other sites

The idea is dates/date are static...they never change. But you can add the XML and create other parent nodes and child nodes that can be inserted into dates. I created it for a form that a client needed to reserve dates. People get on his site and request his service on a certain date that date is then email and added to a database where he can check it. Then on the calendar it removes (or marks) the date that it is reserved.This is just the "skeleton" of the calendar, the rest is up to your own ideas. Really all you need to do is add another call-template inserted in the td's... and wha-la: you have a nice date book. Each date can be modified to add the 24hours in a day, if you really wanted to make it complicated...similiar to the calendar in outlook or etc.Really good for a blog, if you use XML for the data.... you can create date tags for the blog entries (for ex: 070821 for Aug 21, 2007) and line it up with the calendar (by creating id's with the YYMMDD format also).Also it doesn't need to be in a table setup, that was just an added "bonus". It's also good for drop down menus of dates, etc.

Link to comment
Share on other sites

		$selMonth = $_GET['selMonth'];		$currentMonthDate = date('n');		if ($selMonth == NULL) {		  $month1 = date('n');		}		elseif ($currentMonthDate == $selMonth) {		  $month1 = date('n');		}		else {		  $month1 = $selMonth;		}

This is if you want to be able to change month to month just make ?selMonth=9 for Sept and so on and so on

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...