Friday 26 June 2015

timeElapsed() function

<?php
function timeElapsed($t1, $t2 = null) {
/*
 Returns an object with time elapsed from $t1 to $t2. Code From: http://coursesweb.net/php-mysql/
 Properties: y = years, m = months, w = weeks, d = days (till end of month), d2 = days till end of week, h = hours, i = minutes, s = seconds, days = total days
 $t2 is Optional, if not passed, will be set the curent date-time
 $t2 must be higher than $t1, they can be in Unix Timestamp, or string with a valid literaly date/time format (day.month.year , or: year-month-day, or: Year-Month-Day Hour:Minute:Seconds)
*/
  $t1 = is_int($t1) ? new DateTime('@'. $t1) : new DateTime($t1);
  $t2 = ($t2 == null) ? new DateTime() : (is_int($t2) ? new DateTime('@'. $t2) : new DateTime($t2));

  // object with the difference from $t1 to $t2
  $df = $t2->diff($t1);
  $df->w = floor($df->days / 7) - ($df->y * 52) - $df->m * 4;   // weeks
  $df->d2 = $df->d - ($df->w * 7);    // days till the end of week
  return $df;

// $df->y = years, $df->m = months, $df->w = weeks, $df->d = days (till end of month), $df->d2 = days till end of week, $df->h = hours, $df->i = minutes, $df->s = seconds
}

// Here add the timeElapsed() function
//1. Time elapsed from a specified Timestamp till Now.
$time = 1402008961;     // Timestamp
$t_diff = timeElapsed($time);    // gets the object with time difference

// Output (if show elapsed weeks ($w property), use $d2 property for remaining days till end of week)
echo sprintf('%d month, %d weeks, %d days, and %d hours ago', $t_diff->m, $t_diff->w, $t_diff->d2, $t_diff->h);
echo '<br>Total days: '. $t_diff->days .' days ago.';

/*
Output:
1 month, 2 weeks, 3 days, and 11 hours ago
Total days: 47 days ago.

*/

//2.Time elapsed from a literaly date till a specified date-time.
// Here add the timeElapsed() function

$t1 = '15.10.1976';
$t2 = '1996-07-07 15:07:08';
$t_diff = timeElapsed($t1, $t2);    // gets the object with time difference

// Output (if not show elapsed weeks ($w property), use $d property for days till end of month)
echo $t_diff->y .' years, '. $t_diff->m .' months, '. $t_diff->d .' days, '. $t_diff->h .' hours, and '. $t_diff->i .' minutes.';

/*Output:19 years, 8 months, 23 days, 15 hours, and 7 minutes.*/

//3. Time elapsed from a literaly date-time till curent time. With correctly grammar, and without 0 values.
// Here add the timeElapsed() function

// function to get correctly english string with elapsed time
function tStr($t, $str) {
  if($t < 1) return '';
  else return ($t > 1) ? "$t $str". 's' : "$t $str";
}

$time = '1996-07-07 15:09:10';
$t_diff = timeElapsed($time);    // gets the object with time difference

$dt_str = array('y'=>'year', 'm'=>'month', 'd'=>'day', 'h'=>'hour', 'i'=>'minute', 's'=>'second');
$ar_re = array();    // to store resulted strings with year, month, ...

// get string for each part of elapsed time and store in $ar_re
foreach($dt_str as $k=>$v) {
  $dt_re = tStr($t_diff->{$k}, $v);
  if($dt_re != '') $ar_re[] = $dt_re;
}

// Output
echo  implode(', ', $ar_re) .' ago';


/*Output: 18 years, 15 days, 18 hours, 1 minute, 2 seconds ago*/
?>

0 comments:

Post a Comment