Re: Week Days

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

 



On Sat, 2005-08-20 at 03:15, benc11@xxxxxxxxx wrote:
> Rob, 
> 
> I tried your code below, but it didn't work. I put the number of week days 
> in $addWeekdays variable. Am I doing something wrong or is there an error in 
> the code? Appreciate your help!

There's an error... I didn't account for the starting day being a
weekend day :) The following should work (somewhat tested) and I also
removed locale dependency from it (check for "Sat" and "Sun").

<?php

//
// For every 5 days to add we wrap a week and are right back where we
// started so...
//

$addWeekdays = isset( $argv[1] ) ? $argv[1] : 0; // command line for fun
$start = time();

$addWeeks = (int)($addWeekdays / 5);
$addWeekDays = ($addWeekdays % 5);

$daySeconds = (24 * 60 * 60);

$final = $start + ($addWeeks * 7 * $daySeconds);
$day = date( 'w', $final );

if( $day == 1 ) // Saturday
{
    $final += ($daySeconds * 2);
}
else
if( $day == 0 ) // Sunday
{
    $final += $daySeconds;
}
else
{
    while( $addWeekDays > 0 )
    {
        $final += $daySeconds;

        $day = date( 'w', $final );
        if( $day != '0' && $day != '6' )
        {
            $addWeekDays--;
        }
    }
}

echo 'Start: '.date( 'Y-m-d (D)', $start )."\n";
echo 'Final: '.date( 'Y-m-d (D)', $final )."\n";


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