Days, Hours, Minutes, Seconds Difference as Percentage

Discussion in 'PHP' started by FishSword, Dec 20, 2015.

  1. #1
    Hi All,

    How do I conver the following to a percentage of difference.
    When the days, hours, minutes, seconds equal zero, this will be 100%

    $expirationDate = strtotime("2015-12-20 23:10:00");
    $toDay = strtotime(date('Y-m-d H:i:s'));
    
    $difference = abs($toDa - $expirationDate);
    
    $days = floor($difference / 86400);
    $hours = floor(($difference - $days * 86400) / 3600);
    $minutes = floor(($difference - $days * 86400 - $hours * 3600) / 60);
    $seconds = floor($difference - $days * 86400 - $hours * 3600 - $minutes * 60);
    PHP:
    Cheers,

    FishSword
     
    Last edited: Dec 20, 2015
    FishSword, Dec 20, 2015 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    540
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    %Usage = [ time() - $startingDate ] / [ $expiryDate - $startingDate ] * 100
    That would infer 100% for fully used service however. So in your case I guess you have to use 1 to kind of "reverse" it ;)
     
    Last edited: Dec 20, 2015
    hdewantara, Dec 20, 2015 IP