Display time difference between two dates

6 posts by 4 authors in: Forums > CMS Builder
Last Post: October 4, 2017   (RSS)

By JeffC - October 3, 2017

Hi

I would like to display the number of hours and minutes it takes for a lifeboat to attend and deal with an emergency. I have the code below which works ok, but it it displays 0 hours if the incident takes less than 60mins. For example, if an incident takes 59 minutes it would display '0 hours 59 mins'. I would prefer it to display '59 mins'.

Also my code looks a bit clunky. Advice as to how it could/should be done in a more elegant and concise way would be appreciated!

Thanks. Here's the code:

Time to launch:      
<?php 
$requestDate = strtotime($launchesRecord['time_request_received']);
$launchDate = strtotime($launchesRecord['time_launched']);
$reachedDate = strtotime($launchesRecord['time_reached_incident']);
$departedDate = strtotime($launchesRecord['time_departed_incident']);
$recoveredDate = strtotime($launchesRecord['time_lifeboat_recovered']);
$dateDiff = ($launchDate - $requestDate);
$h = ($dateDiff/(60*60))%24;
$m = ($dateDiff/60)%60;
echo $h." hours\n";
echo $m." minutes\n <br />";
?>
     
Time to reach incident:
<?php 
$dateDiff = ($reachedDate - $requestDate);
$h = ($dateDiff/(60*60))%24;
$m = ($dateDiff/60)%60;
echo $h." hours\n";
echo $m." minutes\n <br />";
?>
     
Time at incident:
<?php 
$dateDiff = ($departedDate - $reachedDate);
$h = ($dateDiff/(60*60))%24;
$m = ($dateDiff/60)%60;
echo $h." hours\n";
echo $m." minutes\n <br />";
?>
     
Time to recovery:
<?php 
$dateDiff = ($recoveredDate - $requestDate);
$h = ($dateDiff/(60*60))%24;
$m = ($dateDiff/60)%60;
echo $h." hours\n";
echo $m." minutes\n <br />";
?>

Jeff

By Dave - October 3, 2017

Hi Jeff, 

We have a function in CMSB called prettyDate() which shows the relative age of something in english (eg:  a few seconds ago, about a minute ago, 3 hours ago, etc) but I don't think that's quite what you need for this.

I did a quick Google search for "php display hours minutes seconds" and found this post that looks relevant: https://stackoverflow.com/q/8273804

There are a few pre-written functions in there that look like they might output the date in the format you want.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By leo - October 3, 2017

Hi Jeffncou,

You can make an if statement before echoing out time like this:

if($h > 0){
  echo $h . ' hours\n';
}

Leo - PHP Programmer (in training)
interactivetools.com

By Deborah - October 3, 2017

Jeff,

Being the PHP novice that I am, I'd be the last person in this forum to rely upon for PHP tips, but thought I'd share that before reading the other replies, I found this PHP.net info relating to the empty() variable.

http://php.net/manual/en/function.empty.php#refsect1-function.empty-returnvalues

Disregard above link you've already found the answer, but I'm curious to find out what works for you, so please post back!

~ Deborah

By Dave - October 3, 2017

Hi Deborah, 

I've starting using empty() more myself.  I like how you can use it to check undefined variables and it doesn't return a PHP warning.

Jeff, I missed this line: 

Advice as to how it could/should be done in a more elegant and concise way would be appreciated!

Here's a cleaner version.  I put the duplicated code into a function.

<?php
// display date difference as: x hours, y minutes, z seconds
function hoursAndMinutesBetween($dateString1, $dateString2) {
  $dateTime1 = strtotime($dateString1);
  $dateTime2 = strtotime($dateString2);
  if ($dateTime1 > $dateTime2) { dieAsCaller(__FUNCTION__ . ": First date value must be older then second value"); }

  // get hour/min/sec values
  $totalSeconds = ($dateTime2 - $dateTime1);
  $hours        = ($totalSeconds/(60*60))%24;
  $minutes      = ($totalSeconds/60)%60;
  $seconds      = $totalSeconds % 60;

  // format output
  $output = "";
  if ($hours) { $output .= "$hours hours, "; }
  $output .= "$minutes minutes, ";
  $output .= "$seconds seconds";
  return $output;
}
?>

Time to reach incident: <?php echo hoursAndMinutesBetween($launchesRecord['time_request_received'], $launchesRecord['time_reached_incident']); ?><br/>
Time at incident:       <?php echo hoursAndMinutesBetween($launchesRecord['time_reached_incident'], $launchesRecord['time_departed_incident']); ?><br/>
Time to recovery:       <?php echo hoursAndMinutesBetween($launchesRecord['time_request_received'], $launchesRecord['time_lifeboat_recovered']); ?><br/>

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com