954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Show all days in month except for Sunday

Hi. I need to be able to display each day of the current month excluding Sunday in a format: month/day (eg. 11/11).

So I want the output to be:

11/11
11/12
//excludes Sunday
11/14
11/15


etc.

Does anyone know how to do this?

Thanks

calebcook
Junior Poster in Training
66 posts since Jun 2011
Reputation Points: 10
Solved Threads: 4
 

Actually, I only need to know how to get the dates of all the Sundays in the month in an array.

For instance:

array('6', '13', '20', '27'); //The first Sunday of this month is on the 6th, the second on the 13th, etc


How can I do that?

calebcook
Junior Poster in Training
66 posts since Jun 2011
Reputation Points: 10
Solved Threads: 4
 

Since I just wrote this code answering your first question:

<?php
//Create an instance of now
//This is used to determine the current month and also to calculate the first and last day of the month
$now = new DateTime( 'now', new DateTimeZone( 'America/New_York' ) );

//Create a DateTime representation of the first day of the current month based off of "now"
$start = new DateTime( $now->format('m/01/Y'), new DateTimeZone( 'America/New_York' ) );

//Create a DateTime representation of the last day of the current month based off of "now"
$end = new DateTime( $now->format('m/t/Y'), new DateTimeZone( 'America/New_York' ) );

//Define our interval (1 Day)
$interval = new DateInterval('P1D');

//Setup a DatePeriod instance to iterate between the start and end date by the interval
$period = new DatePeriod( $start, $interval, $end );

//Iterate over the DatePeriod instance
foreach( $period as $date ){

	//Make sure the day displayed is greater than or equal to today
	//Make sure the day displayed is NOT sunday.
	if( $date >= $now && $date->format('w') != 0 ){
		echo $date->format( 'l, Y-m-d H:i:s' ).PHP_EOL;
	}
}
Saturday, 2011-11-12 00:00:00
Monday, 2011-11-14 00:00:00
Tuesday, 2011-11-15 00:00:00
Wednesday, 2011-11-16 00:00:00
Thursday, 2011-11-17 00:00:00
Friday, 2011-11-18 00:00:00
Saturday, 2011-11-19 00:00:00
Monday, 2011-11-21 00:00:00
Tuesday, 2011-11-22 00:00:00
Wednesday, 2011-11-23 00:00:00
Thursday, 2011-11-24 00:00:00
Friday, 2011-11-25 00:00:00
Saturday, 2011-11-26 00:00:00
Monday, 2011-11-28 00:00:00
Tuesday, 2011-11-29 00:00:00


This is easily adapted to answer your second question as well:

//Iterate over the DatePeriod instance
foreach( $period as $date ){

	//Make sure the day displayed is ONLY sunday.
	if( $date->format('w') == 0 ){
		echo $date->format( 'l, Y-m-d H:i:s' ).PHP_EOL;
	}
}
Sunday, 2011-11-06 00:00:00
Sunday, 2011-11-13 00:00:00
Sunday, 2011-11-20 00:00:00
Sunday, 2011-11-27 00:00:00


Format the return as you see fit and store the values into an array instead of displaying them.NOTE: This is PHP 5.3+ ONLY

mschroeder
Work Harder
Team Colleague
666 posts since Jul 2008
Reputation Points: 279
Solved Threads: 131
 

Thanks mschroeder!

calebcook
Junior Poster in Training
66 posts since Jun 2011
Reputation Points: 10
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: