Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calendar help needed

Status
Not open for further replies.

puremm

Technical User
Oct 30, 2006
23
GB
I can't get my dates to land on the correct day, using the following code;

<thead><tr>
<th width=35>Mon</th>
<th width=35>Tue</th>
<th width=35>Wed</th>
<th width=35>Thu</th>
<th width=35>Fri</th>
<th width=35>Sat</th>
<th width=35>Sun</th>
</tr></thead>
<tbody><tr>
<?php

$columnCounter = 0;

for ($fillLeadingEmptyDays=0; $fillLeadingEmptyDays <= ($firstOfMonthDay - 1); $fillLeadingEmptyDays+=1) {
$columnCounter = $columnCounter + 1;
echo "<td height=22></td>";
}

for ($enterDays=1; $enterDays <= $daysInCurrentMonth; $enterDays+=1) {
$columnCounter = $columnCounter + 1;
echo "<td height=22";
if ($day == $enterDays) { echo " class = \"today\""; }
$dayLinkDestination = "default.php?day=".$enterDays."&amp;month=".$month."&amp;year=".$year."";
echo "><a href=\"".$dayLinkDestination."\">".$enterDays."</a></td>";
if ( $columnCounter == 7 ) {
echo "</tr><tr>";
$columnCounter = 0;
}
}

for ($fillEmptyDays=($columnCounter + 1); $fillEmptyDays <= 7; $fillEmptyDays+=1) {
if ($columnCounter ==0) {
break;
}
echo "<td height=22></td>";
}
?>
</tr></tbody>
</table>

With a call to;

<?php

$day = @$_GET["day"];
$month = @$_GET["month"];
$year = @$_GET["year"];

if ($month == ""){
$day = date('j');
$month = date('n');
$year = date('Y');
}

$firstOfMonthDateStamp = mktime(24, 60, 60, $month, 1, $year);

$daysInCurrentMonth = date("t", $firstOfMonthDateStamp);
$firstOfMonthDay = date("d", $firstOfMonthDateStamp);
$lastOfMonthDay = date("w", strtotime(date("n",$firstOfMonthDateStamp)."/".$daysInCurrentMonth."/".date("Y",$firstOfMonthDateStamp)));
$previousMonth = getdate(strtotime('-1 month', $firstOfMonthDateStamp));
$nextMonth = getdate(strtotime('+1 month', $firstOfMonthDateStamp));
?>

I wonder if anyone can point me in the right direction...
 
oddly enough I rewrote a calendar script just this morning. here it is

Code:
<?php
$year = isset($_GET['year']) ? $_GET['year'] : date("Y");
$month = isset($_GET['month']) ? $_GET['month'] : date("n");
if (isset($_GET['change'])){
	$year = date("Y", strtotime($_GET['change']." months", strtotime("$year-$month-01")));
	$month = date("n", strtotime($_GET['change']." months", strtotime("$year-$month-01")));
}
$cal = new calendar($year, $month);
$cal->buildCalendar();
$cal->renderCalendar();

class calendar{

	public $calendar;

	public function calendar($year, $month){
		$this->year = $year;
		$this->month = $month;
	}	
	
	public function buildCalendar(){
		$uD = strtotime($this->year."-".$this->month."-01");
		$firstDay = date('N', $uD) - 1;
		$numDays = date("t", $uD);
		$output = '';
		$numRows = ceil($numDays/7);
		$r1 = true;
		$wordMonth = date("M", $uD);
		$output .= <<<HTML
	<tr>
		<th colspan="7">
			<a href="{$_SERVER['PHP_SELF']}?year={$this->year}&month={$this->month}&change=-1"> <-- </a>$wordMonth, {$this->year} <a href="{$_SERVER['PHP_SELF']}?year={$this->year}&month={$this->month}&change=1"> --> </a>
		</th>
	</tr>		
	<tr>
		<th>Mon</th>
		<th>Tues</th>
		<th>Weds</th>
		<th>Thurs</th>
		<th>Fri</th>
		<th>Sat</th>
		<th>Sun</th>
	</tr>

HTML;
		$day = 1;
	
		for ($r=0; $r<$numRows; $r++){
			$output .= "\t<tr>\r\n";
			$col =0;
			while ($col <7){
				if ($r1){
				
					while ($col < $firstDay){
						$output .= "\t\t<td>&nbsp;</td>\r\n";
						$col++;
					}
					$r1 = false;
				} 
				if ($day > $numDays){
					$output .= "\t\t<td>&nbsp;</td>\r\n";
				} else {
					$output .= "\t\t<td>".$this->getData($this->year, $this->month, $day)."</td>\r\n";
				}
				$day++;
				$col++;
			}
			$output .= "\t</tr>\r\n";
		}
		$this->calendar = "\r\n<table border=\"1\">\r\n".$output."\r\n</table>\r\n";
	}
		public function renderCalendar(){
		echo <<<HTML
<style>
body, select, th, td, option {font-family:"Trebuchet MS"; font-size:0.8em; color:blue;}
select {width:6em; text-align:center;}
table{table-layout:fixed; width:65em;}
a{text-decoration:none; color:blue; }
a.visited{text-decoration:none;}
</style>
</head>
<body>
{$this->calendar}
</body>
HTML;
	}

