The timeElapsed() function presented in this page can be used to get the time elapsed from a specified date/time, or TimeStamp, till Now or till other specified date/time. Details are in the comments in code and examples.

Code of timeElapsed() function

- Click to select it.
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
}

Examples

- Click to select them.

1. Time elapsed from a specified Timestamp till Now.
// Here add the timeElapsed() function

$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.';
Outputs something like this:
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.';
Outputs:
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';
Outputs something like this:
18 years, 15 days, 18 hours, 1 minute, 2 seconds ago

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag can be used to embed an external application (SWF, PDF) in web page?
<mark> <embed> <canvas>
<embed src="flash_game.swf" width="450" height="350" />
Which CSS pseudo-element adds a special style to the first line of a text?
:first-letter :before :first-line
#id:first-line {
  font-weight: bold;
  color: blue;
}
Click on the window object property which gets or sets the URL of current page.
window.location window.self window.status
var url = window.location;
alert(url);
Indicate the PHP function used to get the contents of a file or page and store it into a string.
fopen() file_put_contents() file_get_contents()
$homepage = file_get_contents("http://coursesweb.net/");
echo $homepage;
Get Time Elapsed

Last accessed pages

  1. Clear Canvas Context (3465)
  2. Qwop (59133)
  3. N (549)
  4. Inca Ball (1959)
  5. Flash Games - Free online Games (21264)

Popular pages this month

  1. Qwop (4613)
  2. Drag Racer V3 (2236)
  3. Flash Games - Free online Games (1108)
  4. Bubbles3 (993)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (983)