Reply to Re: [PHP] Week Days

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация