PHP中的时间乘法 [英] Multiplication of a time in PHP

查看:35
本文介绍了PHP中的时间乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 PHP 中增加时间

I need multiplication of time in PHP

$maritime ='01:10:00';       

我需要将此 $maritime 增加到 5
我想得到这样的答案

I need increment this $maritime in to 5
I want to get answer like this

01:10:00*5 =  05:50:00

推荐答案

这是你应该做的

第 1 步:将您的小时数转换为秒

$seconds = strtotime("1970-01-01 $maritime UTC");

第2步:直接相乘

$multiply = $seconds * 5;

第 3 步:将秒转换回小时,大功告成!

Step 3 : Convert the seconds back to hours, And you're done !

echo gmdate("d H:i:s",$multiply);

所以你的最终代码应该是

<?php
$maritime ='01:10:00';
$seconds = strtotime("1970-01-01 $maritime UTC");
$multiply = $seconds * 5;  #Here you can multiply with your dynamic value
echo gmdate("d H:i:s",$multiply);

这是显示输出的 Eval 链接

更新:

如果你工作超过一天

i.e. time * 25 次,那么就会超过一天

i.e., time * 25 times, then it will have more than one day

然后我的输出将是 02 05:10:00

但是,如果您希望以小时为单位,则应使用 DateTime

But if you want it in hours strictly you should use the DateTime

<?php
$maritime ='01:10:00';
$seconds = strtotime("1970-01-01 $maritime UTC");
$multiply = $seconds * 25;  #Here you can multiply with your dynamic value
$seconds = $multiply;
$zero    = new DateTime("@0");
$offset  = new DateTime("@$seconds");
$diff    = $zero->diff($offset);
echo sprintf("%02d:%02d:%02d", $diff->days * 24 + $diff->h, $diff->i, $diff->s);
?>

这是评估链接

这篇关于PHP中的时间乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