我在做电视节目的日历时遇到了麻烦。
基本上,我想输出一周中的天数和每天播放的节目。(显然不需要执行7个不同的查询)
以下命令输出播放日期介于一周开始和结束之间的节目
$stmt = $conn->prepare("SELECT *
FROM show_episode_airdate, show_episode
WHERE show_episode_airdate.airdate BETWEEN :weekbeginning AND :weekend AND show_episode.episode_id = show_episode_airdate.episode_id ");
$stmt->execute(array(':weekbeginning' => $begin_date, ':weekend' => $end_date));
while($row = $stmt->fetch()) {
}
输出一周中的七个日期。
foreach ($listofdays as $num=>$jour) {
$date_table[$num] = date('m-d-Y',$weekbegin[0]+$num*DUREE_UN_JOUR);
$daysoftheweek = $date_table[$num];
var_dump($daysoftheweek);
}
输出如下内容:
string '05-20-2013' (length=10)
string '05-21-2013' (length=10)
string '05-22-2013' (length=10)
string '05-23-2013' (length=10)
string '05-24-2013' (length=10)
string '05-25-2013' (length=10)
string '05-26-2013' (length=10)
我不明白如何把这两件事结合起来达到我所追求的??!
这是我解决这个问题的作战计划:
使用$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
循环这些并将日期从其字符串格式转换为unix时间戳,可能通过使用explode
和mktime
:
foreach($rows as &$row) {
$unix_airdate= mktime(...TODO);
$row['unix_airdate']= $unix_airdate;
}
unset($row); //do not forget because $row was a reference
然后在您已经编写的循环中,使用http://www.php.net/manual/en/function.array-filter.php $filtered_rows= array_filter($rows, 'my_filter_function..TODO');
,以便my_filter_function检查unix_airdate是否介于weekbegin和下周开始之前。
现在遍历过滤后的数组并输出所有显示。如果希望按天进行聚合,则循环一周中的天数,并按day_begin和day_begin_next_day进行过滤。