Jump to content

Problem with Calendar Dates


steelej

Recommended Posts

I'm in a spot here.  I'm trying to troubleshoot an applicaton built by someone else.  It is a tutor appointment scheduler.  It is working fine except that it seems to think all month's have 30 days.  I'm trying to ferret out the error and I think it's in the code below, but I'm having trouble making sense of it:

// print a week
    $today = date("w"); //number of day of the week
$month = date("m");
$day = date("d");
$year = date("y");
$times = array(1=>"8:00",2=>"8:30",3=>"9:00",4=>"9:30",5=>"10:00",6=>"10:30",7=>"11:00",8=>"11:30",9=>"12:00",10=>"12:30",11=>"1:00",12=>"1:30",13=>"2:00",14=>"2:30",15=>"3:00",16=>"3:30",17=>"4:00",18=>"4:30" );
$week = array(1=>"Monday",2=>"Tuesday",3=>"Wednesday",4=>"Thursday",5=>"Friday",6=>"Saturday",7=>"Sunday");


for ( $d=0; $d < 7; $d++)
{
if ($day<10)
$day = sprintf( "%02d", $day);
if ($month<10)
$month = sprintf( "%02d", $month);

echo '<table border="0">'."\n";
echo "<tr>\n";
echo "<td><u>".$week[(($today+$d)%7)]."</u></td>\n";
echo "</tr>\n";
echo "<tr>\n";

if ( date("l", mktime(0, 0, 0, $month, $day, $year)) != "Sunday")
echo "<td>".$month.'/'.$day.'/'.$year."</td>\n";

echo "</tr>\n";
Link to comment
Share on other sites

barand made this for me to learn calender in php hope this helps.

// remember we all crawl before we walk good luck.

[code]
CREATE TABLE room_booking (
`bookID` int(11)  NOT NULL auto_increment,
`clientID` int(11)  NOT NULL ,
`roomID` int(11)  NOT NULL ,
`arrive` date  NOT NULL DEFAULT '0000-00-00',
`depart` date  NOT NULL DEFAULT '0000-00-00',
`nights` int(11)  NOT NULL ,
`discount` varchar(100)  NOT NULL ,
`status` tinyint(1)  NOT NULL ,
PRIMARY KEY(bookID)
);

INSERT INTO room_booking VALUES ('1', '1', '1', '2006-06-30', '2006-07-02', '2', '0', '1');
INSERT INTO room_booking VALUES ('2', '2', '1', '2006-07-07', '2006-07-11', '3', '0', '2');
INSERT INTO room_booking VALUES ('3', '3', '1', '2006-07-14', '2006-07-17', '2', '0', '1');
INSERT INTO room_booking VALUES ('4', '4', '2', '2006-07-07', '2006-07-11', '3', '0', '2');
INSERT INTO room_booking VALUES ('5', '1', '2', '2006-07-14', '2006-07-19', '4', '0', '2');
INSERT INTO room_booking VALUES ('6', '3', '4', '2006-07-14', '2006-07-29', '14', '0', '3');

[/code]

Calendar script
[code]
Code:
<?php
include('db.php');

$cal_start = isset($_GET['cal_start']) ? $_GET['cal_start'] : mktime(0,0,0);
$cal_days = $bookings = array();
  // dates for calendar display
for ($i=0; $i<28; $i++) {
$cal_days[] = strtotime("+$i days", $cal_start);
}
// create room booking array
for ($r=1; $r<=4; $r++) {
$bookings[$r]=array();
foreach($cal_days as $cd) {
$bookings[$r][$cd] = 0;
}
}

$cal_end = end($cal_days);
  // get bookings for calendar period and put in array
$d1 = date('Y-m-d', $cal_start);
$d2 = date('Y-m-d', $cal_end);
$sql = "SELECT b.bookID, b.clientID, b.roomID, b.arrive, b.depart, b.status
FROM room_booking b
WHERE ((b.arrive BETWEEN '$d1' AND '$d2') OR
  (b.depart BETWEEN '$d1' AND '$d2'))
ORDER BY b.roomID, b.arrive";
$res = mysql_query($sql) or die(mysql_error());
while (list($bid, $cid, $rid, $arr, $dep, $stat) = mysql_fetch_row($res)) {
for ($d=strtotime($arr); $d<=strtotime("$dep -1 days"); $d+=86400) {
if (isset($bookings[$rid][$d])) $bookings[$rid][$d] = $stat;
}
}
  // calc prev and next weeks
$prevweek = strtotime('-7 days', $cal_start);
$nextweek = strtotime('+7 days', $cal_start);

function display_calendar (&$dates, &$bookings, $prev, $next) {
echo "<table cellspacing='0' cellpadding='0' >\n";
echo "<TR><TH><a href='?cal_start=$prev'>&lt;</a></TH>
<TH colspan='27'>Fawlty Towers Guest House<br>Room Bookings</TH>
<TH><a href='?cal_start=$next'>&gt;</a></TH></TR>";
// dates
echo "<TR>\n";
echo "<TH>Room</TH>" ;
foreach ($dates as $day) {
switch (date('w', $day)) {
case 0:
case 6: $class = 'class=wkend'; break;
default: $class = '';
}
echo "<TH $class>".date('M', $day).'<br>'.date('j', $day)."</TH>\n";
}
echo "</TR>\n";

// rooms
foreach ($bookings as $room => $rmdata) {
display_room_bookings ($room, $rmdata);
}
echo "</table><br>\n";
display_key();
}

function display_room_bookings ($room, $rmdata) {
echo "<TR><TH>$room</TH>\n";
foreach ($rmdata as $dt=>$status) {
switch ($status) {
case 1: $class = 'class=booked'; break;
case 2: $class = 'class=conf'; break; 
case 3: $class = 'class=paid'; break; 
default: $class = ''; break; 
}
echo "<TD $class>&nbsp;</TD>\n";
}
}

function display_key() {
echo '<table cellspacing="0">
<tr><td>Free</td>
<td class="booked">Booked</td>
<td class="conf">Confirmed</td>
<td class="paid">Paid</td>
</tr></table>';
}

?>
<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>Sample bookings calendar</title>
<meta name="author" content="Barand">
<meta name="creation-date" content="07/15/2006">
<style type='text/css'>
        table {
        border-left: 1px solid gray;
        border-top:  1px solid gray;
        width: 90%;
        }
        th {
        border-right: 1px solid gray;
        border-bottom:  1px solid gray;
        font-family: sans-serif;
        font-size: 0.75em;
        font-weight: 700;
        background-color: #E0E0E0;
        height : 20px;
        width: 3.5%
        }
        th.wkend {
        background-color: #C0C0C0;
        }
        td {
        border-right: 1px solid gray;
        border-bottom:  1px solid gray;
        font-family: sans-serif;
        font-size: 0.75em;
        font-weight: 300;
        text-align: center;
        width: 25px;
        height : 20px;
        background-color: #C0FFC0;
        }
        td.booked {
        background-color: #FFFFC0;
        }
        td.conf {
        background-color: #DFC868;
        }
        td.paid {
        background-color: #FF8080;
        }
</style>
</head>
<body>
<?php display_calendar ($cal_days, $bookings, $prevweek, $nextweek)?>
</body>
</html>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.