计算出已经过去的那一天的百分比

时间:2009-01-26 08:38:18

标签: php datetime date time

有点奇怪的问题,但希望有人可以提供帮助。

实质上,如果时间是下午12点,则经过的百分比为50%,早上6点为25%,下午16点为75%。

鉴于目前的时间,你怎么能算出已经过去的那天?

5 个答案:

答案 0 :(得分:12)

假设您可以获得当天的当前时间,那么计算当天的百分比非常容易。

percentage = (hours / 24 + minutes / (60 * 24)) * 100

答案 1 :(得分:4)

24小时是100% 所以24 /当前时间=结果 100 / result =%;)

答案 2 :(得分:3)

gettimeofday(true)返回自午夜以来经过的秒数(我认为),所以你需要:gettimeofday(true)/(60*60*24)。乘以100得到一个百分比。

编辑:实际上,gettimeofday会返回自纪元开始以来经过的秒数,因此您需要减去午夜:

$midnight = strtotime('00:00');
$epochseconds = gettimeofday(true);
$timeofdayseconds = $epochseconds - $midnight;
$timepercent = $timeofdayseconds/(60*60*24)*100;

答案 3 :(得分:2)

接受的答案让我感到困惑的原因很多,包括与现实生活日历的关系。就个人而言,我可能会这样做:

<?php
$start = strtotime("today");
$end = strtotime("tomorrow");
$now = time();
$secondstoday = $end - $start;
$secondselapsed = $now - $start;
$percentage = ($secondselapsed / $secondstoday ) * 100;

printf('%.2f%% of the day has passed (%d out of %d seconds)', $percentage, $secondselapsed, $secondstoday);

结果:

72.22% of the day has passed (62402 out of 86400 seconds)

然后,您可以通过创建函数并将范围从"today""tomorrow"更改为其他内容来进一步扩展它。

答案 4 :(得分:0)

我不知道php但有没有办法得到当前的日期女巫我猜:P你可以提取出这个或几分钟的小时你想要它的确切时间吗?,当前小时数/ 24 * 100:P 目前会议纪要/ 1440 * 100