M
M
MikMik2018-03-19 09:35:03
PHP
MikMik, 2018-03-19 09:35:03

How to multiply time by a number?

It seems like a simple topic, but I can’t figure it out ...
The database contains data in the time format. It is required to take this data, multiply it by an integer and output/save in the format time (H:i:s)
Tried like this
tTime = date('H:i:s', StrToTime($dTime) * 5);
but for some reason if for example dTime=00:01:00 then the result is 20:05:00

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander null, 2018-03-19
@snikes

You need to multiply the time by 5! O_O
Maybe minutes, hours or something else?
multiplying time() you multiply by years months days, hours, minutes and seconds, it is quite logical that at the output you will get different times even with a difference of 1 second!
For multiplication use for example mktime

M
MikMik, 2018-03-19
@MikMik

I don't know if I did it right...

private function timeToSecond($time)
    {
        $part = explode(':', $time);
        $second = $part[0]*3600 + $part[1]*60 + $part[2];
        return $second;
    }
    
    private function secondToTime($second)
    {
        $hours = intdiv($second, 3600);
        $minutes = intdiv(($second - $hours*3600), 60);
        $seconds = $second - $hours*3600 - $minutes*60;
        
        return date('H:i:s', mktime($hours,$minutes,$seconds));
    }

    tTime = self::secondToTime(self::timeToSecond($dTime) * 5)

Can someone correct, tell me an easier way?
I thought for such simple actions in PHP there are ready-made methods. Maybe you just didn't find it? )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question