Time Elapsed Function

The elapsed time function's primary purpose is to:

  • determine if the start and end are equal, and if so return text that says so.
  • if they aren't equal, find out what percentage of that time has already elapsed.

<?php

function elapsed_time($due, $start) {
  if (
$due == $start) {
   
$data = t('n/a'); // set the return if equal... we'll get a divide by 0 otherwise.
   
return $data;
  }
  else {
/**
* Account for Time Zone
* yes I realize this would normally be done outside this function and passed in, but alas, I was lazy.
*/
   
$due = $due + 21600;
   
$start = $start + 21600;
/**
* Account for Start & End of work day.
* And this is specific to our office, but again laziness.
*/
   
$due = $due + 64800;
   
$start = $start + 32400;
   
$total = $due - $start; // total amound of time in seconds
   
$today = strtotime('now'); // today in seconds
   
$left = $due - $today; // the amound of time left between the start and end dates
   
$data = 100 - ceil(($left / $total)*100); // changing all of our figures into a %
   
if ($data > 100) {
     
$data = t("100%"); // There aren't any limits so we can end up with like 1350%, so this eliminates that
     
return $data;
    }
    else {
     
$data = $data .t("%"); // set the actual percentage
     
return $data;
    }
  }
}

?>