我在处理电视节目时遇到了问题。
基本上,我想输出一周中的几天以及每天播放的节目。 (显然没有做 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 进行过滤。
关于php - 在 PHP 中关联两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16717795/