midesweb / reading-time
Reading time PHP class calculator
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/midesweb/reading-time
Requires
- php: >=8.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.75
- phpunit/phpunit: ^10.0
README
EstimatedReadingTime is a lightweight PHP utility that calculates the estimated reading time (in minutes) for a given block of HTML or plain text content.
It uses a simple heuristic: an average reading speed of 120 words per minute. It strips HTML tags from the content before counting the words and rounds the result to the nearest full minute (rounding up if the leftover seconds exceed 30 seconds).
โ Features
- Automatically strips HTML tags before processing
- Calculates reading time based on word count
- Easy to update the content dynamically
- Supports human-readable output in English (
EN) and Spanish (ES)
๐ Installation
composer require midesweb/reading-time
๐งฑ Basic Example
use Midesweb\ReadingTime\EstimatedReadingTime; $content = "<p>This is a sample article with some <strong>HTML</strong> content.</p>"; $estimator = new EstimatedReadingTime($content); $minutes = $estimator->getEstimatedReadingMinutes(); echo "Estimated reading time: {$minutes} minute(s)";
๐ Updating the content
$estimator->updateContent("New content goes here..."); $minutes = $estimator->getEstimatedReadingMinutes();
๐ Localized Read Time Output
You can get a localized string describing the estimated read time using getReadTime().
The default language is English (EN), but you can switch to Spanish (ES) using setLanguage():
$estimator = new EstimatedReadingTime($content); // English (default) echo $estimator->getReadTime(); // Output: "1 minute", "3 minutes", "1 hour and 5 minutes", etc. // Spanish echo $estimator->setLanguage('es')->getReadTime(); // Output: "1 minuto", "3 minutos", "1 hora y 5 minutos", etc.
๐ Only
ENandESare supported. Invalid languages will throw anInvalidArgumentException.
๐งช How it works
- Strips HTML tags from the input.
- Counts words using
str_word_count(). - Divides by 120 (average words per minute).
- Rounds up the result if more than 30 seconds remain.
- Optionally returns a localized human-readable string.
๐ก Requirements
- PHP 7.4 or higher