• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

Change calendar from monthly to weekly

Roads - You helped with this code - I want to change it to a weekly view with the heading part showing  the days of that week so May 1st - May 7th instead of May 2011 with the next and previous options... Can you help again?
<?php
 
$nextm=$_GET['month'];
$nexty=$_GET['year'];
 

require_once("includes/db_login.inc");
 
if($nextm == "13")
{
        $nextm=1;
        $nexty++;
}
 
if($nextm == "0")
{
        $nextm=12;
        $nexty--;
}
 
//This gets today's date
$date =time () ;
 
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
 
If ($nextm != ""){
        $month=$nextm;
        
}
else{
$month = date('m', $date) ;
 
}
$month2  = date('m', $date);
$minmonth=$month-1;
$maxmonth=$month+1;
If ($nexty != "") {
        $year=$nexty;
}
else{
$year = date('Y', $date) ;
}
$year2 = date('Y', $date) ;
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
 
//This gets us the month name
$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 "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
 
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ; 
//Here we start building the table heads
echo "<table  border=2 class=table width=100% height=80%>";
echo "<tr><th class=head colspan=7> <a href=calendar2.php?month=$minmonth&year=$year><<</a>   $title $year <a href=calendar2.php?month=$maxmonth&year=$year>>></a>   </th></tr>";
echo "<tr class=body><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</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 ($row = mysql_fetch_array($res))
//{
// while ($row2 = mysql_fetch_array($res2))
// {    
	while ( $day_num <= $days_in_month )
	{
      	if ($day==$day_num and $year2==$year and $month2==$month) // so this is today
		{
                
            ?><td class=day2 bgcolor="#FF0000"><? echo "$day_num <br />";
 
 
        	$resl = "select * from Events";
    		$res=mysql_query($resl) or die(mysql_error());
            $row = mysql_fetch_array($res);
        
        
            $thisday = mktime(0,0,0,$month, $day_num, $year);
            $dd = date ("Y-m-d",$thisday); 
       	
            $resl2 = "select * from Events WHERE SUBSTR(StartDate FROM 1 FOR 10) = '$dd'";         
        	
     		$res2=mysql_query($resl2) or die(mysql_error());
            while ($row = mysql_fetch_assoc($res2)){
	    
                
            $event_id = $row['ID'];
                            $event_name = $row['Event'];
                            $event_date = $row['StartDate'];
                            $event_time = mysql2timestamp($event_date);
                            $event_display_date = date("D, F j, Y", $event_time);
                            $event_description = $row['LocationID'];
                            $curDate = getdate($event_time);
                            $curMonth = $curDate['mon'];
                            $curYear = $curDate['year'];
                            $curFullMonth = $curDate['month'];
             
             echo "$event_name<br />";
             }
		?> 
		
		
		</td>


		<?
		
		}
        else{
            $thisday = mktime(0,0,0,$month, $day_num, $year);
            $dd = date ("Y-m-d",$thisday); 
       	
            $resl2 = "select * from Events WHERE SUBSTR(StartDate FROM 1 FOR 10) = '$dd'"; 
            $res2=mysql_query($resl2) or die(mysql_error());
        ?>
        <td class=day>
        <? echo "$day_num\n"; 
        while($row2 = mysql_fetch_array($res2)){
            $event_name2 = $row2['Event'];
            echo"$event_name2<br />";
            }            
        ?> 
        </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 <=7 )
{
echo "<td>  </td>";
$day_count++;
}
 
echo "</tr></table>"; 

 

 ?>

Open in new window

0
katlees
Asked:
katlees
1 Solution
 
Umar Topia.Net Full Stack DeveloperCommented:
JQuery Week Calendar has been provided at the following location:-
https://github.com/robmonie/jquery-week-calendar
0
 
Atique AnsariCommented:
I think you should use the Jquery UI calendar.

Please go to the below URL and choose the theme you want and download the JS file.

http://jqueryui.com/demos/datepicker/
0
 
Lukasz ChmielewskiCommented:
You could base your weekly calendar on that

 $timestamp = time();
echo date("Y-m-d H:i:s", $timestamp);
echo "<br />";
echo date("Y-m-d H:i:s", strtotime("last sunday", $timestamp));
echo "<br />";
echo date("Y-m-d H:i:s", strtotime("next saturday", $timestamp));
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now