Re: Week Days

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

 



Rob, 
 This code works great! Thank you for all of your help and everyone else who 
responded!

 On 8/18/05, Robert Cummings <robert@xxxxxxxxxxxxx> wrote: 
> 
> On Fri, 2005-08-19 at 02:10, Jasper Bryant-Greene wrote:
> > benc11@xxxxxxxxx wrote:
> > > I am trying to add 3 (or a user-defined amount) week days to a certain 
> date..
> > > An example is today 2005-08-18 then adding 3 week days to give me a 
> date of
> > > 2005-08-23. I have tried searching online but cannot find an easy way 
> of
> > > doing so.
> >
> > $numDaysToAdd = 3;
> > $s = ($numDaysToAdd == 1) ? '' : 's';
> >
> > $dayInFuture = strtotime("+$numDaysToAdd day$s");
> > $dayName = date('l', $dayInFuture);
> > if($dayName == 'Saturday') {
> > $dayInFuture = strtotime("+2 days", $dayInFuture);
> > } else if($dayName == 'Sunday') {
> > $dayInFuture = strtotime("+1 day", $dayInFuture);
> > }
> >
> > Disclaimer: untested code.
> 
> This doesn't work since the value can be user defined and thus may span
> multiple weeks :) The following works but is dirty, I'm sure there's a
> better solution using modulus operator and adding and subtracting and a
> little voodoo... I'm just too lazy atm to think it up ;)
> 
> <?php
> 
> $weekdays = array
> (
> 'Mon' => true,
> 'Tue' => true,
> 'Wed' => true,
> 'Thu' => true,
> 'Fri' => true,
> );
> 
> $daySeconds = (60 * 60 * 24);
> 
> $start = time();
> $addWeekdays = 3;
> 
> $final = $start;
> while( $i < $addWeekdays )
> {
> $final += $daySeconds;
> echo date( 'D', $final )."\n";
> if( isset( $weekdays[date( 'D', $final )] ) )
> {
> $i++;
> }
> }
> 
> echo 'Start: '.date( 'Y-m-d (D)', $start )."\n";
> echo 'Final: '.date( 'Y-m-d (D)', $final )."\n";
> 
> Cheers,
> Rob.
> --
> .------------------------------------------------------------.
> | InterJinn Application Framework - http://www.interjinn.com |
> :------------------------------------------------------------:
> | An application and templating framework for PHP. Boasting |
> | a powerful, scalable system for accessing system services |
> | such as forms, properties, sessions, and caches. InterJinn |
> | also provides an extremely flexible architecture for |
> | creating re-usable components quickly and easily. |
> `------------------------------------------------------------'
> 
> --
> 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