PHP 中的时间乘法
Multiplication of a time in PHP
我需要 PHP
中的时间乘法
$maritime ='01:10:00';
我需要将这个 $maritime
增加到 5
我想得到这样的答案
01:10:00*5 = 05:50:00
只需将时间转换为基本单位(在本例中为秒),然后将其相乘并转换回来。
这是一个简单的示例,说明如何操作,请注意 toSeconds
中没有错误检查,您可能希望在 fromSeconds
中处理 00。
function toSeconds($time){
$arr = explode(":", $time);
return $arr[0]*3600 + $arr[1]*60 + $arr[2];
}
function fromSeconds($seconds){
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
return "$hours:$minutes:$seconds";
}
这是你应该做的
第 1 步:将小时换算成秒
$seconds = strtotime("1970-01-01 $maritime UTC");
第二步:直接相乘
$multiply = $seconds * 5;
第 3 步:将秒数转换回小时数,大功告成!
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);
这是显示输出的 Link of Eval
更新:
如果你工作超过一天
即时间*25次,那么就不止一天了
那么我的输出将是02 05:10:00
但是如果你想严格按小时计算,你应该使用 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);
?>
很简单。试试这个
$your_time = "01:10:00";
date_default_timezone_set ("UTC");
$secs = strtotime($your_time ) - strtotime("00:00:00");
echo date("H:i:s",$secs * 5);
我需要 PHP
中的时间乘法$maritime ='01:10:00';
我需要将这个 $maritime
增加到 5
我想得到这样的答案
01:10:00*5 = 05:50:00
只需将时间转换为基本单位(在本例中为秒),然后将其相乘并转换回来。
这是一个简单的示例,说明如何操作,请注意 toSeconds
中没有错误检查,您可能希望在 fromSeconds
中处理 00。
function toSeconds($time){
$arr = explode(":", $time);
return $arr[0]*3600 + $arr[1]*60 + $arr[2];
}
function fromSeconds($seconds){
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
return "$hours:$minutes:$seconds";
}
这是你应该做的
第 1 步:将小时换算成秒
$seconds = strtotime("1970-01-01 $maritime UTC");
第二步:直接相乘
$multiply = $seconds * 5;
第 3 步:将秒数转换回小时数,大功告成!
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);
这是显示输出的 Link of Eval
更新:
如果你工作超过一天
即时间*25次,那么就不止一天了
那么我的输出将是02 05:10:00
但是如果你想严格按小时计算,你应该使用 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);
?>
很简单。试试这个
$your_time = "01:10:00";
date_default_timezone_set ("UTC");
$secs = strtotime($your_time ) - strtotime("00:00:00");
echo date("H:i:s",$secs * 5);