0

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

我有一个如下所示的 MySQL 表:

在此处输入图像描述

我当前的 PHP 脚本从表中输出日期和时间,如下所示:

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

周一至周四:凌晨 12:00 至凌晨 2:00

我希望它看起来像这样:

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

这是我当前的 PHP 脚本:

<?php

//connection

$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 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']][$day][] = $start_time;
    $day_times[$row['time_group_id']][$day][] = $end_time;


// within your query while loop
$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 />';
} 

}

?>

目前得到这样的输出:

周一至周六:下午 4:00 至晚上 7:00

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

周一至周四:凌晨 12:00 至凌晨 2:00

4

2 回答 2

1

编辑:
我认为这可以在不重写表格以使您将结果分组在一起的情况下在代码中解决。目前您只有 time_group_id 作为某种分隔符。第一段和第二段代码将以这种方式对结果进行分组,但最后一段根本没有对它们进行分组。找到一个规则,您可以在表格中拆分结果,然后很容易根据需要显示它们。

第一的:

1) 停止使用 mysql_ 内置函数,它们将从较新版本的 PHP 中删除。

试试这样的脏程序代码:

$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 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']][$day][] = $start_time;
    $day_times[$row['time_group_id']][$day][] = $end_time;
}

这应该给你一个数组:

[time_group_id] => Array(
                      'monday' => Array(
                           '1123323444',
                           '4443543644',
                           '1123323444',
                           '2332133242',
                           '9782122333',
                           '3246556444'
                       ),
                       'tuesday' => Array(
                           '1123323444',
                           '4443543644',
                           '1123323444',
                           '2332133242',
                           '9782122333',
                           '3246556444'
                       // more days and times
[time_group_id] => Array(
                      'monday' => Array(
                           '1123323444',
                           '4443543644',
                           '1123323444',
                           '2332133242',
                           '9782122333',
                           '3246556444'
                       ),
                       'tuesday' => Array(
                           '1123323444',
                           '4443543644',
                           '1123323444',
                           '2332133242',
                           '9782122333',
                           '3246556444'
                       // more days and times


foreach ($day_times as $timegroup => $days) {
    foreach ($days as $day => $times) {
    echo $day .': ';
    echo date('g:ia', min($times));
    echo ' to ';
    echo date('g:ia', max($times));     
    echo '<br />';
    } 
}

这将为您提供每一天的开始和结束时间。因为您希望它们按连续天数分组:

// within your query while loop
$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 />';
}   

这假设您按连续顺序提取日期,例如周一、周二、周三、周四。

更新:

这整个块应该是你所需要的:

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

更新 2:

$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 time_group_id, start_time, end_time, day
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[] = $start_time
    $day_times[] = $end_time;
    $days[]      = $day;
}

$first_day = array_shift($days);
$last_day = array_pop($days);

echo $days_map[$first_day] . ' - ' . $days_map[$last_day] . ': ';
echo date('g:ia', min($day_times));
echo ' to ';
echo date('g:ia', max($day_times));    
echo '<br />';
于 2013-03-15T09:40:17.030 回答
0

看看 PHP 提供的日期和时间函数和类。这将使您的生活比尝试自己重新创建此功能要容易得多。

DateTime类非常智能,并且可以接受大多数表示日期和时间的字符串格式。如果你永远在 64 位处理器上或者不担心 2038 年strtotime()也是非常强大的。

于 2013-03-15T09:03:10.190 回答