Re: Calendar Problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux