<?php
if ( isset($_GET[month]))
$mon = $_GET[month];
else
$mon = idate("m");
if (isset($_GET[year]))
$year = $_GET[year];
else
$year = idate("Y");
$timestamp = mktime(0 ,0 ,0 ,$mon, 1, $year);
$mon = idate("m", $timestamp);
$year = idate("Y", $timestamp);
setlocale(LC_TIME, 'danish');
// Opretter forbindelse til database
require_once('../Connections/mindevej.php');
mysql_select_db($database);
$thismonth = mktime(0 ,0 ,0 ,$mon, 1, $year);
$nextmonth = mktime(0, 0, 0, $mon+1, 30, $year);
$firstday = "'" . date('Y-m-d',$thismonth) . "'";
$lastday = "'" . date('Y-m-d',$nextmonth) . "'";
$query = "SELECT * FROM kalender WHERE Slut BETWEEN $firstday AND $lastday ORDER BY Start ASC";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="
http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="sel_date">
<select name="month" onchange="sel_date.submit();">
<?php for($i = 1; $i < 13 ; $i++)
{
echo '<option value="'.$i.'" ';
if ($i == $mon )
echo 'selected="selected"';
echo '>'.ucfirst(strftime("%B", mktime(0 ,0 ,0 ,$i, 1, $year)))."</option>\r\n ";
}?>
</select>
<select name="year" onchange="sel_date.submit();">
<?php
for($i = (idate("Y")-1); $i < 2010 ; $i++)
{
echo '<option value="'.$i.'" ';
if ($i == $year)
echo 'selected="selected"';
echo '>'.$i."</option>\r\n";
} ?>
</select>
</form>
Denne måned er: <?php echo ucfirst(strftime("%B", mktime(0 ,0 ,0 ,$mon, 1, $year)))." $year"; ?> <br />
<br />
<table cellpadding="0" cellspacing="0" border="1" >
<tr>
<th>Uge</th>
<th>Mandag</th>
<th>Tirsdag</th>
<th>Onsdag</th>
<th>Torsdag</th>
<th>Fredag</th>
<th>Lørdag</th>
<th>Søndag</th>
</tr>
<tr>
<?php
$weekday = 1;
for ($day=1; $day < idate("t", mktime(0 ,0 ,0 ,$mon, 1, $year))+1 ; $day++)
{
$tid = mktime(0 ,0 ,0 ,$mon, $day, $year); // Timestamp for dagen udregnes
$dagsdato = date('Y-m-d',mktime(0 ,0 ,0 ,$mon, $day, $year)); //Datoen for dagen udregnes til mysql-værdi
while($row['Slut'] < $dagsdato)
{
if(!$row = mysql_fetch_assoc($result))
break 1;
}
// Skifter til en ny uge
if( $weekday==8 )
{
echo "</tr><tr>\r\n";
echo "<td>".strftime("%W", $tid)."</td>";
$weekday=1;
}
//Den forige måneds sidste dage.
if ( $day==1)
{
echo "<td>".strftime("%W", $tid)."</td>";
$weekday = intval(strftime("%u", $tid));
for ($k=1 ; $k < $weekday ; $k++)
{
$lastdays=$k-$weekday;
echo '<td style="background-color: gray;">'.date("d", strtotime("$lastdays day", $tid))."</td>\r\n";;
}
}
// En optaget dag farves rød
if ($row['Start'] <= $dagsdato and $row['Slut'] >= $dagsdato )
{
echo "<td ";
echo 'style=" background-color: red;" ';
echo ">".date("d", $tid);
echo "</td>\r\n";
//strftime(" er en %A", $tid).
}
else
{
echo "<td ";
echo '> <a href="create_date.php?day='.$day.'&month='.$mon.'">'.date("d", $tid).'</a>';
echo "</td>\r\n";
}
//næste måneds datoer når den sidste dag i måneden er nået
if ( $day == idate("t", mktime(0 ,0 ,0 ,$mon, 1, $year)))
{
$weekday = intval(strftime("%u", $tid));
$nextdays = 0;
for ($k=7 ; $k > $weekday ; $k--)
{
$nextdays += 1;
echo '<td style="background-color: gray;">'.date("d", strtotime("+$nextdays day", $tid))."</td>\r\n";;
}
}
$weekday +=1;
}
?>
</tr>
</table>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?month=<?php echo $mon-1; ?>&year=<?php echo $year; ?>"><-- Forrige måned </a> <a href="<?php echo $_SERVER['PHP_SELF']; ?>?month=<?php echo $mon+1; ?>&year=<?php echo $year; ?>">Næste måned --></a>
</body>
</html>
Kan være det skal optimeres lidt, det var kun noget jeg somsagt lavede for sjov.
Det er en god lille test i dato håndtering.