Re: Calendar Problem

From: Date: Wed, 12 Aug 2009 13:25:23 +0000
Subject: Re: Calendar Problem
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-296692@lists.php.net to get a copy of this message
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

« previous php.general (#296692) next »