我正在使用Laravels PHP Framework,我遇到了以下问题。
我有以下数组:
array(2) {
[0]=> array(7) {
["createdAt"]=> string(19) "2014-07-24T10:49:49"
["message"]=> string(103) "Helaas geldt dit niet voor status updates, foto's en video's. Er is wel een trucje die ik zelf altij..."
["pageTitle"]=> string(45) "[Facebook Update] Facebook Introduceert Save!"
["pageURL"]=> string(78) "mywebsiteurl"
["authorName"]=> string(16) "Parsifal Tritsch"
["authorProfileURL"]=> string(34) "http://disqus.com/parsifaltritsch/"
["authorAvatar"]=> string(66) "avatarurl" }
[1]=> array(7) {
["createdAt"]=> string(19) "2014-07-22T22:04:30"
["message"]=> string(103) "Ja, dat vind ik een goed idee! Ik heb vaak al naar 'omwegen' gezocht om iets later te kunnen bekijke..."
["pageTitle"]=> string(45) "[Facebook Update] Facebook Introduceert Save!"
["pageURL"]=> string(78) "mywebsiteurl"
["authorName"]=> string(8) "Marjanne"
["authorProfileURL"]=> string(0) ""
["authorAvatar"]=> string(50) "avatarurl" } }
我尝试获取两个数组的'createdAt',并检查它们是否过去3天。
上面的数组是$ DQComments的print_r 在实现它时,我使用以下代码,但我希望'$ past'在foreach之前。
foreach($DQComments as $comment){
$henk = date(substr($comment['createdAt'], 0, 10));
$jan = date("d-m-Y", strtotime($henk));
$commentdatum = strtotime($jan);
$past = strtotime(date("d-m-Y", strtotime("3 days ago")));
if ($commentdatum >= $verleden) {
echo '<li class="comment-blok col-12">';
echo '<div class="col-12">';
echo '<img src="'.$comment['authorAvatar'].'" class="comment-author-profile-img pull-left">';
echo '<span class="comment-author pull-left">'.'<a href="'.$comment['authorProfileURL'].'" class="comment-author">'.$comment['authorName'].'</a></span>'.'<span class="created_at"> om: '.$jan.'</span>';
echo '</div>';
echo '<div class="col-12">';
echo '<div class="comment-message pull-left">'.$comment['message'].'</div>';
echo '</div>';
echo '<div class="col-12 pull-left">';
echo '<a class="comment-link pull-left" href="'.$comment['pageURL'].'">Bekijk bericht</a>';
echo '</div>';
echo '</li>';
} else {
echo 'There are no new comments';
}
}
如何在foreach之前使用“过滤器”检查注释是否为-3天或更长时间?我知道这不是我现在这样做的正常做法。但我已经检查了互联网和所有的东西,无法找到我想要的东西。或者我使用了错误的搜索词。如果这是重复的,请告诉我:))
答案 0 :(得分:2)
你会以任何方式迭代数组,所以也许没有必要先过滤你的数组......也许你可以改善这样一点:
$past = strtotime("3 days ago");
$numComments = 0;
foreach($DQComments as $comment){
$henk = strtotime(substr($comment['createdAt'], 0, 10));
if ($henk >= $past) {
echo '<li class="comment-blok col-12">';
echo '<div class="col-12">';
echo '<img src="'.$comment['authorAvatar'].'" class="comment-author-profile-img pull-left">';
echo '<span class="comment-author pull-left">'.'<a href="'.$comment['authorProfileURL'].'" class="comment-author">'.$comment['authorName'].'</a></span>'.'<span class="created_at"> om: '.$jan.'</span>';
echo '</div>';
echo '<div class="col-12">';
echo '<div class="comment-message pull-left">'.$comment['message'].'</div>';
echo '</div>';
echo '<div class="col-12 pull-left">';
echo '<a class="comment-link pull-left" href="'.$comment['pageURL'].'">Bekijk bericht</a>';
echo '</div>';
echo '</li>';
$numComments ++;
}
}
if($numComments == 0) {
echo 'There are no new comments';
}