合并一系列天数的部分

时间:2013-03-15 08:47:53

标签: php mysql arrays foreach

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

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

enter image description here

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

星期一 - 星期四:晚上11点到晚上11:59

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

我希望它看起来像这样:

<星期一 - 星期四:晚上11点到凌晨2点

这是我当前的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点到晚上11:59

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

2 个答案:

答案 0 :(得分: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 />';

答案 1 :(得分:0)

查看PHP提供的Date and Time函数和类。它会让你的生活比试图自己重新创建这个功能容易得多。

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