Jump to content

loop through days 1 - 28 and then start again 1 - 28


brown2005

Recommended Posts

<!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=utf-8" />
<title>Linear Calendar</title>
<style type="text/css" media="all">
body {
font-family:"Calibri";
color:#009;
}
.currentDay {
background:#FFC;
color:red;
}
table {
border-collapse:collapse;
border:1px #009 solid;
}
td {
height:60px;
vertical-align:top;
text-align:center;
width:30px;
}
.days:hover {
background:#9F0;
border-color:#000;
}
.day6 {
background:#ECECFF;
}
.day7 {
background:#ECECFF;
}
.monthName {
text-align:left;
vertical-align:middle;
}
.monthName div {
padding-left:10px;
}
</style>
</head>


<body>
<?php
$dDaysOnPage = 37;
$dDay = 1;
if ($_REQUEST['year'] <> "") { $dYear = $_REQUEST['year']; } else { $dYear = date("Y"); }
?>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<th><?php echo $dYear; ?></th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
</tr>
<?php


function FriendlyDayOfWeek($dayNum) {


if ($dayNum == 0){ return 7; } else { return $dayNum; }


}


for($mC=1;$mC<=12;$mC++){

$currentDT = mktime(0,0,0,$mC,$dDay,$dYear);

echo "<tr><td class='monthName'><div>".date("F",$currentDT)."</div></td>";

$daysInMonth = date("t",$currentDT);


echo InsertBlankTd(FriendlyDayOfWeek(date("w",$currentDT))-1);

for($i=1;$i<=$daysInMonth;$i++){

 $exactDT = mktime(0,0,0,$mC,$i,$dYear);

 if ($i==date("d")&&date("m",$currentDT)==date("m")){

  $class="currentDay";

 }else{ 

  $class = "";

 }



[b]   echo "<td class='".$class." days day".FriendlyDayOfWeek(date("w",$exactDT))."'>".$i."</td>";
 [/b]


}


echo InsertBlankTd($dDaysOnPage - $daysInMonth - FriendlyDayOfWeek(date("w",$currentDT))+1);

echo "</tr>";

}


?>
</table>
</body>
</html>


<?php


function InsertBlankTd($numberOfTdsToAdd) {

for($i=1;$i<=$numberOfTdsToAdd;$i++) {

 $tdString .= "<td></td>";

}

return $tdString;

}


?>

 

i have the above code that is for a yearly calendar, now I want to start at the 1/1/2013 on the number 16 then loop through the till 28, start at 1 again, until the end of the calendar?

 

any help is much appreciated

Link to comment
Share on other sites

for($x=16;$x <=28;$x++){

echo "<td class='".$class." days day".FriendlyDayOfWeek(date("w",$exactDT))."'>".$i. "<br />" .$x."</td>";

}

 

i have added this, but it shows 16-28 for every day and not just the corresponding answer, such as:

 

1/1/13 = 16

2/1/13 = 17

3/1/13 = 18

 

then when it gets to 28 go back to 1

Edited by brown2005
Link to comment
Share on other sites

Here you go. You only need to modify the first three variables. I changed the output for verification purposes. Modify as you need it.

 

$calendarYear = '2013';
$indexStart = 16;
$indexMax = 28;

$date = strtotime("1/1/{$calendarYear}");
$year = $calendarYear;
$index = $indexStart - 1;
echo "<table border='1'>";
echo "<tr><th>No:</th><th>Date</th><th>Day of Week</th></tr>";
while($year == $calendarYear)
{
$indexNo = $index % $indexMax + 1;
$dateStr = date('m-d-Y', $date);
$dowNo = date('w', $date);
echo "<tr><td>$indexNo</td><td>{$dateStr}</td><td>{$dowNo}</td></tr>\n";

$index++;
$date = strtotime("+1 day", $date);
$year = date('Y', $date);
}

Edited by Psycho
Link to comment
Share on other sites

<!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=utf-8" />
<title>Linear Calendar</title>
<style type="text/css" media="all">
body {
font-family:"Calibri";
color:#009;
}
.currentDay {
background:#FFC;
color:red;
}
table {
border-collapse:collapse;
border:1px #009 solid;
}
td {
height:60px;
vertical-align:top;
text-align:center;
width:30px;
}
.days:hover {
background:#9F0;
border-color:#000;
}
.day6 {
background:#ECECFF;
}
.day7 {
background:#ECECFF;
}
.monthName {
text-align:left;
vertical-align:middle;
}
.monthName div {
padding-left:10px;
}
</style>
</head>

<body>
<?php

$dDaysOnPage = 37;

$dDay = 1;

if ($_REQUEST['year'] <> "") { $dYear = $_REQUEST['year']; } else { $dYear = date("Y"); }
?>

<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<th><?php echo $dYear; ?></th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
</tr>

<?php

function FriendlyDayOfWeek($dayNum) {

if ($dayNum == 0){ return 7; } else { return $dayNum; }

}

