0

Hi,

I want to find All no of week in any month.
And first and last date of that week.

Assume Month is Jan 2011

date in Y-m-d
1st week -> Start date=2011-01-01 End date=2011-01-01
2nd week -> Start date=2011-01-02 End date=2011-01-08
3rd week -> Start date=2011-01-09 End date=2011-01-15
4th week -> Start date=2011-01-16 End date=2011-01-22
5th week -> Start date=2011-01-23 End date=2011-01-29
6th week -> Start date=2011-01-30 End date=2011-01-31


How to Find all these?

Edited by Aamit: n/a

Attachments cal.png 2.95 KB
5
Contributors
13
Replies
15
Views
6 Years
Discussion Span
Last Post by urtrivedi
0

What you want to use, myql query , php code or combination of both?

Edited by urtrivedi: n/a

0

//sorry will come back, misread the post

Edited by diafol: n/a

0
<?php
function getWeeks($month,$year){
	$month = intval($month);				//force month to single integer if '0x'
	$suff = array('st','nd','rd','th','th','th'); 		//week suffixes
	$end = date('t',mktime(0,0,0,$month,1,$year)); 		//last date day of month: 28 - 31
  	$start = date('w',mktime(0,0,0,$month,1,$year)); 	//1st day of month: 0 - 6 (Sun - Sat)
	$last = 7 - $start; 					//get last day date (Sat) of first week
	$noweeks = ceil((($end - ($last + 1))/7) + 1);		//total no. weeks in month
	$output = "";						//initialize string		
	$monthlabel = str_pad($month, 2, '0', STR_PAD_LEFT);
	for($x=1;$x<$noweeks+1;$x++){	
		if($x == 1){
			$startdate = "$year-$monthlabel-01";
			$day = $last - 6;
		}else{
			$day = $last + 1 + (($x-2)*7);
			$day = str_pad($day, 2, '0', STR_PAD_LEFT);
			$startdate = "$year-$monthlabel-$day";
		}
		if($x == $noweeks){
			$enddate = "$year-$monthlabel-$end";
		}else{
			$dayend = $day + 6;
			$dayend = str_pad($dayend, 2, '0', STR_PAD_LEFT);
			$enddate = "$year-$monthlabel-$dayend";
		}
		$output .= "{$x}{$suff[$x-1]} week -> Start date=$startdate End date=$enddate <br />";	
	}
	return $output;
}

if(isset($_POST) && !empty($_POST)){
	$month = $_POST['m'];
	$year = $_POST['y']; 
	echo getWeeks($month,$year);
}
?>


<form method="post">
M: <input name="m" value="6" />
Y: <input name="y" value="2011" />
<input type="submit" value="go" />

</form>

You can try that. I've probably over-egged the calculations, but it seems to work. You could easily add functionality for date input formats (e.g. '6/2011' or 'Jun 2011'). You could use strtotime() or similar for this.

Edited by diafol: n/a

0

Dont know if this is what you are looking for, I see you have thumbnail of a calender in your question, but I programmed a small calendar one time and you can use it if you like:

function determine_next_months(&$month, &$year){
    if($month == 1){
        $prev_month = 12;
        $next_month = 2;
        $prev_year = $year - 1;
        $next_year = $year;
    }
    else if($month == 12){
        $prev_month = 11;
        $next_month = 1;
        $prev_year = $year;
        $next_year = $year + 1;
    }
    else{
        $prev_month = $month - 1; 
        $next_month = $month + 1;
        $prev_year = $year;
        $next_year = $year;
    }
    ?>
    <tr height="30">
    <td colspan="3" align="right"><a href="tester.php?month=<?php echo $prev_month."&year=".$prev_year; ?>"><img src="images/Knob Left.png" /></a></td>
    <td></td>
    <td colspan="3" align="left"><a href="tester.php?month=<?php echo $next_month."&year=".$next_year; ?>"><img src="images/Knob Forward.png" /></a></td>
    </tr>
    <?php
} 

