如何计算日期范围内的特定日期和时间

时间:2014-04-04 05:34:36

标签: php mysql sql date pseudocode

我有一个数据库,其中包含员工的登录和注销时间表。

我正在寻找在星期日早上6点到星期四凌晨4点或星期六早上6:30和星期六下午3点之间开启或关闭的工作人员。

我希望最终用户能够从网络界面(即1/1/14开始和12/1/14结束)中选择任何日期范围,并从此计算出在白天和时间范围内工作的人。 我希望能够显示工作人员列表及其登录和注销时间。

到目前为止,我已经研究了如何使用Determine Whether Two Date Ranges Overlap

检查用户是否已登录

但是我对如何计算daterangeB

的伪代码感到难过 到目前为止,我有:

start at datefrom,
check if datefrom = dateto, if true end loop,
find out day name, 
check if saturday, if saturday add timestamps to array (start and finish),
increment datefrom by 1 day, start loop again.

这将给我一个星期六日期范围B的数组,在用户选择的日期之间。

然而,更大的日期范围是一个问题,因为它可能是第一天说是星期二,如果我正在检查星期日和星期四,说我找到星期四,我将不会有开始日星期天,我需要在星期二00:00:00。同样相反的情况可能发生,我可以找到一个星期日,用户选择的最后一天可能是星期一,因此我需要将结束日期设为daterangeB

start at datefrom,
check if datefrom = dateto, if true end loop,
find out day name, 
check if Sunday, if Sunday add timestamp to array (start),
increment datefrom by 1 day, start loop again.

start at datefrom,
check if datefrom = dateto, if true end loop,
find out day name, 
check if Thursday, if Thursday add timestamps to array (start and finish),
increment datefrom by 1 day, start loop again.

1 个答案:

答案 0 :(得分:0)

所以对于所有感兴趣的人来说,这是我最终做的一个愚蠢的版本:

$stcars = array();

// Create connection
try {
    $db = new PDO('mysql:host=localhost;dbname=Reports;charset=utf8', 'XXX', 'XXX');

    $db->query('hi'); //invalid query!
} catch(PDOException $ex) {
    echo "An Error occured!" . $ex->getMessage(); //user friendly message
}

    $stmt = $db->prepare('SELECT CarNumber FROM STcars WHERE CarNumber > 0');


    if ($stmt->execute()){

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

array_push($stcars, $row[CarNumber]);


}// end while

}// end if

$DateFrom = $_POST["DateFrom"];
$DateTo = $_POST["DateTo"];

$DateToArray = date('m/d/Y', strtotime($DateTo . ' + 1 day')); 


$period = new DatePeriod(
     new DateTime($DateFrom),
     new DateInterval('P1D'),
     new DateTime($DateToArray)
);

foreach($period as $date) 
{

    $daterange[] = $date->format('m/d/Y');

}

//  MS SQL Server with PDO_DBLIB
$DBH = new PDO("dblib:host=$myServer;dbname=$myDB", $myUser, $myPass);

for ($i = 0; $i < count($stcars); ++$i) {

$CarNumber = $stcars[$i];

$STH3 = $DBH->query("SELECT dbo.Driver.DriverNumber, dbo.Driver.DriverName, dbo.DriverLogon.LogonTime, dbo.DriverLogon.LogoffTime, dbo.Vehicle.RegistrationNumber, dbo.OwnerOperator.OOName FROM dbo.Driver INNER JOIN dbo.DriverLogon ON dbo.Driver.DriverID = dbo.DriverLogon.DriverID INNER JOIN dbo.Vehicle ON dbo.DriverLogon.VehicleID = dbo.Vehicle.VehicleID INNER JOIN dbo.OwnerOperator ON dbo.Vehicle.OperatorID = dbo.OwnerOperator.OwnerOperatorID WHERE dbo.Vehicle.CarNumber = '$CarNumber' AND dbo.DriverLogon.LogonTime >= '$DateFrom $Beginning' AND dbo.DriverLogon.LogoffTime <= '$DateTo $End' AND dbo.Driver.DriverNumber != '7777' AND dbo.Driver.DriverNumber != '83890';");

$STH3->setFetchMode(PDO::FETCH_OBJ);

while($row3 = $STH3->fetch()) {

$logonday = date("D", strtotime($row3->LogonTime));

$chkStartTime = strtotime($row3->LogonTime);
$chkEndTime   = strtotime($row3->LogoffTime);
$Tplate = $row3->RegistrationNumber;
$Operator = $row3->OOName;
$logontime = gmdate("D d/m/Y H:i:s", strtotime($row3->LogonTime));
$logofftime = gmdate("D d/m/Y H:i:s", strtotime($row3->LogoffTime));




for ($e = 0; $e < count($daterange); ++$e) {
       $Date = $daterange[$e];
$day = date("D", strtotime($Date)); 




if(strpos( $day, $Mon ) !== FALSE){


$startTime = strtotime($Date . " " . $Beginning);
$endTime   = strtotime($Date . " " . $End);



if($chkStartTime >= $endTime || $chkEndTime <= $startTime){}else
{   
echo "<tr>" ;
echo "<td>" . $day .  "</td> ";
echo "<td>" . $row3->DriverNumber .  "</td> ";
echo "<td>" . $row3->DriverName .  "</td> ";
echo "<td>" . $Operator .  "</td> ";
echo "<td>" . $CarNumber .  "</td> ";
echo "<td>" . $Tplate .  "</td> ";
echo "<td>" . $logontime   .  "</td> ";
echo "<td>" . $logofftime   .  "</td> ";
//echo "<td>" . "Logon after Time End or Logoff before Time Start" .  "</td> ";
//echo "<td>" . gmdate("D d/m/Y G:i:s", strtotime($Date . " " . $Beginning))   . "</td> ";
//echo "<td>" . gmdate("D d/m/Y G:i:s", strtotime($Date . " " . $End))   .  "</td> ";
 echo "</tr>" ;

$list1 ="$day, $row3->DriverNumber, $row3->DriverName, $Operator, $CarNumber, $Tplate, $logontime, $logofftime " . "\n";

fwrite($handle, $list1);

}