	private function getData($year, $month, $day){
		//return some data to go into each table cell
	}
} //end of class
?>
 
How weird ... I rewrote one too ! :)

It's a calendar that have color zones, clickable days, highlighted days and that is multilingual too.

Well, its not as clean as Jpadie's work though ;)

Code:
function hd_time_setlocal($lang) {

$local["fr"] = "'fra', 'fr_FR'";

eval("setlocale(LC_TIME, " . $local[$lang] . ");");

}

function hd_time_calendar($arg_imp) {

// - ! - available arguments and their default values
$arg["year_to_show"] = date("Y", time());
$arg["month_to_show"] = date("m", time());

$arg["prne_to_link"] = array();
$arg["day_name_strlen"] = 3;
$arg["cur_month_url"] = "";
$arg["first_day_of_week"] = 1;

$arg["zones"]["color"] = array(); // "#000000"
$arg["zones"]["from"] = array(); // "MM-DD" or "YYYY-MM-DD" 
$arg["zones"]["to"] = array(); // "MM-DD" or "YYYY-MM-DD" 

$arg["days"]["dates"] = array(); // "01.22" or "2007.01.22"
$arg["days"]["options"]["classname"] = array();
$arg["days"]["options"]["url"] = array();

$arg["days_to_link"]["url"] = "index.php?date=";
$arg["days_to_link"]["options"]["zone_in"] = "yes"; // yes / no 
$arg["days_to_link"]["options"]["zone_out"] = "no"; // yes / no 
$arg["days_to_link"]["options"]["start"]["date"] = NULL; // "YYYY-MM-DD" 
$arg["days_to_link"]["options"]["start"]["offset"] = 0; // number of days after date

$arg = array_merge($arg, $arg_imp);
   
// - ! - set variables
$first_day_of_month = gmmktime(0,0,0,$arg["month_to_show"],1,$arg["year_to_show"]);
$today["day"] = date("d", time());
$day_names_array = array();

    // - ! - get day names
    for($i = 0, $t = (3 + $arg["first_day_of_week"]) * 86400; $i < 7; $i++, $t += 86400) {
    
    $day_names_array[$i] = ucfirst(gmstrftime('%A',$t));
    
    }

list($arg["month_to_show"], $arg["year_to_show"], $month_name, $weekday) = explode(',',gmstrftime('%m,%Y,%B,%w',$first_day_of_month));
    
    // - ! - adjust for $arg["first_day_of_week"]
    $weekday = ($weekday + 7 - $arg["first_day_of_week"]) % 7;
    $title   = htmlentities(ucfirst($month_name)) . "&nbsp;" . $arg["year_to_show"];

    // - ! - begin calendar
    @list($prev_month_html, $prev_month_url) = each($arg["prne_to_link"]);
    @list($next_month_html, $next_month_url) = each($arg["prne_to_link"]);
    
    if($prev_month_html) $prev_month_html = "
    <span class=\"calendar-prev\">" . ($prev_month_url ? "
    <a href=\"" . htmlspecialchars($prev_month_url) . "\">" . $prev_month_html . "</a>
    " : $prev_month_html) . "</span> &nbsp;
    ";
    
    if($next_month_html) $next_month_html = "
    &nbsp; 
    <span class=\"calendar-next\">" . ($next_month_url ? "
    <a href=\"" . htmlspecialchars($next_month_url) . "\">" . $next_month_html . "</a>
    " : $next_month_html) . "</span>
    ";
    
    $calendar = "
    <table class=\"calendar\">" . "\n
    <caption class=\"calendar-month\">
    " . $prev_month_html . "
    " . ($month_href ? "<a href=\"" . htmlspecialchars($arg["cur_month_url"]) . "\">" . $title . "</a>" : $title) . "
    " . $next_month_html . "
    </caption>\n
    <tr>
    ";
    
    foreach($day_names_array as $day_name) {
    
    $calendar .= "
    <th abbr=\"" . htmlentities($day_name) . "\">
    " . htmlentities($arg["day_name_strlen"] < 4 ? substr($day_name, 0, $arg["day_name_strlen"]) : $day_name) . "
    </th>
    ";
    
    }
        
    $calendar .= "
    </tr>\n
    <tr>
    ";

    if($weekday > 0) $calendar .= "<td colspan=\"" . $weekday . "\">&nbsp;</td>";
    
    // - ! - on first iteration
    if (!$static) static $static;
    if (!$static) {
    
    // - ! - grab zones endings
    $first_to_array = $arg["zones"]["to"];
    
    // - ! - order them
    asort($first_to_array);
        
        // - ! - loop through zone endings
        foreach($first_to_array as $key => $value) {
            
            // - ! - if there is a zone ending with no start in this month
            if (strstr($value, $arg["month_to_show"] . "-")) { // THIS CAN BE BUGGY, MUST BE FIXED
            
            // - ! - set css
            $static["css"]["style"]["zone"] = " class=\"iszone\" style=\"
            background-color: " . $arg["zones"]["color"][$key] . ";
            \"";
            
            // - ! - set the very first day as zone start
            $static["is_zone"] = "yes";

            break;
            
            }
    
        }
    
    unset($key);
    
    }

    // - ! - loop through days
    for($day = 1, $days_in_month = gmdate('t', $first_day_of_month); $day <= $days_in_month; $day++, $weekday++) {
    
    $date["d"] = str_pad($day, 2, "0", STR_PAD_LEFT);
    $date["m-d"] = $arg["month_to_show"] . "-" . $date["d"];
    $date["Y-m-d"] = $arg["year_to_show"] . "-" . $date["m-d"];
        
        if($weekday == 7){
        
        $weekday   = 0;
        $calendar .= "</tr>\n<tr>";
        
        }

    $calendar .= "<td"; 
    
    // - ! - zone start
    $key_zones = array_search($date["m-d"], $arg["zones"]["from"]);
    
        if ($key_zones === FALSE) $key_zones = array_search($date["Y-m-d"], $arg["zones"]["from"]);

        if ($key_zones !== FALSE) {
        
        $static["css"]["style"]["zone"] = " class=\"iszone\" style=\"
        background-color: " . $arg["zones"]["color"][$key_zones] . ";
        \"";
        
        $static["is_zone"] = "yes";
        
        }
    
    $key_days = array_search($date["m-d"], $arg["days"]["dates"]);
     
        if ($key_days === FALSE) $key_days = array_search($date["Y-m-d"], $arg["days"]["dates"]);     
     
        // - ! - highlighted days
        if (in_array($date["m-d"], $arg["days"]["dates"]) OR in_array($date["Y-m-d"], $arg["days"]["dates"])) {

        $calendar .= " class=\"" . $arg["days"]["options"]["classname"][$key_days] . "\"";
        $is_highlighted = "yes";
        
        } else {
        
        $calendar .= $static["css"]["style"]["zone"];
        $is_highlighted = "no";
        
        }    
     
    $calendar .= ">";
    
    // - ! - days with links
    $day_temp = "
    " . (
    $arg["days_to_link"]["url"] ? "<a href=\"" . 
    $arg["days_to_link"]["url"] . $date["Y-m-d"] . 
    "\">" . $day . "</a>" : $day
    ) . "
    ";

        // - ! - link options
        if (
        $static["is_zone"] == "yes" AND $arg["days_to_link"]["options"]["zone_in"] == "no"
        ) $day_temp = $day;
        
        if (
        $static["is_zone"] == "no" AND $arg["days_to_link"]["options"]["zone_out"] == "no"
        ) $day_temp = $day; 

        if (
        $is_highlighted == "yes" AND $arg["days"]["options"]["url"][$key_days] == ""
        ) $day_temp = $day; 

        // - ! - link offset
        if ($static["offset"]["count"] == $arg["days_to_link"]["options"]["start"]["offset"]) {
        
        unset($static["offset"]["count"]);
        $static["offset"]["over"] = "yes";
        
        } else {
                    
            if ($arg["days_to_link"]["options"]["start"]["date"] == $date["Y-m-d"]) {
            
            $static["offset"]["count"] = 0;
            
            }
            
            if (isset($static["offset"]["count"])) {
            
            $static["offset"]["count"]++;
            $day_temp = $day; 
            
            } else {
            
                if (!$static["offset"]["over"] ) $day_temp = $day; 
            
            }            
        
        }
        
    // - ! - zone end    
    $key_zones = array_search($date["m-d"], $arg["zones"]["to"]);
    
        if ($key_zones === FALSE) $key_zones = array_search($date["Y-m-d"], $arg["zones"]["to"]);

        if ($key_zones !== FALSE) {
        
        $static["css"]["style"]["zone"] = "";
        
        $static["is_zone"] = "no";
        
        }          

    $calendar .= $day_temp . "</td>";
    
    }
    
    // - ! - empty days
    if($weekday != 7) $calendar .= "
    <td colspan=\"" . (7-$weekday) . "\">&nbsp;</td>
    ";

$calendar .= "
</tr>\n
</table>\n
";

// - ! - return calendar
return $calendar;

}


...

// - ! - set local time
hd_time_setlocal("fr");

  // - ! - loop through next year months
  for($month = $cal_months[0]; $month <= $cal_months[1]; $month++) {
  
  $hd_time_calendar["month_to_show"] = $month;
  $hd_time_calendar["year_to_show"] = $year;
  
  $output .= "
  " . hd_time_calendar($hd_time_calendar) . "
  ";
  
  }
 
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top