简体   繁体   English

PHP从总时间的X百分比中获取时间

[英]PHP Get Time From X Percentage of Total Time

I don't know if this will make sense but I would like to get the time from percentage of a value, I have the following as an input example: 我不知道这是否有意义,但是我想从一个值的百分比中获取时间,我将以下内容作为输入示例:

mypercentage = 50; 
mytime = "00:59:59"

As you can see, my time is 60 minutes (1 hour) and 50% of that is 30 minutes therefore from the 2 above inputs I would like to get the following output (50 % of an hour): 如您所见,我的时间是60分钟(1小时),其中50%是30分钟,因此我希望从上述2个输入中获得以下输出(一个小时的50%):

00:30:00
  1. Explode the string into an array 将字符串分解为数组
  2. Convert the 3 elements into numbers 将3个元素转换为数字

  3. Find the value in seconds. 查找以秒为单位的值。 $array[0]*3600+$array[1]*60+$array[2] $数组[0] * 3600 + $阵列[1] * 60 + $阵列[2]

  4. Calculate the time from the value in step 3 and the percentage. 根据步骤3中的值和百分比计算时间。

  5. Use /3600 and /60 to find the hour and minutes and %60 to find the seconds. 使用/ 3600和/ 60查找小时和分钟,并使用%60查找秒。

  6. put the values in an array and implode to a string, or just use 将值放入数组并内插到字符串中,或​​只使用

    $hour.":".$minute.":".$second $小时 “:” $分钟。 “:” $秒。

Thank you all who responded, I found this solution to work: 谢谢所有回答的人,我发现此解决方案有效:

            $mypercentage = 50;
            $mytime = '00:59:59';

            $totaltime = date('H:i:s', strtotime($mytime));

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

            $per_seconds = ($mypercentage / 100) * $seconds;

            $time_from_percentage = gmdate("H:i:s", $per_seconds);
            echo $time_from_percentage;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM