【问题标题】:How to calculate difference time value retrieved from phpMyadmin without using DATETIME?如何在不使用 DATETIME 的情况下计算从 phpMyadmin 检索的时间差值?
【发布时间】:2023-03-17 16:48:01
【问题描述】:

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

这是酒店管理系统input value interface。 这是涉案database in phpMyadmin

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

【问题讨论】:

  • 您为什么不想使用DateTime 使用最常见、经过验证的方法来解决此问题?

标签: php time hour calc


【解决方案1】:

退房延迟按小时计费 下面的代码似乎提出了一种比您的更灵活的方法。 希望对您有所帮助。

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

【讨论】:

  • 请始终将 elseif 作为 php 中的一个单词背书。请参阅加法赋值运算符+= 以了解简化的语法。
猜你喜欢
  • 1970-01-01
  • 2015-12-21
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
相关资源
最近更新 更多