Re: [PHP] Calendar Problem

2009-08-12 Thread 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?


?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


--
---
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: [PHP] Calendar Problem

2009-08-12 Thread 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?

?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



Re: [PHP] Calendar Problem

2009-08-12 Thread 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: [PHP] Calendar Problem

2009-08-12 Thread Martin Scotta
On Wed, Aug 12, 2009 at 10:25 AM, Robert Cummings rob...@interjinn.comwrote:



 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


Re: [PHP] Calendar Problem

2009-08-12 Thread Stuart
2009/8/12 tedd tedd.sperl...@gmail.com:
 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/

Bit late to this party, but give this a whizz...

http://dev.stut.net/php/fridays.php

-Stuart

-- 
http://stut.net/

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



Re: [PHP] Calendar Problem

2009-08-12 Thread tedd

At 11:03 AM -0300 8/12/09, Martin Scotta wrote:

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



Martin:

It's included here:

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

Works great -- thanks.

As Shawn said Many ways to skin a cat

As Jeff Foxworthy added But he ain't going to like any of them!

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: [PHP] Calendar Problem

2009-08-12 Thread 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: [PHP] Calendar Problem

2009-08-12 Thread tedd

At 3:26 PM +0100 8/12/09, Stuart wrote:

2009/8/12 tedd tedd.sperl...@gmail.com:

 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/


Bit late to this party, but give this a whizz...

http://dev.stut.net/php/fridays.php

-Stuart


It's included here:

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

Looks like your solution is the fastest.

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: [PHP] Calendar Problem

2009-08-12 Thread 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



Re: [PHP] Calendar Problem

2009-08-12 Thread Martin Scotta
On Wed, Aug 12, 2009 at 12:07 PM, tedd tedd.sperl...@gmail.com wrote:

 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


Well, this was a nice experiment.
What do we must learn about this? performance  legibility
Stuart solutions look that's the faster, but in the other hand Shawn's
solution 2 looks the most legible (so far).

-- 
Martin Scotta


[PHP] Calendar Problem

2009-08-11 Thread 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: [PHP] Calendar Problem

2009-08-11 Thread 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?


?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.
--
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: [PHP] Calendar Problem

2009-08-11 Thread Jim Lucas
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;

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: [PHP] Calendar Problem

2009-08-11 Thread 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?

?php

$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: [PHP] Calendar Problem

2009-08-11 Thread 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: [PHP] Calendar problem

2003-01-15 Thread John W. Holmes
 I have two fields in my form. When two dates are entered in these two
 boxes, I need to find the diiference in calendar months and remaining
days
 between these two dates. This Calendar months and remaining days is
 important. The functions that I know do not work for calendar months.
 Can anyone help me and tell me how this could be achieved and are
there
 any PHP functions to do this?

You can use strtotime() (probably) to convert what's entered into a unix
timestamp. From there you know how many seconds are in a day, so a
little math can figure out how many days are between the two dates. You
can use date() to find out the month of each one and do some more
math...

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/



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




[PHP] Calendar problem

2003-01-14 Thread menezesd

Hello friends.

I have two fields in my form. When two dates are entered in these two boxes, I need to 
find the diiference in calendar months and remaining days between these two dates. 
This Calendar months and remaining days is important. The functions that I know do 
not work for calendar months.
Can anyone help me and tell me how this could be achieved and are there any PHP 
functions to do this?

best regards
Denis

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