Change calendar from monthly to weekly

Posted on 2011-04-30
Last Modified: 2012-05-11
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?

if($nextm == "13")
if($nextm == "0")
//This gets today's date
$date =time () ;
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
If ($nextm != ""){
$month = date('m', $date) ;
$month2  = date('m', $date);
If ($nexty != "") {
$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
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;
//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 />";

            $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 />";

//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>";
echo "</tr></table>"; 



Open in new window

Question by:katlees
    LVL 10

    Expert Comment

    by:Umar Topia
    JQuery Week Calendar has been provided at the following location:-
    LVL 7

    Expert Comment

    by:Atique Ansari
    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.
    LVL 27

    Accepted Solution

    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));

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now