Skip to content

Instantly share code, notes, and snippets.

@dana-ross
Created June 14, 2016 19:18
Show Gist options
  • Save dana-ross/9719e94812125685b90eb88587cde69b to your computer and use it in GitHub Desktop.
Save dana-ross/9719e94812125685b90eb88587cde69b to your computer and use it in GitHub Desktop.
Reading time calculation from one of my WordPress plugins. MIT licensed. Use as you see fit.
<?php
/**
* Calculate the reading time for a piece of content
*
* @param string $content
* @param integer $wpm Words Per Minute reading speed
*
* @return array minutes => integer, seconds => integer
*/
private static function calc_reading_time( $content, $wpm = 250 ) {
$wpm = absint( $wpm );
$word = str_word_count( strip_tags( $content ) );
$time = array();
$time['minutes'] = floor( $word / $wpm );
$time['seconds'] = floor( $word % $wpm / ( $wpm / 60 ) );
return $time;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment