Here I am using custom function for finding the time elapsed since a date time.
echo Datetodays('2013-7-26 17:01:10');
function Datetodays($d) {
$date_start = $d;
$date_end = date('Y-m-d H:i:s');
define('SECOND', 1);
define('MINUTE', SECOND * 60);
define('HOUR', MINUTE * 60);
define('DAY', HOUR * 24);
define('WEEK', DAY * 7);
$t1 = strtotime($date_start);
$t2 = strtotime($date_end);
if ($t1 > $t2) {
$diffrence = $t1 - $t2;
} else {
$diffrence = $t2 - $t1;
}
//echo "
".$date_end." ".$date_start." ".$diffrence;
$results['major'] = array(); // whole number representing larger number in date time relationship
$results1 = array();
$string = '';
$results['major']['weeks'] = floor($diffrence / WEEK);
$results['major']['days'] = floor($diffrence / DAY);
$results['major']['hours'] = floor($diffrence / HOUR);
$results['major']['minutes'] = floor($diffrence / MINUTE);
$results['major']['seconds'] = floor($diffrence / SECOND);
//print_r($results);
// Logic:
// Step 1: Take the major result and transform it into raw seconds (it will be less the number of seconds of the difference)
// ex: $result = ($results['major']['weeks']*WEEK)
// Step 2: Subtract smaller number (the result) from the difference (total time)
// ex: $minor_result = $difference - $result
// Step 3: Take the resulting time in seconds and convert it to the minor format
// ex: floor($minor_result/DAY)
$results1['weeks'] = floor($diffrence / WEEK);
$results1['days'] = floor((($diffrence - ($results['major']['weeks'] * WEEK)) / DAY));
$results1['hours'] = floor((($diffrence - ($results['major']['days'] * DAY)) / HOUR));
$results1['minutes'] = floor((($diffrence - ($results['major']['hours'] * HOUR)) / MINUTE));
$results1['seconds'] = floor((($diffrence - ($results['major']['minutes'] * MINUTE)) / SECOND));
//print_r($results1);
if ($results1['weeks'] != 0 && $results1['days'] == 0) {
if ($results1['weeks'] == 1) {
$string = $results1['weeks'] . ' week ago';
} else {
if ($results1['weeks'] == 2) {
$string = $results1['weeks'] . ' weeks ago';
} else {
$string = '2 weeks ago';
}
}
} elseif ($results1['weeks'] != 0 && $results1['days'] != 0) {
if ($results1['weeks'] == 1) {
$string = $results1['weeks'] . ' week ago';
} else {
if ($results1['weeks'] == 2) {
$string = $results1['weeks'] . ' weeks ago';
} else {
$string = '2 weeks ago';
}
}
} elseif ($results1['weeks'] == 0 && $results1['days'] != 0) {
if ($results1['days'] == 1) {
$string = $results1['days'] . ' day ago';
} else {
$string = $results1['days'] . ' days ago';
}
} elseif ($results1['days'] != 0 && $results1['hours'] != 0) {
$string = $results1['days'] . ' day and ' . $results1['hours'] . ' hours ago';
} elseif ($results1['days'] == 0 && $results1['hours'] != 0) {
if ($results1['hours'] == 1) {
$string = $results1['hours'] . ' hour ago';
} else {
$string = $results1['hours'] . ' hours ago';
}
} elseif ($results1['hours'] != 0 && $results1['minutes'] != 0) {
$string = $results1['hours'] . ' hour and ' . $results1['minutes'] . ' minutes ago';
} elseif ($results1['hours'] == 0 && $results1['minutes'] != 0) {
if ($results1['minutes'] == 1) {
$string = $results1['minutes'] . ' minute ago';
} else {
$string = $results1['minutes'] . ' minutes ago';
}
} elseif ($results1['minutes'] != 0 && $results1['seconds'] != 0) {
$string = $results1['minutes'] . ' minute and ' . $results1['seconds'] . ' seconds ago';
} elseif ($results1['minutes'] == 0 && $results1['seconds'] != 0) {
if ($results1['seconds'] == 1) {
$string = $results1['seconds'] . ' second ago';
} else {
$string = $results1['seconds'] . ' seconds ago';
}
}
return $string;
}
?>