-1

I have a database that has the startTime for a task, the endTime, and how often it runs (everyMinutes).

I am taking this data into php, and using a loop writing all expected times to a separate table. The problem is when a task starts at 00:00:00 and ends at 23:59:00. Functionally this means it runs all day. If everyMinutes was 15, then it would start at 00:00:00 and then run again at 00:00:15.

The problem with my code below is that $latestRunTime will be 23:45:00 which is less than $endTime so it iterates again. However, instead of being larger than $endTime at the end of the loop, it is set back to 00:00:00 and I am stuck in an infinite loop. How can I mitigate this?

$getTaskDetailQuery = "SELECT taskID, startTime, endTime, everyMinutes FROM frequencyTable WHERE everyMinutes != '00:00:00' ORDER BY taskID ASC";
$stmt = sqlsrv_query($conn, $getTaskDetailQuery);
if($stmt === FALSE){
    die(print_r(sqlsrv_errors(), true));
}
$insertQueryMulti = "INSERT INTO exactTimeScheduleTable (taskID, expectedTime) VALUES (?, ?)";
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
    $taskID = $row['taskID'];
    $startTimeRaw = $row['startTime'];
    $endTimeRaw = $row['endTime'];
    $everyMinutesRaw = $row['everyMinutes'];
    $startTime = $startTimeRaw->format('H:i:s');
    $endTime = $endTimeRaw->format('H:i:s');
    $everyMinutes = $everyMinutesRaw->format('H:i:s');
    $secs = strtotime($everyMinutes)-strtotime("00:00:00");
    $latestRunTime = date("H:i:s",strtotime($startTime));
    while ($latestRunTime < $endTime){
        $timeToInsertRaw = strtotime($latestRunTime);
        $dataForInsert = array($taskID,$latestRunTime);
        $execute = sqlsrv_query($conn, $insertQueryMulti, $dataForInsert);
        $latestRunTime = date("H:i:s",strtotime($latestRunTime)+$secs);
    }
}
4

2 回答 2

2

just add this as the last line inside your while loop:

if($latestRunTime == "00:00:00") break;
于 2013-04-23T15:48:02.197 回答
0

You're currently checking wether the last run operation was within the accepted time limit, just change it to check if the next run operation is within the accepted time limit.

while (strtotime($latestRunTime)+$secs < $endTime);
于 2013-04-23T15:36:32.747 回答