PHP Missing Function In Older Version

Posted by Umair Ashraf on Stack Overflow See other posts from Stack Overflow or by Umair Ashraf
Published on 2010-11-20T15:46:56Z Indexed on 2010/12/25 9:53 UTC
Read the original article Hit count: 290

Filed under:

My this PHP function converts a datetime string into more readable way to represent passed date and time. This is working perfect in PHP version 5.3.0 but on the server side it is PHP version 5.2.17 which lacks this function. Is there a way I can fix this efficiently? This is not only a function which needs this "diff" function but there are many more.

public function ago($dt1)
 {
  $interval = date_create('now')->diff(date_create($dt1));
  $suffix = ($interval->invert ? ' ago' : '-');
  if ($v = $interval->y >= 1) return $this->pluralize($interval->y, 'year') . $suffix;
  if ($v = $interval->m >= 1) return $this->pluralize($interval->m, 'month') . $suffix;
  if ($v = $interval->d >= 1) return $this->pluralize($interval->d, 'day') . $suffix;
  if ($v = $interval->h >= 1) return $this->pluralize($interval->h, 'hour') . $suffix;
  if ($v = $interval->i >= 1) return $this->pluralize($interval->i, 'minute') . $suffix;
   return $this->pluralize($interval->s, 'second') . $suffix;
 } 

© Stack Overflow or respective owner

Related posts about php