Created
June 14, 2016 19:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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