On Wed, Aug 12, 2009 at 10:25 AM, Robert Cummings <robert@xxxxxxxxxxxxx>wrote: > > > tedd wrote: > >> At 4:08 PM -0400 8/11/09, Robert Cummings wrote: >> >>> tedd wrote: >>> >>>> Hi gang: >>>> >>>> I want to show the dates for all Fridays +-30 days from a specific date. >>>> >>>> For example, given today's date (8/11/2009) the Fridays that fall +-30 >>>> days are July 17, July 24, July 31, Aug 7, Aug 14, Aug 21, Aug 28, and Sept >>>> 4. >>>> >>>> I'm curious, how would you guys solve this? >>>> >>> <?php >>> >>> $fridays = array(); >>> for( $i = -30; $i <= 30; $i++ ) >>> { >>> $mod = $i < 0 ? $i : '+'.$i; >>> >>> $time = strtotime( $mod.' day' ); >>> >>> if( date( 'D', $time ) == 'Fri' ) >>> { >>> $fridays[] = date( 'Y-m-d', $time ); >>> } >>> } >>> >>> print_r( $fridays ); >>> >>> ?> >>> >>> Cheers, >>> Rob. >>> >> >> Rob: >> >> That's slick -- you never let me down with your simplicity and creativity. >> >> My solution was to find the next Friday and then cycle through +-28 days >> (four weeks) from that date, like so: >> >> <?php >> $fridays = array(); >> >> for( $i = 1; $i <= 7; $i++ ) >> { >> $time = strtotime( $i . ' day' ); >> if( date( 'D', $time ) == 'Fri' ) >> { >> $start = 28 - $i; >> $end = 28 + $i; >> } >> } >> >> for( $i = -$start; $i <= $end; $i += 7 ) >> { >> $time = strtotime( $i . ' day' ); >> $fridays[] = date( 'Y-m-d', $time ); >> } >> >> print_r( $fridays ); >> ?> >> >> Your solution had 61 iterations (for loop) while mind had only 21, so >> mine's a bit faster. Here's the comparison: >> >> http://php1.net/b/fridays/ >> >> But I'll use your solution -- it's more elegant. >> >> Thanks for the code, >> >> tedd >> > > I think Shawn McKenzie's is the best. It seems his would take at most 12 > iterations. > > Cheers, > Rob. > -- > http://www.interjinn.com > Application and Templating Framework for PHP > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Hi all This is my point of view. I try to stay the most legible and simple as possible. @tedd: can you benchmark this script too? usualy legibility > performance <?php $fridays = array(); # I'll store the friday here $day = strtotime('-1 month', time() ); # let's start at one month ago while( 5 != date('w', $day)) # advance to a friday $day = strtotime('+1 day', $day); # I'll stop when $day where ahead one month from today $end = strtotime( '+1 month', time() ); do{ $friday[] = date('Y-m-d', $day); # store the friday $day = strtotime('+1 week', $day); # advance one week } while( $day < $end ); # job's done! print_r( $friday ); -- Martin Scotta