function make_calendar(&$current_month, &$current_year, &$current_month_text, &$current_day = "", $my_array){
    //First initialize counters for us in the for calender for loop.
    $count_col = 0;
    $count_days = 1;
    
    //determine the first weekday of the month(to know how many empty columns to print and then when to start printing out the days)
    $first_weekday = date('w', mktime(0, 0, 0, $current_month, 1, $current_year));
    //Here we determine the last day of the month(to know when to stop printing out days and how many empty columns after that.).
    //First I put the limit to 31, then decrement the counter until I hit the right day, then I break out of the loop and $i is then the last day
    $limit = 31;
    for($i = $limit; $i > 28; $i--){
        if(checkdate($current_month, $i, $current_year)){ 
            break;
        }
    }
    //reset the limit variable to what is stored in $i, since I plan to use $i to count in next loop.
    $limit = $i;
    ?>
    <table class="calender_coulumns">
        <tr height="30">
            <td colspan="7" align="center"><?php echo $current_month_text." ".$current_year; ?></td>
        </tr>
        <tr>
            <td width="25" class="calender_columns blue_day">SU</td>
            <td width="25" class="calender_columns blue_day">MO</td>
            <td width="25" class="calender_columns blue_day">TU</td>
            <td width="25" class="calender_columns blue_day">WE</td>
            <td width="25" class="calender_columns blue_day">TH</td>
            <td width="25" class="calender_columns blue_day">FR</td>
            <td width="25" class="calender_columns blue_day">SA</td>
        </tr>
    
    <?php
    //Here I determine when to put the final closing tag on the row</tr> I take the first_weekday(which is a number sunday = 0 saturday = 7)
    //and I then add it to the limit
    
    $CONST_LIMIT = $first_weekday + $limit;
    for($i = 1; $i <= $CONST_LIMIT; $i++){
        //if the count_col is 0, then it is time for a opening row tag
        if($count_col == 0){
            echo "<tr>";
        }
        //if the $i counter is less or equal to the first_weekday, then I print out empty column. Remember first weekeday sunday is 0 but the
        //i counter is initialized at 0.
        if($i <= $first_weekday){
            echo "<td></td>";
        }
        else{
            //inside here, it is plain old printing out numbers, and of course different styling and link according to if there is special date.
            //Check to see if day is inside array, meaning it has event.
            if($i == $current_day && in_array($count_days, $my_array)){
                ?>
                <td class="calender_columns purple_day"><a href="events.php?day=<?php echo $count_days."&month=".$current_month."&year=".$current_year; ?>"><?php echo $count_days; ?></a></td>
                <?php
            }
            else if($count_days == $current_day){
                ?>
                <td class="calender_columns purple_day"><?php echo $count_days; ?></td>
                <?php
            }
            else if(in_array($count_days, $my_array)){
                ?>
                <td class="calender_columns green_day"><a href="events.php?day=<?php echo $count_days."&month=".$current_month."&year=".$current_year; ?>"><?php echo $count_days; ?></a></td>
                <?php
            }
            else{
            echo "<td class=\"calender_columns\">".$count_days."</td>";
            }
            $count_days++;
        }
        
        
        $count_col++;
        //above I incremented the col, if it equals 7, then I am at the end of the row and need to close it and initialize count_col to zero again
        if($count_col == 7){
            echo "</tr>";
            $count_col = 0;
        }
        //here all the days have been printed out, but possible not all the empty columns, so use a bit of math to print out the rest of the
        //columns, empty of course and then close the row.
        else if($i == $CONST_LIMIT){
            for($g = $count_col; $g < 7; $g++){
                echo "<td></td>";
            }
            echo "</tr>";
        }
    }
    //Now we call another function to determine next and previous month to give the user the benefit of browsing through months. Notice the table
    //is still not closed, so in the function I can start by opening and closing rows instead of new table. Argument is month and year, sent as         
    //reference to save processing.
    determine_next_months($current_month, $current_year);
    ?>
    </table>
<?php
}
?>

<!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>Untitled Document</title>
</head>

<body>
<?php
if(isset($_GET['month']) && isset($_GET['year'])){
    $current_month = $_GET['month'];
    $current_year = $_GET['year'];
    $current_month_text = date('F', mktime(0,0,0, $current_month, 1, $current_year));
    
    if($current_month == date('m') && $current_year == date('Y')){
        $current_day = date('d');
    }
    else{
        $current_day = "";
    }
    
}
else{
    $current_day = date('d');
    $current_month_text = date('F');
    $current_month = date('m');
    $current_year = date('Y');
}
$my_array = array('2','7','8');
make_calendar($current_month, $current_year, $current_month_text, $current_day, $my_array);
?>
</body>
</html>
0

