t3chguy
asked on
Add days to previous looped value
I am calculating a tentative due date for each task within a project, and I calculated the first task's due date just fine, which was 5 business days from the project start date, but now I want to calculate each new based off of the previous tasks's due date.
The project Start date is 7-15-13 Task one has a duration of 5 days, which should set deadline date to 7-22-13 Task two has a duration of 5 days, which should set deadline date to 7-19-13 Task three has a duration of 3 days which should set my deadline date to 7-24-13.
Each project can have a variable number of tasks.
The project Start date is 7-15-13 Task one has a duration of 5 days, which should set deadline date to 7-22-13 Task two has a duration of 5 days, which should set deadline date to 7-19-13 Task three has a duration of 3 days which should set my deadline date to 7-24-13.
Each project can have a variable number of tasks.
//Format the Start Date
$startDate = date("Y-m-d", strtotime($projectStart));
//Subtract one day to project length to include the starting date in the count.
$projectLengthMinusOne = ($projectLength - 1);
//Calculate estimated deadline date based off of out of office, absence, holidays, and weekdays.
$deadlineDate = date("Y-m-d", strtotime($projectLengthMinusOne . ' weekdays', strtotime($startDate)));
//Check for holidays
$checkHolidays = mysql_query("SELECT id FROM fm_calendar_holidays WHERE fromDate BETWEEN '".$startDate."' AND '".$deadlineDate."'") or die("Check Holidays: " . mysql_error());
$numHolidays = mysql_num_rows($checkHolidays);
//Check for Absences
$checkAbsences = mysql_query("SELECT numDays FROM fm_calendar_absenceRequest WHERE startDate BETWEEN '".$startDate."' AND '".$deadlineDate."' and respid = '".$taskData['assignedTo']."'") or die("Check Absences: " . mysql_error());
if(mysql_num_rows($checkAbsences) > 0)
{
$totalAbsences = 0;
while($abData = mysql_fetch_array($checkAbsences))
{
$numDays = $abData['numDays'];
$totalAbsences += $numDays;
}
}
$numAbsences = $totalAbsences;
//Check for out of office
$checkOutOffice = mysql_query("SELECT startDate, endDate FROM fm_calendar_outofOffice WHERE startDate BETWEEN '".$startDate."' AND '".$deadlineDate."' and requesterId = '".$taskData['assignedTo']."'") or die("Check Out of Office: " . mysql_error());
if(mysql_num_rows($checkOutOffice) > 0)
{
$totalOutOffice = 0;
while($oooData = mysql_fetch_array($checkOutOffice))
{
$oooStart = $oooData['startDate'];
$oooEnd = $oooData['endDate'];
$numWorkingDays = getWorkingDays($oooStart, $oooEnd);
$totalOutOffice += $numWorkingDays;
}
}
$numOutOffice = $totalOutOffice;
//Add up all days off from the calendar
$totalDaysAdded = ($numHolidays + $numAbsences + $numOutOffice);
//Recalculate the deadline date based off of the new days off information
$deadlineDate = date("Y-m-d", strtotime($totalDaysAdded . ' weekdays', strtotime($deadlineDate)));
$deadlineDate = date("Y-m-d", strtotime($taskData['taskLength'] . ' weekdays', strtotime($deadlineDate)));
echo '<div class="page_collapsible" id="body-section1">'.$taskData['taskName'] . '<span></span></div>';
echo '<div class="container">
<div class="collapseContainer">';
echo '<table width = "900px" cellspacing = "5" cellpadding = "5" border = "0">
<tr>
<td width = "125px" align="right"><strong>Description:</strong></td>
<td width = "775px" align="left">'.$taskData['taskDescription'] . '</td>
</tr>
<tr>
<td width = "125px" align="right"><strong>Programmer:</strong></td>
<td width = "775px" align="left">'.getCkname($taskData['assignedTo']). '</td>
</tr>
<tr>
<td width = "125px" align="right"><strong>Task Status:</strong></td>
<td width = "775px" align="left">'.$taskData['taskStatus'] . '</td>
</tr>
<tr>
<td width = "125px" align="right"><strong>Task Duration:</strong></td>
<td width = "775px" align="left">'.$taskData['taskLength'] . ' days</td>
</tr>
<tr>
<td width = "125px" align="right"><strong>Tentative Due Date:</strong></td>
<td width = "775px" align="left">'.$deadlineDate . '</td>
</tr>
</table>
</div>
</div>';
}
}
ASKER
Thank you for that, very nice article. Still having problems visualizing how to get that to work with my code.
I don't know what to say. Your code snippet begins by relying on undefined variables. I don't know how to fix that.
ASKER
Alright
let me modify the code so that it doesn't.
let me modify the code so that it doesn't.
$getTasks = mysql_query("SELECT assignedTo, taskId, taskName, taskStatus, taskLength, taskDescription FROM helpDesk_projectTasks WHERE projectId = '".$projectId."' ORDER BY taskId") or die("Get Tasks: " . mysql_error());
if(mysql_num_rows($getTasks) > 0)
{
while($taskData = mysql_fetch_array($getTasks))
{
$startDate = '2013-07-09';
$deadlineDate = date("Y-m-d", strtotime($taskData['taskLength'] . ' weekdays', strtotime($startDate)));
echo '<div class="page_collapsible" id="body-section1">'.$taskData['taskName'] . '<span></span></div>';
echo '<div class="container">
<div class="collapseContainer">';
echo '<table width = "900px" cellspacing = "5" cellpadding = "5" border = "0">
<tr>
<td width = "125px" align="right" valign="top"><strong>Description:</strong></td>
<td width = "775px" align="left">'.$taskData['taskDescription'] . '</td>
</tr>
<tr>
<td width = "125px" align="right" valign="top"><strong>Programmer:</strong></td>
<td width = "775px" align="left">'.getCkname($taskData['assignedTo']). '</td>
</tr>
<tr>
<td width = "125px" align="right" valign="top"><strong>Task Status:</strong></td>
<td width = "775px" align="left">'.$taskData['taskStatus'] . '</td>
</tr>
<tr>
<td width = "125px" align="right" valign="top"><strong>Task Duration:</strong></td>
<td width = "775px" align="left">'.$taskData['taskLength'] . ' days</td>
</tr>
<tr>
<td width = "125px" align="right" valign="top"><strong>Tentative Due Date:</strong></td>
<td width = "775px" align="left">'.$deadlineDate . '</td>
</tr>
<tr>
<td width = "125px" align="right" valign="top"><strong>Task Related ?s:</strong></td>
<td width = "775px" align="left"><textarea rows = "6" cols = "80" class="fieldclasssm" name="taskQuestions">'.$_POST['taskQuestions'].'</textarea></td>
</table>
</div>
</div>';
}
}
Maybe the best way to work on this problem is to deconstruct it into some kind of SSCCE -- something that is small enough that we can get that one step right, then build back up toward the larger collection of moving parts.
As I understand the problem, there is a known "first date" and we want to add some number of weekdays to that date. Plus we want to make a query to a holidays table, and add a day for any holiday that falls between the first date and the added weekdays.
Does that sum it up?
As I understand the problem, there is a known "first date" and we want to add some number of weekdays to that date. Plus we want to make a query to a holidays table, and add a day for any holiday that falls between the first date and the added weekdays.
Does that sum it up?
The project Start date is 7-15-13 Task one has a duration of 5 days, which should set deadline date to 7-22-13 Task two has a duration of 5 days, which should set deadline date to 7-19-13 Task three has a duration of 3 days which should set my deadline date to 7-24-13.These numbers don't make sense - are there some typo's here?
@julianH: I think the author is talking about "weekdays" which is a term of art in the input to strtotime() -- weekday arithmetic skips the weekend days Saturday and Sunday. But the numbers still do not add up, and that's why I think our author should deconstruct the example into something that will allow for clear thinking.
From July 15 to July 22 (inclusive) is six weekdays, not five. So maybe the author means that Task One starts on the day following the day it is assigned (?). From July 15 to July 19 (inclusive) is five weekdays, which seems to fit with Task Two. If you start Task Three on July 22, the first weekday after completion of Task Two, and you use inclusive days, you get to July 24.
From July 15 to July 22 (inclusive) is six weekdays, not five. So maybe the author means that Task One starts on the day following the day it is assigned (?). From July 15 to July 19 (inclusive) is five weekdays, which seems to fit with Task Two. If you start Task Three on July 22, the first weekday after completion of Task Two, and you use inclusive days, you get to July 24.
<?php // RAY_temp_julianh.php
error_reporting(E_ALL);
echo '<pre>';
$p = 'Y-m-d D';
$alpha = date($p, strtotime('July 15, 2013'));
$omega = date($p, strtotime('July 31, 2013'));
$n = 0;
while ($alpha < $omega)
{
$n++;
echo PHP_EOL . "$alpha IS DAY NUMBER $n";
$alpha = date($p, strtotime("$alpha + 1 DAY"));
}
@Ray - I understood all that but the numbers don't work so I am assuming typos
I was asking for clarification.
If projects are made up of tasks that follow each other and if are only weekdays then we have 5 + 5+ 3 on top of 15 July - which puts us at 1 August.
Just need to understand the requirement which means data needs to be good.
I was asking for clarification.
If projects are made up of tasks that follow each other and if are only weekdays then we have 5 + 5+ 3 on top of 15 July - which puts us at 1 August.
Just need to understand the requirement which means data needs to be good.
ASKER
If the project is set to start on July 15th, the first task would also be set on that day.
Task One starts on the 15th and scheduled for 5 days, which should end on the 19th.
Task Two should therefore start on the 22nd which is scheduled for 5 days which should end on the 26th
Task Three would start then on 29th and will last for three days which should end on the 31st of July.
I am skipping weekends and when a deadline date is hit, the task would always start the following weekday.
Task One starts on the 15th and scheduled for 5 days, which should end on the 19th.
Task Two should therefore start on the 22nd which is scheduled for 5 days which should end on the 26th
Task Three would start then on 29th and will last for three days which should end on the 31st of July.
I am skipping weekends and when a deadline date is hit, the task would always start the following weekday.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Another perfect solution provided. Thank you so much!
Thanks for the points -- it's a great question! ~Ray
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
best regards, ~Ray