【问题标题】:Work out the percentage of the day that has elapsed计算已过去一天的百分比
【发布时间】:2010-10-03 12:12:25
【问题描述】:

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

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

鉴于当前时间,您如何计算已经过去的天数?

【问题讨论】:

    标签: php datetime date time


    【解决方案1】:

    假设您可以获取当前时间,则很容易计算一天中经过的百分比。

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

    【讨论】:

    【解决方案2】:

    24 小时是 100% 所以 24/currentTime = 结果 100 / 结果 = % ;)

    【讨论】:

      【解决方案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;
      

      【讨论】:

      • 请注意,这在大多数情况下都有效,除了 DST 天!今天我的时钟倒退了一个小时,这个功能失败了,因为从周日午夜到周日晚上 8 点的小时数有额外的 +1 小时!被警告
      【解决方案4】:

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

      <?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" 更改为其他值,从而进一步扩展它。

      【讨论】:

        【解决方案5】:

        我不知道 php,但有什么方法可以获取我猜的当前日期:P 你能从中提取小时数或分钟数,你希望它有多精确?,当前小时数/24 * 100:P 当前分钟/1440 * 100

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-10-27
          • 1970-01-01
          • 1970-01-01
          • 2022-06-23
          • 2011-06-01
          • 2021-01-10
          相关资源
          最近更新 更多