PHP Kalender
Jeg fandt en php kalender på et tutorial site, og valgte at bygge lidt videre på den med noget session.Jeg har dog problemer med at tælle måneder op og ned uden de konflikter med hinanden! Og nu er jeg fandme ved at blive sinsyg af det!
Min idé med kalenderen er således, når måneden skifter fra dec til jan skal året sættes en op, ligeledes skal skift fra jan til dec trække en fra året.
Året skal bare kunne tælles op og ned.
Er der en der kan gennemskue hvad min lille hjerne havde forestillet sig? LOL
<?php
session_start();
echo $_SESSION['month'];
//This gets today's date
$date =time () ;
//This puts the day, month, and year in seperate variables
if(!isset($_SESSION['month']))
{
if($_SESSION['month'] == "")
{
$_SESSION['month'] = date('m', $date) ;
}
if($_SESSION['year'] == "")
{
$_SESSION['year'] = date('Y', $date) ;
}
}
if($_SESSION['month'] >=12)
{
unset($_SESSION['month']);
$_SESSION['month'] = 0;
$_SESSION['year']++;
}
if($_SESSION['month'] <=0)
{
unset($_SESSION['month']);
$_SESSION['month'] = 11;
}
/*
$_SESSION['day'] = date('d', $date) ;
$_SESSION['month'] = date('m', $date) ;
$_SESSION['year'] = date('Y', $date) ;
*/
if($_GET[action] == monthplus)
{
$_SESSION['month']++;
}
if($_GET[action] == monthminus)
{
$_SESSION['month']--;
}
if($_GET[action] == yearplus)
{
$_SESSION['year']++;
}
if($_GET[action] == yearminus)
{
$_SESSION['year']--;
}
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$_SESSION['month'], 1, $_SESSION['year']) ;
//This gets us the month name
$_SESSION['title'] = date('F', $first_day) ;
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Mon": $blank = 0; break;
case "Tue": $blank = 1; break;
case "Wed": $blank = 2; break;
case "Thu": $blank = 3; break;
case "Fri": $blank = 4; break;
case "Sat": $blank = 5; break;
case "Mon": $blank = 6; break;
}
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $_SESSION['month'], $_SESSION['year']) ;
$month = $_SESSION['month'];
$year = $_SESSION['year'];
$title = $_SESSION['title'];
//Here we start building the table heads
echo "<table border=0 width=500>";
echo "<tr><td width=42><a href=\"calendar.php?action=yearminus\"><<</a></td><td width=42><a href=\"calendar.php?action=monthminus\"><</a></td><td width=42> $title <td width=42></td><td width=42>$year </td><td width=42><a href=\"calendar.php?action=monthplus\">></a></td><td width=42><a href=\"calendar.php?action=yearplus\">>></a></td></th></tr>";
echo "<tr><td width=42>M</td><td width=42>T</td><td width=42>O</td><td width=42>T</td><td width=42>F</td><td width=42>L</td><td width=42>S</td></tr>";
//This counts the days in the week, up to 7
$day_count = 1;
echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td></td>";
$blank = $blank-1;
$day_count++;
}
//sets the first day of the month to 1
$day_num = 1;
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td><a href=\"calendar.php?datefrom=$day_num.$month.$year\">$day_num</a></td>";
$day_num++;
$day_count++;
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
}
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=6 )
{
echo "<td> </td>";
$day_count++;
}
echo "</tr></table>";
?>