</> Code The Pixel

PHP: Estimate Reading Time

Asyraf Wahi Anuar - June 20, 2020

Estimated reading time: 1 minute, 40 seconds

This tutorial will show how to create a function to estimate the time for reading some text on your web or application. Many websites have shown the estimated reading time for the users at the same time may be helpful for the users to know how long they need to finish the reading. 

This tutorial uses $mytext as the variable for the text as shown below:

<?php
    $mytext = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)';

    $words = str_word_count(strip_tags($mytext));
    $minutes = floor( $words / 120 );
    $seconds = floor( $words % 120 / ( 120 / 60 ) );

    if (1 <= $minutes) {
        $estimated_time = $minutes . ' minute' . ($minutes == 1 ? '' : 's') . ', ' . $seconds . ' second' . ($seconds == 1 ? '' : 's');
    } else {
        $estimated_time = $seconds . ' second' . ($seconds == 1 ? '' : 's');
    }

    echo $estimated_time;
?>


The output will be 1 minute, 37 seconds. If use CakePHP, the $mytext should be something like:

$mytext = $user->note;


That all. Happy coding :)


Cite this article (APA 6th Edition)

Popular
PHP: Estimate Reading Time
June, 20 2020
CakePHP 4 Print PDF Using CakePDF
May, 17 2020
CakePHP 4 Authentication Using Auth...
May, 14 2020
CakePHP 4 Sending Email
February, 01 2022
CakePHP 4 Export To CSV
May, 29 2020
CakePHP 4 jQuery Date Time Picker
October, 01 2018
CakePHP 4 Authentication Using...
May, 11 2020
Share
Sharing Link
Click the icon to share