PHP multiply hour by integer value?

Asked

Viewed 962 times

0

How do I multiply hours in the PHP?

Example :

$time = "0:01:00";
$mult = "5";

echo $time*$mult;

The return was zero because it did not return 00:05:00?

  • 1

    It would not be better to work in a unit (minutes) and then convert the results?

  • convert and then convert... then easier to do by mysql select?

  • All you had to do was explain what $time is. A time interval (one minute) or a time interval (zero hour and one minute)?

  • this team would be a time of a task, how long it takes for an employee to do a certain task, so they have tasks that is a few minutes, others take days and others can reach weeks.

2 answers

7

You need to use the class DateTime, where there is the method modify so that you can modify your date according to your need.

$time="0:01:00";
$mult="5";
$datetime=DateTime::createFromFormat('H:i:s',$time,new DateTimeZone('America/Sao_Paulo'));
$datetime->modify('+' . $mult . ' minutes');
echo $datetime->format('H:i:s');

EDIT: In this case then, you can take the amount of minutes and multiply by the desired value, thus:

$time="00:01:00";
$mult=5;
$datetime=DateTime::createFromFormat('H:i:s',$time,new DateTimeZone('America/Sao_Paulo'));
$minute=$datetime->format('i');
$datetime->modify('+' . ($minute * $mult) . ' minutes');
$datetime->modify('-' . $minute  . ' minutes');
echo $datetime->format('H:i:s');

In addition, I put to use the time in 24 and the TimeZone of São Paulo

  • 1

    In this case you are adding up 5 minutes, and not multiplying the hours. But I believe that is exactly what the author wants to do.

  • Thank you for the answer but I would like to multiply, because it is that time being repeated as in the example 5 times, example if the time is 1:07:37 being repeated 5 times the result is 5:38:05. Thank you

  • I made a change in the answer, did some tests and had no problem. Check if it solves your problem.

  • 1

    I do not understand, because for 01:07:37 in the ideone gives 01:35:37 see https://ideone.com/ifOnTt. The question is to multiply hours and not only minutes.

5


The return was zero because it did not return 0:05:00?

The return was zero because you’re trying to multiply strings, and the conversion of PHP will interpret only the first digit 0 of your time, making "0:01:00" in 0, That’s what you’re doing 0 * 5 = 0.

How do I multiply hours in php?

If you want, as reported in the comments, multiply the total value by some multiplier, work with timestamp through function strtotime and to format the time, use date:

$time = "0:01:00";
$mult = "5";

var_dump(date("H:i:s", strtotime("00:01:00") * $mult));
var_dump(date("H:i:s", strtotime("01:07:37") * $mult));

The result will be:

string(8) "00:05:00"
string(8) "05:38:05"

See working on ideone.

@Edit

how to make it return 1 day and 1:00:00 ?

If you need this count, you can do it using the timestamp from the day it began:

$time ='01:00:00';
$mult = 5;

$calculated = strtotime("1970-01-01 $time") * $mult;
$date = date("d H:i:s", $calculated);

$datePart = explode(" ", $date);

printf("%s dia(s) %s", $datePart[0]-1, $datePart[1]);

Will result in:

0 dia(s) 05:00:00
1 dia(s) 01:00:00
  • Our thanks for the answer, but has a problem if the time goes past 24:00:00 it will return the time difference, example $mult= "25" var_dump(date("H:i:s", strtotime("01:00:00") * $mult);, returned 01:00:00

  • That would be my next question rsss, how to make it return 1 day and 1:00:00 ?

  • @Junior I edited and added another way, that’s it?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.