0

I am using Laravels PHP Framework and i ran into the following problem.

i have got the following array:

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" } } 

and i tried to get the 'createdAt' of both arrays, and check if they are 3 days in the past or not.

The above array is a print_r of $DQComments When implementing it i use the following code, but i would like the '$past' to be before the 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">&nbsp;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';
    }
}

How can i use the 'filter' of checking if the comment is -3 days or more before the foreach?? I know it's not the normal way of doing stuff around here the way i am doing now. But i have checked the internet and SO all over and couldnt find what i was looking for. Or i used the wrong search term.. If this is a duplicate, please let me know :)

3
  • 2
    Is there some reason why you can't simply select the comments from your DB that are 3 days old? Commented Jul 28, 2014 at 11:27
  • What do you want to achieve? I would start with looking at @msturdy 's suggestion. Then depending on what you want, if you want to not show the old comments, you can just do (in the foreach) before you do other logic, check if the comment is more than 3 days aga, if so, use continue. Commented Jul 28, 2014 at 11:30
  • the comments arent stored in my DB, they come from Disqus. Commented Jul 28, 2014 at 11:47

1 Answer 1

2

In any way you would iterate the array, so maybe there is no need to previously filter your array... Maybe you could improve just a little bit like this:

$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">&nbsp;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';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Minor change to calc of $henk
Thanks mate! This simplifies a lot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.