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: https://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
Click on the HTML tag which creates an horizontal line in web page.
<br /> <em> <hr />
Some content ...
<hr />
Content under line ...
Which CSS property defines the text color?
font-style font-variant color
h2 {
  color: #cbdafb;
}
Click on the function which searches if a character, or text exists in a string.
indexOf() toString() split()
var str = "Web courses - http://CoursesWeb.net/";
if(str.indexOf("http://") == -1) alert("http:// isn`t in string");
else alert("http:// is in string");
Which function splits a string into an array of strings based on a separator?
array_merge() explode() implode()
$str = "apple,banana,melon,pear";
$arr = explode(",", $str);
var_export($arr);      // array (0=>"apple", 1=>"banana", 2=>"melon", 3=>"pear")
Get Time Elapsed

Last accessed pages

  1. Sending Email, Mailables (240)
  2. Define Custom List-item Markers, Bullets for UL, OL Lists (3680)
  3. SHA256 Encrypt hash in JavaScript (7023)
  4. Input text fields (2285)
  5. Insert, Select and Update NULL value in MySQL (30219)

Popular pages this month

  1. Insert, Select and Update NULL value in MySQL (515)
  2. Read Excel file data in PHP - PhpExcelReader (430)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (319)
  4. PHP getElementById and getElementsByTagName (255)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (234)