I changed the code a bit before I posted it here, so there are some broken links, like some links direct to events.php and others to tester.php. Obviously the css style sheet is missing and I threw the database connection out that fetches event dates and just put instead 2, 7, 8.
If there are sessions on your page, then you would want to strip the get variables of all tags
But this calendar always starts on the current month, has previous and next month links to fetch other months, different styling for current date, different styling and link on special dates(with events) and no styles for empty dates.
Possibly it is something you can use.

Notice on line 35 you can see in one line how to get the weekday of the first day of the month. And online 38-43 you can see how to determine last day of the month(although there is probably a better way to do this than 5 lines of code).

0

Hi ardav,

I tried this code. On some conditions it fails like 10 2010 (i.e.October 2010)

It gives output

1st week -> Start date=2010-10-01 End date=2010-10-02
2nd week -> Start date=2010-10-03 End date=2010-10-09
3rd week -> Start date=2010-10-10 End date=2010-10-16
4th week -> Start date=2010-10-17 End date=2010-10-23
5th week -> Start date=2010-10-24 End date=2010-10-31

Edited by Aamit: n/a

Attachments cal.png 2.83 KB
1
<?php 		

$textdt="01 june 2011";
$dt= strtotime( $textdt);
$currdt=$dt;
$nextmonth=strtotime($textdt."+1 month");
$i=0;

do 
{
    $weekday= date("w",$currdt);
    $nextday=7-$weekday;
    $endday=abs($weekday-6);
    $startarr[$i]=$currdt;
    $endarr[$i]=strtotime(date("Y-m-d",$currdt)."+$endday day");
    $currdt=strtotime(date("Y-m-d",$endarr[$i])."+1 day");

    echo "Week ".($i+1). "-> start date = ". date("Y-m-d",$startarr[$i])." end date = ". date("Y-m-d",$endarr[$i])."<br>";
     $i++;
 	  		     
}while($endarr[$i-1]<$nextmonth);



?>

Edited by urtrivedi: n/a

Votes + Comments
like this :)
0

Brilliant U! Nice and simple.

However, the last end date is a bit off.

0

Your code (which I love BTW) gives:

Week 1-> start date = 2011-06-01 end date = 2011-06-04
Week 2-> start date = 2011-06-05 end date = 2011-06-11
Week 3-> start date = 2011-06-12 end date = 2011-06-18
Week 4-> start date = 2011-06-19 end date = 2011-06-25
Week 5-> start date = 2011-06-26 end date = 2011-07-02

He gave the impression that he just wants the dates of that particular month:

1st week -> Start date=2011-01-01 End date=2011-01-01
2nd week -> Start date=2011-01-02 End date=2011-01-08
3rd week -> Start date=2011-01-09 End date=2011-01-15
4th week -> Start date=2011-01-16 End date=2011-01-22
5th week -> Start date=2011-01-23 End date=2011-01-29
6th week -> Start date=2011-01-30 End date=2011-01-31

0

Thanks ardav, I though He wanted upto last date of week, Here is corrected code

<?php 		

$textdt="01 jan 2011";
$currdt= strtotime( $textdt);
$nextmonth=strtotime($textdt."+1 month");
$i=0;
$flag=true;

do 
{
    $weekday= date("w",$currdt);
    $endday=abs($weekday-6);
    $startarr[$i]=$currdt;
    $endarr[$i]=strtotime(date("Y-m-d",$currdt)."+$endday day");
    $currdt=strtotime(date("Y-m-d",$endarr[$i])."+1 day");

	if($endarr[$i]>=$nextmonth)
	{
		$endarr[$i]=strtotime(date("Y-m-d",$nextmonth)."-1 day");;
		$flag=false;		
	}

	echo "Week ".($i+1). "-> start date = ". date("Y-m-d",$startarr[$i])." end date = ". date("Y-m-d",$endarr[$i])."<br>";
    $i++;
 	  		     
}while($flag);



?>

Edited by urtrivedi: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.