我想通过给它一个百分比值来获取剩余时间。
开始时间:10:07:03 AM
结束时间:5:00:00 PM
剩余时间:6:52:57
10%的时间:0:41:18
19%的时间:1:18:28
25%的时间:1:43:14
44%的时间:3:01:42
93%的时间:6:24:03
答案 0 :(得分:1)
这可能是一个很好的起点:
<?php
$starttime = "10:07:03 AM";
$endtime = "5:00:00 PM";
$starttime_ts = strtotime($starttime);
$endtime_ts = strtotime($endtime);
echo "Start time: " . $starttime . " (Timestamp: " . $starttime_ts . ")";
echo "<br>";
echo "End time: " . $endtime . " (Timestamp: " . $endtime_ts . ")";
$v10percent = ($starttime_ts + ($endtime_ts - $starttime_ts) * 10 / 100);
$v90percent = ($starttime_ts + ($endtime_ts - $starttime_ts) * 90 / 100);
$v100percent = ($starttime_ts + ($endtime_ts - $starttime_ts) * 100 / 100);
echo "<br>";
echo "<br>";
echo "10% of time: " . date("h:i:s A", $v10percent);
echo "<br>";
echo "...";
echo "<br>";
echo "90% of time: " . date("h:i:s A", $v90percent);
echo "<br>";
echo "100% of time: " . date("h:i:s A", $v100percent);
?>
基本上,您将时间转换为timestamp
,以便获得可以处理的数字。然后,您可以按一些基本比例操作它们以获得所需的百分比。
strtotime()
:返回成功的时间戳记date()
:返回格式化的日期字符串Start time: 10:07:03 AM (Timestamp: 1554386823)
End time: 5:00:00 PM (Timestamp: 1554411600)
10% of time: 10:48:20 AM
...
90% of time: 04:18:42 PM
100% of time: 05:00:00 PM
答案 1 :(得分:0)
您应该使用时间戳而不是日期格式,以下代码将为您提供帮助
$startString = '10:07:03 AM';
$endString = '5:00:00 PM';
// convert to timistamp
$start = strtotime($startString);
$end = strtotime($endString);
$duration = $end-$start;
echo date('H:i:s',$duration);
// print 19% of duration
echo '<br />19% of duartion is: '. date('H:i:s',$duration*19/100);