
本文旨在提供一个php解决方案,用于准确判断某个地点(如餐厅)的开放状态,包括“开放”、“即将关闭”和“已关闭”三种情况。我们将探讨常见的逻辑错误,特别是条件判断的顺序和时间字符串处理,并展示如何通过封装函数和优化条件逻辑,实现一个健壮、可维护的时间状态检查系统。
在开发涉及时间条件判断的应用时,例如显示餐厅的开放状态,我们常常需要处理多种复杂的逻辑,例如“开放中”、“即将关闭”和“已关闭”。不正确的条件顺序或时间处理方式可能导致逻辑错误,使得系统无法按预期工作。本教程将深入探讨如何使用PHP有效地解决这类问题。
原始代码尝试通过一系列if-elseif语句来判断状态:
$current_time = date('H:i'); // 获取当前时间
$closed_soon = date("H:i", strtotime("-5 minutes", strtotime($row['ad_zamkniecie']))); // 关闭时间前5分钟
if($current_time >= date($row['ad_otwarcie']) && $current_time <= date($row['ad_zamkniecie'])) {
echo "The place (ex. restaurant) is open";
} elseif($current_time >= $closed_soon && $current_time <= date($row['ad_zamkniecie']) ) {
echo "The place (ex. restaurant) will close in 5 mins";
} elseif($current_time >= date($row['ad_zamkniecie'])) {
echo "The place (ex. restaurant) is closed";
}这段代码存在两个主要问题:
为了解决上述问题,我们可以采用以下策略:
立即学习“PHP免费学习笔记(深入)”;
下面是优化后的PHP代码实现:
<?php
/**
* 判断地点(如餐厅)的开放状态。
*
* @param string $open_time 开放时间,格式为 'H:i'
* @param string $close_time 关闭时间,格式为 'H:i'
* @param string|null $current_time 当前时间,格式为 'H:i'。如果为null,则使用当前系统时间。
* @return array 包含状态码和状态描述的数组,例如 ['open', 'The place is open']
*/
function place_open_status(string $open_time, string $close_time, ?string $current_time = null): array
{
// 如果未提供当前时间,则获取系统当前时间
if (is_null($current_time)) {
$current_time = date('H:i');
}
// 计算“即将关闭”的时间点(关闭时间前5分钟)
// 注意:strtotime 需要一个有效的日期时间字符串,所以这里将时间与一个虚拟日期结合
$closed_soon = date('H:i', strtotime('-5 minutes', strtotime('2000-01-01 ' . $close_time)));
// 1. 首先判断是否处于“开放中”但未进入“即将关闭”的窗口
// 这里的关键是 `current_time < $closed_soon`,排除了即将关闭的5分钟窗口
if ($current_time >= $open_time && $current_time < $closed_soon) {
return ['open', 'The place (ex. restaurant) is open'];
}
// 2. 接着判断是否处于“即将关闭”的窗口
// 条件是当前时间在 $closed_soon 和 $close_time 之间(不包含 $close_time)
if ($current_time >= $closed_soon && $current_time < $close_time) {
return ['closing_soon', 'The place (ex. restaurant) will close in 5 mins'];
}
// 3. 如果以上条件都不满足,则默认为“已关闭”
// 这包括了当前时间在开放时间之前,或者在关闭时间之后的情况
return ['closed', 'The place (ex. restaurant) is closed'];
}
// --- 示例用法 ---
$open_time_example = '09:00';
$close_time_example = '17:00';
echo '<h3>测试不同时间点的状态:</h3>';
echo '<ul>';
// 模拟开放时间
echo '<li>' . place_open_status($open_time_example, $close_time_example, '10:00')[1] . ' (预期: open)</li>';
// 模拟即将关闭时间
echo '<li>' . place_open_status($open_time_example, $close_time_example, '16:58')[1] . ' (预期: closing_soon)</li>';
// 模拟已关闭时间
echo '<li>' . place_open_status($open_time_example, $close_time_example, '17:24')[1] . ' (预期: closed)</li>';
// 模拟开放前时间
echo '<li>' . place_open_status($open_time_example, $close_time_example, '08:30')[1] . ' (预期: closed)</li>';
echo '</ul>';
// --- 数据库集成示例 ---
echo '<h3>数据库集成示例 (伪代码):</h3>';
// 假设你从数据库中获取了多行数据
$rows_from_db = [
['ad_otwarcie' => '09:00', 'ad_zamkniecie' => '17:00'], // 开放
['ad_otwarcie' => '09:00', 'ad_zamkniecie' => '17:00'], // 即将关闭 (假设当前时间是16:57)
['ad_otwarcie' => '09:00', 'ad_zamkniecie' => '17:00'], // 已关闭 (假设当前时间是17:05)
['ad_otwarcie' => '09:00', 'ad_zamkniecie' => '10:00'], // 已关闭 (假设当前时间是11:00)
];
// 模拟当前时间,以便测试不同场景
$simulated_current_times = ['10:00', '16:57', '17:05', '11:00'];
$i = 0;
foreach ($rows_from_db as $row) {
// 实际应用中,这里不需要传入 $simulated_current_times[$i],而是直接使用 place_open_status($row['ad_otwarcie'], $row['ad_zamkniecie']);
// 传入是为了演示不同场景
$status = place_open_status($row['ad_otwarcie'], $row['ad_zamkniecie'], $simulated_current_times[$i]);
echo '<div class="exampleclass ' . $status[0] . '">' . $status[1] . ' (模拟当前时间: ' . $simulated_current_times[$i] . ')</div>';
$i++;
}
?>通过将时间状态判断逻辑封装到独立的函数中,并仔细处理时间字符串的比较以及条件判断的顺序,我们可以构建一个清晰、准确且易于维护的系统。这种方法不仅解决了原始代码中的具体问题,也提供了一个处理类似时间条件逻辑的通用模式,强调了代码模块化和精确条件定义的重要性。在实际项目中,这种健壮的时间判断机制对于提升用户体验和系统准确性至关重要。
以上就是如何使用PHP准确判断地点开放状态(含即将关闭提醒)的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号