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 tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Get Time Elapsed

Last accessed pages

  1. Spring Garden Cleanup (23)
  2. Create simple Website with PHP (11484)
  3. Emma Christmas Tree (33)
  4. Barbie Games (362)
  5. Mir And Ror (19)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (45791)
  2. Read Excel file data in PHP - PhpExcelReader (34450)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (34339)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (30919)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (28164)