我有一项艰巨的任务是显示各个地方的小时数。我想结合时间从一个晚上开始到第二天早上结束的实例。任何人都知道我如何能够修改下面的脚本以使其正常工作?
应尽可能合并具有相同 place_id 的所有结果,time_group_id 对此没有任何意义。如果时间在 11:59 结束,那么我想假设它一直持续到第二天早上。
我当前的表格输出如下:
周一至周四:晚上 11:00 至晚上 11:59
周二至周五:凌晨 12:00 至凌晨 2:00
我希望它看起来像这样:
周一至周四:晚上 11:00 至凌晨 2:00
我有一个如下所示的 MySQL 表:
我的 PHP 脚本:
<?php
//connect
$day_times = array();
$days_map = array(
'monday' => 'Mon',
'tuesday' => 'Tue',
'wednesday' => 'Wed',
'thursday' => 'Thu',
'friday' => 'Fri',
'saturday' => 'Sat',
'sunday' => 'Sun'
);
$query = mysql_query("SELECT distinct day, time_group_id, start_time, end_time FROM times
WHERE place_id = '80' ORDER BY id ASC");
while ($row=mysql_fetch_assoc($query)) {
$start_time = stripslashes($row['start_time']);
$end_time = stripslashes($row['end_time']);
$start_time = strtotime("$start_time");
$end_time = strtotime("$end_time");
$day = strtolower($row['day']);
$day_times[$row['time_group_id']]['times'][] = $start_time;
$day_times[$row['time_group_id']]['times'][] = $end_time;
$day_times[$row['time_group_id']]['days'][] = $day;
}
foreach ($day_times as $timegroup) {
$first_day = array_shift($timegroup['days']);
$last_day = array_pop($timegroup['days']);
$times = $timegroup['times'];
echo $days_map[$first_day] . ' - ' . $days_map[$last_day] . ': ';
echo date('g:ia', min($times));
echo ' to ';
echo date('g:ia', max($times));
echo '<br />';
}
?>