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

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?

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

Thanks mschroeder!

Hello.Thank you. I tested this code to find Sundays in August, 2014. Does not print the last Sunday- 31.08.2014.

@dani.stanisheva, to fix it you can change this codeline:

$end      = new \DateTime( $now->format('m/t/Y'), new \DateTimeZone( 'America/Bahia' ) );

to:

$end      = new \DateTime( $now->format("m/".**cal_days_in_month(CAL_GREGORIAN,$month,$year)**."/Y **23:59:59**"), new \DateTimeZone( 'America/Bahia' ) );

Add "cal_days_in_month(CAL_GREGORIAN,$month,$year) and 23:59:59" on format method. It will include the last day.

I create this function to wrap it:

function getOccurencesDatesOf($weekday, $month, $year){
    $occurrences = array();
    $now      = new \DateTime( 'now', new \DateTimeZone( 'America/Bahia' ) );

    $start    = new \DateTime( $now->format("$month/01/$year"), new \DateTimeZone( 'America/Bahia' ) );
    $end      = new \DateTime( $now->format("$month/".cal_days_in_month(CAL_GREGORIAN,$month,$year)."/$year 23:59:59"), new \DateTimeZone( 'America/Bahia' ) );
    $interval = new \DateInterval('P1D');
    $period   = new \DatePeriod( $start, $interval, $end );

    foreach($period as $date){
        if( $date->format('w') == $weekday ){
            $occurrences[] = $date;
            echo $date->format( 'l, d/m/Y H:i:s' )."<br />";
        }
    }

    return $occurrences;
}
Member Avatar for diafol

@Adriano_1

Whatever the solution, you're a year late. Please resist the temptation to necropost. Thanks.

@diafol independent of time, I posted a fix. If anyone ever need someone, It will find the right solution. Just as I needed. Be smart. :)

Member Avatar for diafol

No Adriano, not independent of time. That's the whole point. Dead threads clutter up the top pages. Thank you.

Andriano_1 this question was allready solved (four years ago and not one). But are you sure you posted "a fix" ? Have you tested your own code ? You wrote:

$end      = new \DateTime( $now->format("m/".**cal_days_in_month(CAL_GREGORIAN,$month,$year)**."/Y **23:59:59**"), new \DateTimeZone( 'America/Bahia' ) );

what is this ".**" after format("m/" ?

Thx @jkon!

If you see the function code dont has: cal_days_in_month(CAL_GREGORIAN,$month,$year)

You are sure. ** is bold here **. When i paste it on code window i didn't pay attention. The correct code is:

$end      = new \DateTime( $now->format("m/".cal_days_in_month(CAL_GREGORIAN,$month,$year)."/Y 23:59:59"), new \DateTimeZone( 'America/Bahia' ) );

So the function is the fix. I tested on 1970 to 2099 years range, for all months and all days. You can test too.

Thx again to contribute on my necro-comment! haha

Regards,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.