Difference between two dates and times

They have: 71 posts

Joined: Mar 2004

I am having trouble modifying this script to display the difference between two times. Do you guys see anything wrong?

function elapsed($eyear, $emonth, $eday, $ehour, $eminute, $year, $month, $day, $hour, $minute) {

  $years = $year - $eyear;
  if ($years < 1) $yearWord = '';
  elseif ($years == 1) $yearWord = 'year,';
  elseif ($years > 1) $yearWord = 'years,';
  if ($month > $emonth) {
    if ($day < $eday) $months = $month - $emonth -1;
    else $months = $month - $emonth;
  } else {
    $between = $emonth - $month;
    $plus = 12 - $emonth;
    if ($day < $eday) $months = $emonth + $plus - $between -1;
    else $months = $emonth + $plus - $between;
  }
  if ($months == 1) $monthWord = 'month,';
  elseif ($months > 1) $monthWord = 'months,';
  if ($day > $eday) {
    if ($hour < $ehour) $days = $day - $eday -1;
    else $days = $day - $eday;
  } else {
    $between = $eday - $day;
    $daysToEndOfMonth = $day - $eday;
    if ($day == 28) $plus = $daysToEndOfMonth;
    if ($day == 29) $plus = $daysToEndOfMonth;
    if ($day == 30) $plus = $daysToEndOfMonth;
    if ($day == 31) $plus = $daysToEndOfMonth;
    if ($hour < $ehour) $days = $eday + $plus - $between - 1;
    else $days = $eday + $plus - $between;
  }
  if ($days == 1) $dayWord = 'day,';
  else $dayWord = 'days,';
  if ($hour > $ehour) {
    if ($minute < $eminute) $hours = $hour - $ehour - 1;
    else $hours = $hour - $ehour;
  } else {
    $between = $ehour - $hour;
    $plus = 24 - $ehour;
    if ($minute < $eminute) $hours = $ehour + $plus - $between - 1;
    else $hours = $ehour + $plus - $between;
  }
  if ($hours == 1) $hourWord = 'hour,';
  if ($hours > 1) $hourWord = 'hours,';
  if ($minute > $eminute) {
    $minutes = $minute - $eminute;
  } else {
    $between = $eminute - $minute;
    $plus = 60 - $eminute;
    $minutes = $eminute + $plus - $between;
  }
  if ($minutes == 60) $minutes = 0;
  if ($minutes == 1) $minuteWord = 'minute';
  elseif ($minutes > 1) $minuteWord = 'minutes';
  if ($years == 0) { $years = ''; $yearWord = ''; }
  if ($months == 0) { $months = ''; $monthWord = ''; }
  if ($days == 0) { $days = ''; $dayWord = ''; }
  if ($hours == 0) { $hours = ''; $hourWord = ''; }
  if ($minutes == 0) { $minutes = ''; $minuteWord = ''; }
  echo $years.' '.$yearWord.' '.$months.'  '.$monthWord.' '.$days.' '.$dayWord.' '.$hours.' '.$hourWord.' '.$minutes.' '.$minuteWord.'';
}
'

They have: 18 posts

Joined: Jul 2004

what language are you trying for... PHP?? Confused

They have: 3 posts

Joined: Aug 2005

Yeap definitely php Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.