S
S
Stepan Gervik2019-11-06 18:30:14
PHP
Stepan Gervik, 2019-11-06 18:30:14

How to convert numeric representation of minutes to text?

Hello! Please tell me, maybe there is some solution on the git or somewhere else?
It is necessary, for example, to convert 123 minutes into the string "2 hours 3 minutes", but at the same time, so that everything is normal with declensions (that is, I could cram different numbers there, and at the same time everything would look normal from the point of view of the Russian language)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Vanyushin, 2019-11-18
@HiLevel

I will offer my library, which does just what you need: https://github.com/wapmorgan/Morphos
After installing via composer, you can use the \morphos\Russian\TimeSpeller::spellDifference method :

var_dump(\morphos\Russian\TimeSpeller::spellDifference('+123 minute'));
// либо unix timestamp
var_dump(\morphos\Russian\TimeSpeller::spellDifference(time() + 123*60+1));
// +1 нужно потому, что во время парсинга эта секунда "съедается"

Both give the result: string(25) "2 hours 3 minutes"

R
Ramzesh Halifionakis, 2019-11-07
@ramiloremispum

I passed the value through this function.

function test($value)
    {
        $ost = $value % 10;
        if ($value >= 5 && $value <= 20) {
            $str = 'часов';
        } else if ($ost == 2 || $ost == 3 || $ost == 4) {
            $str = 'часа';
        } else if ($ost == 1) {
            $str = 'час';
        } else {
            $str = 'часов';
        }
        return $value . ' ' . $str;
    }

For minutes the same logic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question