首页 > 解决方案 > 如何在不使用 DATETIME 的情况下计算从 phpMyadmin 检索的时间差值?

问题描述

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

这是酒店管理系统输入值界面。这是phpMyadmin 中涉及的数据库

如果客户在中午 12:00 之后出院,他们将在 12:00到 phpMyadmin 数据库中的time_out 之间每小时罚款 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。但我相信还有其他方法可以解决这个问题。

标签: phptimehourcalc

解决方案


退房延迟按小时计费 下面的代码似乎建议了一种比您的方法更灵活的方法。希望能帮助到你。

$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>";

推荐阅读