for ($mC=1;$mC<=12;$mC++){

$currentDT = mktime(0,0,0,$mC,$dDay,$dYear);

echo "<tr><td class='monthName'><div>".date("F",$currentDT)."</div></td>";

$daysInMonth = date("t",$currentDT);

echo InsertBlankTd(FriendlyDayOfWeek(date("w",$currentDT))-1);

$calendarYear = '2013';
$indexStart = 16;
$indexMax = 28;

$date = strtotime("1/1/{$calendarYear}");
$year = $calendarYear;
$index = $indexStart - 1;#

for($i=1;$i<=$daysInMonth;$i++) {

$exactDT = mktime(0,0,0,$mC,$i,$dYear);

if ($i==date("d")&&date("m",$currentDT)==date("m")) { $class="currentDay"; } else { $class = ""; }

$indexNo = $index % $indexMax + 1;

$dateStr = date('d', $date);

$dowNo = date('D', $date);

echo "<td class='".$class." days day".FriendlyDayOfWeek(date("w",$exactDT))."'>".$dateStr."<br />".$indexNo."</td>";

$index++;
$date = strtotime("+1 day", $date);
$year = date('Y', $date);

}


echo InsertBlankTd($dDaysOnPage - $daysInMonth - FriendlyDayOfWeek(date("w",$currentDT))+1);
echo "</tr>";

}

?>

</table>
</body>
</html>
<?php
function InsertBlankTd($numberOfTdsToAdd) {
for($i=1;$i<=$numberOfTdsToAdd;$i++) {
$tdString .= "<td></td>";
}
return $tdString;
}
?>

 

I have tried incorporating your code into the linear calendar I have and I can not seem to get it to work, it does the calendar, but every month, the first day of that month starts on 16 yet I would like it to continue from where it left off the month before.

 

I look forward to your help.

Link to comment
Share on other sites

  • 2 weeks later...

Well, I'll help. But, here's the thing. If I really don't like something in someone's code I will change it. I'm not being paid to do this, so that's my perogative. And, I really don't like how you are outputting the calendar. It looks really odd how each month/row is staggered from the previous. I also moved the "logic" to the top of the page.

 

Using functions such as date() and strtotime() are inefficient. So, I rewrote the logic from the ground up. Since we know there are 12 months in a year we only need to determine the number of days in each month to figure out how to build the calendar. So, we only need to use those functions once for each month - not each day.

 

<?php

//Determine the year
$dYear = isset($_REQUEST['year']) ? trim($_REQUEST['year']) : false;
if (strlen($dYear)!=4 || !ctype_digit($dYear))
{
   $dYear = date("Y");
}

//Create vars for the code assignments
$code = 16;    //Start value
$codeMax = 28; //Maximum value

//Create array of weekday names for headers
$dowNames = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Fri');

//Create var to hold the output
$calendarHTML = '';
//Var to test for current day
$todayStr = date('Y-n-j');

//Iterate through each month
for($dMonth = 1; $dMonth <= 12; $dMonth++)
{
   //Create needed date/time variables for the month
   $monthTimeStamp = strtotime("{$dYear}-{$dMonth}-01");
   $monthName   = date('F', $monthTimeStamp);
   $daysInMonth = date('t', $monthTimeStamp);
   $dowInt	  = date('w', $monthTimeStamp);

   //Iterate through each day
   $monthHeadersHTML = '';
   $monthDaysHTML = '';
   for($dDay=1; $dDay<=31; $dDay++)
   {
    //Check if day exists
    if($dDay > $daysInMonth)
    {
	    //Day does not exist in current month
	    $monthDaysHTML .= "<td></td>\n";
	    $monthHeadersHTML .= "<th></th>\n";
    }
    else
    {
	    //Check if code has exceeded maximum
	    if($code > $codeMax) { $code = 1; }

	    //Determine appropriate class for the dow & day
	    if($dowInt%7 != 0 && $dowInt%7 != 6) {
		    $dowClass = " weekDay";
	    }
	    else {
		    $dowClass = " weekEnd";
	    }
	    $dayClass = ("{$dYear}-{$dMonth}-{$dDay}" == $todayStr) ? 'currentDay' : $dowClass;

	    //Create the output for the day & header
	    $monthDaysHTML .= "<td class=\"day {$dayClass}\"><b>{$dDay}</b><br>[{$code}]</td>\n";
	    $monthHeadersHTML .= "<th class=\"dow {$dowClass}\">{$dowNames[$dowInt%7]}</th>\n";
	    //Increment code and day of week values
	    $code++;
	    $dowInt++;
    }
   }

   //Create output for the current month
   $calendarHTML .= "<tr>\n";
   $calendarHTML .= "<th class=\"monthName\" rowspan=\"2\">{$monthName}</th>\n";
   $calendarHTML .= $monthHeadersHTML;
   $calendarHTML .= "</tr>\n";
   $calendarHTML .= "<tr>\n";
   $calendarHTML .= $monthDaysHTML;
   $calendarHTML .= "</tr>\n";
}

