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