Jump to content

since last visit


martind
 Share

Recommended Posts

The first part of doing this would be to determine what you want your starting date to be. You could do this by setting a cookie on every pageview to track the time they last viewed it:

Setting and getting the date and time of the last visit

<?php

// see when their last visit was from a cookie that we set
if(isset($_COOKIE['lastVisit'])) {

    // get the last visit time from the cookie
    $lastVisit = (int) $_COOKIE['lastVisit'];

    // if it was more than 30 days ago, then just assume 30 days
    $minLastVisit = strtotime("-30 days"); 
    if($lastVisit < $minLastVisit) $lastVisit = $minLastVisit;  

} else {

    // no last visit, so assume 7 days ago 
    $lastVisit = strtotime("-7 days"); 
}

// make a nice formatted date we can use for output later, i.e. October 1, 2011 5:00 am
$lastVisitStr = date("F j, Y H:i a", $lastVisit); 

// set a cookie with the time of this request, and the cookie expires in 30 days
setcookie('lastVisit', time(), strtotime('+30 days')); 

OR, if you wanted to just use a specific date, then you could just do this instead:

<?php
$lastVisit = strtotime("October 1, 2011 5:00 am"); 
$lastVisitStr = date("F j, Y H:i a", $lastVisit); // formatted version for output

Displaying pages modified since the last visit

Once you know your starting date/time (lastVisit), then you can use that to find pages that have been modified since that time:

<?php

// find the pages that have been modified, and sort by that
$modifiedPages = $pages->find("modified>=$lastVisit, sort=-modified, limit=50");

if(count($modifiedPages)) echo "<h2>Pages modified since $lastVisitStr</h2>";

// loop through the pages and display them
foreach($modifiedPages as $p) {
        $modified = date('F j, Y H:i a', $p->modified); // i.e. October 1, 2011 5:00 am
        echo "<p><strong><a href='{$p->url}'>{$p->path}</a></strong> last modified $modified</p>";
}

If you instead wanted to use the created date rather than the modified date, then you would just replace every instance of the word 'modified' with 'created' above. And likewise you can substitute any other date fields you might add to your page.

Displaying new comments since the last visit

When it comes to comments, we'll use a similar approach. But we first find the pages that have new comments, and then find the new comments on those pages. Since this is more of a specific purpose, we're not using PW's built in comments render functions and instead doing it ourselves here. Also, we'll assume that you already have that $lastVisit variable set from the first code example.

<?php

// first find the pages that have new comments on them, sort by the comment created date
$commentPages = $pages->find("comments.created>=$lastVisit, sort=-comments.created, limit=50"); 

// output a header that uses the $lastVisitStr var we set in the previous code example
if(count($commentPages)) echo "<h2>Comments posted since $lastVisitStr</h2>"; 

// loop through the pages that have new comments
foreach($commentPages as $p) {

        // find the new comments on this page that have an approved status
        // status=1 means approved (i.e. not spam or pending approval)
        $comments = $p->comments->find("created>=$lastVisit, sort=-created, status=1"); 

        // find out how many there are. if none, then move to next in loop
        $total = count($comments); 
        if(!$total) continue; 

        // output the page that had the comments with a link to it
        echo "<h3><a href='{$p->url}'>$total new comment(s) found at {$p->path}</a></h3>";

        // display the comments
        foreach($comments as $c) {

                $cite = htmlentities($c->cite); // prep what we need for output
                $text = nl2br(htmlentities($c->text, ENT_QUOTES, "UTF-8")); 
                $created = date('F j, Y H:i a', $c->created); 

                echo "<p class='comment'>";
                echo "<strong>Posted by $cite on $created:</strong>";
                echo "<blockquote>$text</blockquote>";           
                echo "</p>";
        }
}

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...