"Time to read" average for a blog post

When I started building this blogging application, I did some research on existing platforms. One thing I particularly liked was the time to read feature: an estimate on how long a post would take to read.

It turns out there are all kinds of statistics that can provide you with an estimate. I decided to go with a simple one: count the number of words and divide it by an average "words per minute" number. According to Wikipedia, people are able to read about 180 to 200 words per minute, depending on the medium.

With this knowledge, I whipped up a simple model accessor, providing me with the average time for each post:

public class Post extends Model {

    // ....

    public function getMinutesToReadAttribute() {
        $wordsPerMinute = 200;

        $wordCount = str_word_count(strip_tags($this->text));

        return ceil($wordCount / $wordsPerMinute);
    }
}

And voilĂ , with only a few lines of code, there was my time to read average.

Clicky