php - 如何在不使用 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。但我相信还有其他方法可以解决这个问题。
解决方案
退房延迟按小时计费 下面的代码似乎建议了一种比您的方法更灵活的方法。希望能帮助到你。
$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>";
推荐阅读
- user-agent - 如何区分 QT WebKit 用户代理字符串与主流浏览器?
- python - 在 Python 中的列表列表中连接元素
- python - 将 2*2 像素图像(每个图像由 1 x 4 numpy 向量给出)重新排列为单个 8 x 8 矩阵,而不使用 for 循环
- arrays - 为什么我的 MATLAB 代码会重复除空格之外的所有符号,但会删除这些符号?
- javascript - 为什么人们在关闭后尝试重新连接 Websockets?
- c++ - 如何将 std::string 转换为 L 数据类型
- php - 有时()条件验证中的验证规则数组
- r - 在 r 中从数据库中的抓取字符串转换整数
- python - 我正在使用 python 和 opencv 拍摄图像;但是,图像质量未显示正确尺寸的图像
- python - 为超时 UDP 连接创建一个循环