Work out the percentage of the day that has elapsed

前端 未结 5 580
深忆病人
深忆病人 2021-01-12 10:10

Slightly strange question, but hopefully someone can help.

In essence, if the time was 12pm the the elapsed percentage would be 50%, 6am would be 25% and 16pm would

相关标签:
5条回答
  • 2021-01-12 10:43

    Assuming you can get the current time of day, it'd be pretty easy to calculate the percentage of the day elapsed.

    percentage = (hours / 24 + minutes / (60 * 24)) * 100
    
    0 讨论(0)
  • 2021-01-12 10:45

    The accepted answer bugs me for many reasons including the relationship to a real life calendar. Personally, I would probably do this:

    <?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);
    

    result:

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

    You could then extend it even further by making a function and changing the range from "today" and "tomorrow" to something else.

    0 讨论(0)
  • 2021-01-12 10:46

    gettimeofday(true) returns the number of seconds elapsed since midnight as a float (I think), so you want: gettimeofday(true)/(60*60*24). Multiply by 100 to get a percentage.

    EDIT: Actually, gettimeofday returns the number of seconds elapsed since the start of the epoch, so you need to subtract midnight:

    $midnight = strtotime('00:00');
    $epochseconds = gettimeofday(true);
    $timeofdayseconds = $epochseconds - $midnight;
    $timepercent = $timeofdayseconds/(60*60*24)*100;
    
    0 讨论(0)
  • 2021-01-12 11:06

    24hours are 100% so 24/currentTime = result 100 / result = % ;)

    0 讨论(0)
  • 2021-01-12 11:06

    I dont know php but is there any way to get the current date witch i guess :P can you extract the hours out of this or minutes how exact do you want it to be?, Current hours/24 * 100 :P current minutes/1440 * 100

    0 讨论(0)
提交回复
热议问题