将MySQL表的元素与PHP结合使用

时间:2022-03-01 01:02:43

I've got a difficult task of displaying hours for various places. I would like to combine the instances where the hours start one evening and end the following morning. Anyone know how I might be able to modify the script below to get this to work properly?

我有一个艰难的任务,显示各个地方的时间。我想结合一个小时从一个晚上开始到第二天早上结束的情况。任何人都知道如何修改下面的脚本以使其正常工作?

all results with the same place_id should be combined where possible, the time_group_id doesn't have any significance to this. If the time ends at 11:59 then I would like to assume that it is continuous into the next morning.

应尽可能组合具有相同place_id的所有结果,time_group_id对此没有任何意义。如果时间在11:59结束,那么我想假设它连续到第二天早上。

My current output from the table like this:

我当前从表中输出的内容如下:

Mon - Thu: 11:00pm to 11:59pm

周一至周四:晚上11点至晚上11:59

Tue - Fri: 12:00am to 2:00am

星期二 - 星期五:凌晨12:00至凌晨2:00

I would like it to look like this:

我希望它看起来像这样:

Mon - Thu: 11:00pm to 2:00am

周一至周四:晚上11:00至凌晨2:00

I have a MySQL table that looks like this:

我有一个MySQL表,看起来像这样:

将MySQL表的元素与PHP结合使用

My PHP script:

我的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 />';
}  

?>

1 个解决方案

#1


0  

you might want to think about simply adding another column for the "end"-day to ease your life. this would even offer the possibility of going from mo 01:00 to mo 00:55 in one data set

您可能想要考虑简单地为“结束”日添加另一列以减轻您的生活。这甚至可以提供从一个数据集中的mo 01:00到mo 00:55的可能性

(and mysql_* is deprecated. use pdo)

(并且不推荐使用mysql_ *。使用pdo)

#1


0  

you might want to think about simply adding another column for the "end"-day to ease your life. this would even offer the possibility of going from mo 01:00 to mo 00:55 in one data set

您可能想要考虑简单地为“结束”日添加另一列以减轻您的生活。这甚至可以提供从一个数据集中的mo 01:00到mo 00:55的可能性

(and mysql_* is deprecated. use pdo)

(并且不推荐使用mysql_ *。使用pdo)