|
Posted by Robert Cummings on 10/20/45 11:24
On Fri, 2005-08-19 at 02:10, Jasper Bryant-Greene wrote:
> benc11@gmail.com 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. |
`------------------------------------------------------------'
Navigation:
[Reply to this message]
|