Calendar Problem

Calendar Problem

am 11.08.2009 21:53:58 von TedD

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?

Cheers,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Calendar Problem

am 11.08.2009 22:08:07 von Robert Cummings

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?


$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.
--
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

Re: Calendar Problem

am 11.08.2009 22:30:43 von Shawn McKenzie

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?
>
> Cheers,
>
> tedd

Lots of ways to skin that cat, here is one:

$date = time(); //or whenever
$start = strtotime('-30 days', $date);
$end = strtotime('+30 days', $date);

$next = $start;
while(($next = strtotime('Next Friday', $next)) <= $end) {
echo date("F j, Y", $next)."\n";
}

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Calendar Problem

am 11.08.2009 22:32:23 von List Manager

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?
>
> >
> $fridays = array();
> for( $i = -30; $i <= 30; $i++ )
> {
> $mod = $i < 0 ? $i : '+'.$i;

man, please use some parenthesis...

my translation of the above would be the following:

$mod = (($i < 0) ? $i : ('+'.$i));

would that be correct?

>
> $time = strtotime( $mod.' day' );
>
> if( date( 'D', $time ) == 'Fri' )
> {
> $fridays[] = date( 'Y-m-d', $time );
> }
> }
>
> print_r( $fridays );
>
> ?>
>
> Cheers,
> Rob.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Calendar Problem

am 11.08.2009 22:39:59 von Robert Cummings

Jim Lucas wrote:
> 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?
>> >>
>> $fridays = array();
>> for( $i = -30; $i <= 30; $i++ )
>> {
>> $mod = $i < 0 ? $i : '+'.$i;
>
> man, please use some parenthesis...
>
> my translation of the above would be the following:
>
> $mod = (($i < 0) ? $i : ('+'.$i));
>
> would that be correct?

Yes... in a superfluous hold-my-hand sort of way :)

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

Re: Calendar Problem

am 11.08.2009 22:53:42 von Shawn McKenzie

Shawn McKenzie 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?
>>
>> Cheers,
>>
>> tedd
>
> Lots of ways to skin that cat, here is one:
>
> $date = time(); //or whenever
> $start = strtotime('-30 days', $date);
> $end = strtotime('+30 days', $date);
>
> $next = $start;
> while(($next = strtotime('Next Friday', $next)) <= $end) {
> echo date("F j, Y", $next)."\n";
> }
>

I must be bored:

for($next = strtotime('-30 days');
$next <= strtotime('+30 days');
$next = strtotime('Next Friday', $next))
{
echo date("F j, Y", $next)."\n";
}

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Calendar Problem

am 12.08.2009 00:29:00 von Paul M Foster

On Tue, Aug 11, 2009 at 03:53:58PM -0400, 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?

Convert to julian days, then add or subtract days as desired. Then
convert back to gregorian.

Paul

--
Paul M. Foster

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Calendar Problem

am 12.08.2009 15:20:19 von TedD

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?
>
> >
>$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:

$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


--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Calendar Problem

am 12.08.2009 15:25:23 von Robert Cummings

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?
>> >>
>> $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:
>
> > $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

Re: Calendar Problem

am 12.08.2009 15:55:41 von TedD

>>At 4:08 PM -0400 8/11/09, Robert Cummings wrote:
>
>I think Shawn McKenzie's is the best. It seems his would take at
>most 12 iterations.
>
>Cheers,
>Rob.

Rob:

For some reason I did not see/consider Shawn's solutions -- sorry Shawn.

However, it appears that mine is still the fastest in most test. Check this:

http://php1.net/b/fridays/

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Calendar Problem

am 12.08.2009 16:50:14 von Shawn McKenzie

tedd wrote:
>
> 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
>

Actually, if you refresh your page you have different winners. When I
first visited the page yours was fastest by approximately .001, but on
refresh yours is slower by .001. You need to execute the test let's say
100 or 1000 times or more (more is better) and take either the average
or I would say the minimum.

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Calendar Problem

am 12.08.2009 17:07:25 von TedD

At 9:50 AM -0500 8/12/09, Shawn McKenzie wrote:
>tedd wrote:
>>
>> 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
>>
>
>Actually, if you refresh your page you have different winners. When I
>first visited the page yours was fastest by approximately .001, but on
>refresh yours is slower by .001. You need to execute the test let's say
>100 or 1000 times or more (more is better) and take either the average
>or I would say the minimum.
>
>--
>Thanks!


But of course -- no one try is definitive, you need many.

For example, I wrote a script to show that PHP's rounding function
had an upward bias, which it does, but it took thousands of
iterations to see it. Not worth the effort to correct.

In any event, it's interesting to see how we all approached the
problem from different directions.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php