Jump to content

Only Show Saturdays of the month


xionhack

Recommended Posts

Hello. I want to show all the saturdays of this month, I tried the following code but its not working, can somebody help me?

 


$dtFirstDay = date("j/n/Y", mktime(0, 0, 0, date("m") , date("d")-date("d")+1, date("Y")));

$dtLastDay = date("j/n/Y", mktime(0, 0, 0, date("m")+1 , date("d")-date("d"), date("Y"))); 

    for($date = $dbFirstDay ; $date < $dtLastDay; $date = mktime(0,0,0,date("m", $date),date("d", $date)+1,date("Y", $date))){
        if(date('w' ,$date) == 0){
            echo $date;
        }

        }

Link to comment
Share on other sites

Grrrrrr the 'sledge hammer' approach...

takes any date in the YYYY/MM/DD format and does some 'stuff' ;)

 

<?PHP
#########################################
# function for getting 
#		the first day 
#		the last day
#		the number of days
#		in a given month - date must be in YYYY/MM/DD format
#########################################
function first_last_number_Of_Month($given_date) {
list($yr,$m,$dt) = split('/',$given_date); // separate year, month and date
	$first_day_of_month = $yr . "/" . $m . "/" . "01";
$first_day_ts = mktime( 0, 0, 0, $m, 1, $yr ); 
$last_day_of_month = $yr . "/" . $m . "/" . date("t",$first_day_ts); 
$number_of_days = date("t",$first_day_ts);
$first_last_array = array($first_day_of_month,$last_day_of_month,$number_of_days);
return $first_last_array;
} 

#########################################
# function for calculating 
#		the number of sundays, mondays, tuedays etc
#		there are in a given month
#		it uses the first_last_number_Of_Month function 
#
#		its returns the following array:
#				0 = date of first day format YYYY/MM/DD
#				1 = date of last day format YYYY/MM/DD
#				2 = number of days in the month
#				3 = number of sundays
#				4 = number of mondays
#				5 = number of tuesdays
#				6 = number of wednesdays
#				7 = number of thursdays
#				8 = number of fridays
#				9 = number of saturdays
#
#		in a given month - date must be in YYYY/MM/DD format
#########################################

function unique_weekdays_in_a_Month($given_date) {
$first_last_array = first_last_number_Of_Month($given_date);
$number_days = $first_last_array[2];
$i=0;
while($i<$number_days) {
	$date = date("D m d Y", (strtotime(date("Y/m/d", strtotime($first_last_array[0])) . " +$i day")));
	$week_day = date("D", (strtotime(date("Y/m/d", strtotime($first_last_array[0])) . " +$i day")));
	switch ($week_day){ 
		case "Sun": 
			$sundays = $sundays +1;
			break; 
		case "Mon": 
			$mondays = $mondays + 1; 
			break; 
		case "Tue": 
			$tuesdays = $tuesdays + 1; 
			break; 
		case "Wed": 
			$wednesdays = $wednesdays + 1; 
			break; 
		case "Thu": 
			$thursdays = $thursdays + 1; 
			break; 
		case "Fri": 
			$fridays = $fridays + 1; 
			break; 
		default: 
			$saturdays = $saturdays + 1; 
		break; 
	} 
$i++;
}
array_push ($first_last_array,$sundays,$mondays,$tuesdays,$wednesdays,$thursdays,$fridays,$saturdays); 

return $first_last_array;
}

###############################
#
#		END FUNCTIONS
#
#		BEGIN EXAMPLES
#
############################### 

$given_date = "2010/09/07";
$days_of_week_array = array ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
echo "For the given date of " . $given_date . ".  We get the following: </p>";
$first_last_array = unique_weekdays_in_a_Month($given_date);
$x = 0;
while($x<7) {
$i=0;
while($i<$first_last_array[2]) {
	$next_day = (60*60*24) * $i; // increment seconds in a day
	$ts = strtotime($first_last_array[0]) + $next_day; // get timestamp for date
	$what_day = date('D', $ts);
	if($what_day == $days_of_week_array[$x]) {
		echo date('l jS \of F Y h:i:s A', $ts) . "<br>";
	}
	$i++;
}
echo "<hr>";
$x++;
}

$ts1 = strtotime($first_last_array[0]);
$new_date = date('l jS \of F Y h:i:s A', $ts1);
echo "First day is " . $first_last_array[0] . " - aka - " . $new_date . "</p>";
$ts1 = strtotime($first_last_array[1]);
$new_date = date('l jS \of F Y h:i:s A', $ts1);
echo "Last  day is " . $first_last_array[1]  . " - aka - " . $new_date . "</p>";
echo "There are  " . $first_last_array[2] . " days in the month.</p>";
echo "There are  " . $first_last_array[3] . " sundays in the month.</p>";
echo "There are  " . $first_last_array[4] . " mondays in the month.</p>";
echo "There are  " . $first_last_array[5] . " tuesdays in the month.</p>";
echo "There are  " . $first_last_array[6] . " wednesdays in the month.</p>";
echo "There are  " . $first_last_array[7] . " thursdays in the month.</p>";
echo "There are  " . $first_last_array[8] . " fridays in the month.</p>";
echo "There are  " . $first_last_array[9] . " saturdays in the month.</p>";

?>

 

example: http://nstoia.com/demo/demo_date_stuff01/

 

makes sense?

 

Link to comment
Share on other sites

Here it is in general. Modify for your needs:

<?php
$month_start = strtotime('2010-05-01');
$month_end = strtotime('2010-05-31');
for($i=$month_start;$i<=$month_end;$i += 86400) {
   if (date('l',$i) == 'Saturday') {
       echo date('Y-m-d',$i) . "<br>\n";
   }
}
?>

 

Ken

Link to comment
Share on other sites

  • 3 weeks later...

Inspired on kenrbnsn's code:

 

$saturday_start = strtotime('first Saturday of May 2010');
$saturday_end = strtotime('last Saturday of May 2010');

$plus7days = 604800; // 86400 * 7
for ($i = $saturday_start;
     $i <= $saturday_end;
     $i += $plus7days) {
    echo date('Y-m-d', $i);
}

$tuesday_start = strtotime('first Tuesday of May 2010');
$tuesday_end = strtotime('last Tuesday of May 2010');

for ($i = $tuesday_start;
     $i <= $tudesday_end;
     $i += $plus7days) {
    echo date('Y-m-d', $i);
}

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.