Skip to content

Instantly share code, notes, and snippets.

@abranhe
Forked from mynameispj/Props
Created June 23, 2020 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abranhe/11483e621fea81b847c070f02896f796 to your computer and use it in GitHub Desktop.
Save abranhe/11483e621fea81b847c070f02896f796 to your computer and use it in GitHub Desktop.
Estimated reading time in PHP, by Brian Cray
In Drupal 7:
<?php
$postContent = render($content);
$word = str_word_count(strip_tags($postContent));
$m = floor($word / 200);
$s = floor($word % 200 / (200 / 60));
$est = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's');
?>
<p>Estimated reading time: <?php echo $est; ?></p>
Brian Cray's original, for Wordpress:
<?php
$mycontent = $post->post_content; // wordpress users only
$word = str_word_count(strip_tags($mycontent));
$m = floor($word / 200);
$s = floor($word % 200 / (200 / 60));
$est = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's');
?>
<p>Estimated reading time: <?php echo $est; ?></p>
Total props to Brian Cray: http://briancray.com/posts/estimated-reading-time-web-design/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment