Snippet · Mecha CMS

Code snippet collection for developers.

Estimated Reading Time

Author: Taufik Nurrohman · 353 Views

Tweet

Gives the information needed to make a quick decision on whether to commit to reading an article or not.

<?php

// <http://www.binarymoon.co.uk/2013/10/wordpress-estimated-reading-time>
if ($_page = Lot::get('page')) {
    $_words = str_word_count(To::text($_page->content));
    $_min = floor($_words / 120);
    $_sec = floor($_words % 120 / (120 / 60));
    $_min_text = $language->{$_min == 1 ? 'minute' : 'minutes'};
    $_sec_text = $language->{$_sec == 1 ? 'second' : 'seconds'};
    if ($_min >= 1) {
        $_text = $_min . ' ' . $_min_text . ', ' . $_sec . ' ' . $_sec_text;
    } else {
        $_text = $_sec . ' ' . $_sec_text;
    }
    echo '<p>Estimate time required to read this page: ' . $_text . '</p>';
}

?>

0 Comments

No comments yet.