Calendar problem
am 15.01.2003 06:47:08 von 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
RE: Calendar problem
am 16.01.2003 05:14:20 von John 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
Re: Calendar Problem
am 12.08.2009 16:03:45 von Martin Scotta
--0016364ed61c8c78f90470f24988
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
On Wed, Aug 12, 2009 at 10:25 AM, Robert Cummings 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?
>>>>
>>>
>>>
>>> $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
>
>
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
$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
--0016364ed61c8c78f90470f24988--
Re: Calendar Problem
am 12.08.2009 16:26:15 von Stut
2009/8/12 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/
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: Calendar Problem
am 12.08.2009 16:28:31 von 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
>
>
>
>$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: Calendar Problem
am 12.08.2009 16:56:22 von TedD
At 3:26 PM +0100 8/12/09, Stuart wrote:
>2009/8/12 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/
>
>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: Calendar Problem
am 12.08.2009 17:50:35 von Martin Scotta
--001485e7e9609eff2e0470f3c7fe
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
On Wed, Aug 12, 2009 at 12:07 PM, tedd 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
--001485e7e9609eff2e0470f3c7fe--