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 adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
}
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
alert(content);
Click on the "echo" correct instruction.
echo "CoursesWeb.net" echo "CoursesWeb.net"; echo ""CoursesWeb.net";
echo "Address URL: http://CoursesWeb.net";
Get Time Elapsed

Last accessed pages

  1. Butterfly Kyodai (6560)
  2. Bubbles3 (8623)
  3. Zuma Deluxe (9622)
  4. Qwop (3126)
  5. Get and change IFrame content through a JavaScript script created in another IFrame (6070)

Top accessed pages

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