?>
<!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=utf-8" />
<title>Linear Calendar</title>
<style type="text/css" media="all">
body {
   font-family: "Calibri";
   color: #009;
}

table {
   border-collapse: collapse;
   border: 1px #009 solid;
}

td {
   height:60px;
   vertical-align: top;
   text-align: center;
   width: 30px;
}

.monthName {
   text-align: left;
   vertical-align: middle;
   padding-left: 10px;
   width: 10px;
   border-top-width: 3px;
   border-bottom-width: 3px;
}

.dow {
   border-top-width: 3px;
}

.day {
   border-bottom-width: 3px;
}

.day:hover {
   background: #9F0;
   border-color: #000;
}

.currentDay {
   background: #FFC;
   color:red;
   border-color: #009
}

.weekDay {

}

.weekEnd {
   background:#ECECFF;
}
</style>

</head>

<body>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
 <th colspan="32"><?php echo $dYear; ?></th>
</tr>
<?php echo $calendarHTML; ?>

</table>
</body>
</html>

Edited by Psycho
Link to comment
Share on other sites

i have the above code that is for a yearly calendar, now I want to start at the 1/1/2013 on the number 16 then loop through the till 28, start at 1 again, until the end of the calendar?

 

So you want the resulting calendar to have the "index" values like below?

 

Mic4fQm.png

 

 

P.S. @Psycho, you seem to have entirely missed the point of a linear calendar. :shy:

Edited by salathe
Link to comment
Share on other sites

P.S. @Psycho, you seem to have entirely missed the point of a linear calendar. :shy:

 

I guess so. This will work - and again removes the problems with many calls to date() and strtotime()

<?php

//Determine the year
$dYear = isset($_REQUEST['year']) ? trim($_REQUEST['year']) : false;
if (strlen($dYear)!=4 || !ctype_digit($dYear))
{
   $dYear = date("Y");
}

//Create vars for the code assignments
$code = 16;    //Start value
$codeMax = 28; //Maximum value
//Max number of cells for the days
$maxCells = 37;

//Create var to hold the output
$calendarBodyHTML = '';
//Var to test for current day
$todayStr = date('Y-n-j');

//Iterate through each month
for($dMonth = 1; $dMonth <= 12; $dMonth++)
{
   //Create needed date/time variables for the month
   $monthTimeStamp = strtotime("{$dYear}-{$dMonth}-01");
   $monthName   = date('F', $monthTimeStamp);
   $daysInMonth = date('t', $monthTimeStamp);
   $dowStart    = date('N', $monthTimeStamp);

   $monthDaysHTML = '';
   $dDay = 1;
   for($cellIdx=1; $cellIdx<=$maxCells; $cellIdx++)
   {
    //Show empty cell for days before/after days in month
    if($cellIdx<$dowStart || $cellIdx>$daysInMonth+$dowStart-1)
    {
	    $monthDaysHTML .= "<td class=\"empty\"></td>\n";
    }
    else
    {
	    //Reset code, if needed
	    if($code > $codeMax) { $code = 1; }

	    //Determine appropriate class for the dow & day
	    $dayClass = ($cellIdx%7==0 || $cellIdx%7==6) ? 'weekEnd' : 'weekDay';
	    if("{$dYear}-{$dMonth}-{$dDay}" == $todayStr)
	    {
		    $dayClass = 'currentDay';
	    }

	    $monthDaysHTML .= "<td class=\"day {$dayClass}\"><b>{$dDay}</b><br>[{$code}]</td>\n";

	    //Increment code and day of week values
	    $code++;
	    $dDay++;
    }
   }

   //Create output for the current month
   $calendarBodyHTML .= "<tr>\n";
   $calendarBodyHTML .= "<th class=\"monthName\">{$monthName}</th>\n";
   $calendarBodyHTML .= $monthDaysHTML;
   $calendarBodyHTML .= "</tr>\n";
}

//Create the calendar headers
$dowNames = array('Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su');
$calendarHeadHTML = "<th>{$dYear}</th>\n";
for($cellIdx=0; $cellIdx<$maxCells; $cellIdx++)
{
   $calendarHeadHTML .= "<th>{$dowNames[$cellIdx%7]}</th>\n";
}

?>
<!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=utf-8" />
<title>Linear Calendar</title>
<style type="text/css" media="all">
body {
   font-family: "Calibri";
   color: #009;
}

table {
   border-collapse: collapse;
   border: 1px #009 solid;
}

td {
   height:60px;
   vertical-align: top;
   text-align: center;
   width: 30px;
}

.monthName {
   text-align: left;
   vertical-align: middle;
   padding-left: 10px;
   width: 10px;
}

.empty {
   background: #ccc;
}

.day:hover {
   background: #9F0;
   border-color: #000;
}

.currentDay {
   background: #FFC;
   color:red;
}

.weekDay {

}

.weekEnd {
   background:#ECECFF;
}
</style>

</head>

<body>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
   <?php echo $calendarHeadHTML; ?>
</tr>
<?php echo $calendarBodyHTML; ?>
</table>

</body>
</html>

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.