如何在不使用DATETIME的情况下计算从phpMyadmin检索到的差异时间值?

如何在不使用DATETIME的情况下计算从phpMyadmin检索到的差异时间值?,php,time,hour,calc,Php,Time,Hour,Calc,我做了一些搜索,有很多关于计算两次时间差的信息。但我不确定为什么我的代码不起作用 这是酒店管理系统。 这是涉及的问题 如果客户在中午12:00后出院,他们将在12:00到下班时间之间每小时罚款50.00美元。这是我计算总费用的代码。我对计算日期的持续时间没有问题 //discharge $dateout = date_create($_POST['dateout']); $timeout = $_POST['timeout']; //book $datein = date_create($_P

我做了一些搜索,有很多关于计算两次时间差的信息。但我不确定为什么我的代码不起作用

这是酒店管理系统。 这是涉及的问题

如果客户在中午12:00后出院,他们将在12:00到下班时间之间每小时罚款50.00美元。这是我计算总费用的代码。我对计算日期的持续时间没有问题

//discharge
$dateout = date_create($_POST['dateout']);
$timeout = $_POST['timeout'];

//book
$datein = date_create($_POST['datein']);
$timein = $_POST['timein'];

//calc duration
$duration = date_diff($datein,$dateout);
$days = $duration->format("%a");
但是,我对时间计算有问题

if($timeout >= "12:00")
{
    $overtime = strtotime("12:00");
    $time = strtotime($timeout);
    
    $diff = $time - $overtime;
    $diffhour = $diff->format("%H");//in hour
                    
    $fine= 50;//$50.00
    $charge = $fine*$diffhour;
    
    if($row["bed_type"] == "3 person room")
    {
        $price = 175;
    }
    else if($row["bed_type"] == "1 person room")
    {
        $price = 280;
    }
}
else if($timeout < "12:00")
{
    $overtime = strtotime("12:00");
    $time = strtotime($timeout);
    
    $diff = $time - $overtime;
    $diffhour = $diff->format("%H");//in hour
    
    $fine= 0;//$0
    $charge = $fine*$diffhour;
    
    if($row["bed_type"] == "3 person room")
    {
        $price = 175;
    }
    else if($row["bed_type"] == "1 person room")
    {
        $price = 280;
    }
}
$totalprice = ($days*$price)+$charge;
echo $totalprice."<br>";

互联网上的大部分信息来源都是DATETIME。但我相信还有其他方法可以解决这个问题。

查看延迟小时计费 下面的代码似乎提出了一种比您更灵活的方法。 希望能有帮助

$OverTimeStandardTime   =   strtotime( "12:00" );
$nowTime                =   time(); // unixtime

$OverTimeExist          =   $nowTime - $OverTimeStandardTime;
$addCharge              =   0;
if( $OverTimeExist > 0 )
{
    $addCharge  =   $addCharge + ( 50 * ceil( $OverTimeExist / ( 60 * 60 ) ) );
    //  1 hour is 3600 seconds per hour Additional charge processing
}

if($row["bed_type"] == "3 person room")
{
    $addCharge = $addCharge + 175;
}

if($row["bed_type"] == "1 person room")
{
    $addCharge = $addCharge + 280;
}

$totalprice = ($days*$price)+$addCharge;
echo $totalprice."<br>";

为什么您不想使用DateTime解决此问题的最常见、最尝试和最测试的方法?请始终将elseif作为php中的一个单词来支持。有关简化语法,请参见加法赋值运算符+=。