How to find the time elapsed since a date time stamp like 2010-04-28 17:25:43
, final out put text should be like xx Minutes Ago
/xx Days Ago

- 303,325
- 100
- 852
- 1,154

- 49,883
- 70
- 181
- 236
-
http://stackoverflow.com/questions/2020517/calculating-the-time-difference-in-an-php-mysql-javascript-system – Saif Bechan May 26 '10 at 18:58
-
This is related to PHP not with Java or Javascript – Mithun Sreedharan May 26 '10 at 19:10
-
2Todays unix timestamp minus your datetime as a unix timestamp, then divide the result by 86400 seconds to get the time passed in days – Timo Huovinen Sep 19 '13 at 08:56
17 Answers
Most of the answers seem focused around converting the date from a string to time. It seems you're mostly thinking about getting the date into the '5 days ago' format, etc.. right?
This is how I'd go about doing that:
$time = strtotime('2010-04-28 17:25:43');
echo 'event happened '.humanTiming($time).' ago';
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$time = ($time<1)? 1 : $time;
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
I haven't tested that, but it should work.
The result would look like
event happened 4 days ago
or
event happened 1 minute ago
-
Why create the array from least to most only to call array_reverse? Regardless, I believe you need to pass "preserve_keys" as true as the second parameter to array_reverse. – Jeremy Kauffman Jun 24 '10 at 21:24
-
good point about reversing. I haven't tried the code, so I'm guessing you're right about preserve_keys. – arnorhs Jul 07 '10 at 13:20
-
1It's a good start, but it's just a division, and floor means "happended at least 1 day ago" etc... the real mean of your code is "it happened 4 days 3 hours ago". – Thomas Decaux Jul 24 '13 at 11:09
-
2
-
strtotime is unreliable function. Refer http://in1.php.net/manual/en/function.strtotime.php#111989 echo date( "Y-m-d", strtotime( "2009-01-31 +1 month" ) ); // PHP: 2009-03-03 echo date( "Y-m-d", strtotime( "2009-01-31 +2 month" ) ); // PHP: 2009-03-31 – Swatantra Kumar Jun 06 '14 at 10:51
-
1@SwatantraK that's not proof of unreliability, just of a different approach – lucasreta May 12 '16 at 07:34
-
@porjolovsky Even if it is a different approach, the operation failed resulted in wrong outcome and shows the vulnerability, hence can not be relied upon. – Swatantra Kumar May 12 '16 at 13:34
-
@SwatantraK you are right, I chose the wrong words. What I meant to say is that I particularly prefer this method, in which the mistake at which the comment points can be easily rectified (by specifying day number instead of adding months), to other methods, for I find that despite this error it manages pretty well other common mistakes in date handling. Plus, I usually just use it to transform "Y-m-d" strings without modification, and rely on `DateTime` objects for even slightly more complex (eg. add time to date) operations. Is there a 100% reliable alternative for strtotime conversion? – lucasreta May 12 '16 at 16:29
-
-
Most can live without those missing 5 days (6 in a leap year [splitting hairs]) for loosely-referenced times. Though I will be adding approximations" such as "about a year", "over a year" and "a few years" to polish the results ;) – Mavelo Jul 21 '18 at 13:10
-
only shows 1 month, then 2 months nothing like 1 month 4 days 2 hours, as advertised. How is this the right answer? – Norman Bird Oct 25 '20 at 03:30
Want to share php function which results in grammatically correct Facebook like human readable time format.
Example:
echo get_time_ago(strtotime('now'));
Result:
less than 1 minute ago
function get_time_ago($time_stamp)
{
$time_difference = strtotime('now') - $time_stamp;
if ($time_difference >= 60 * 60 * 24 * 365.242199)
{
/*
* 60 seconds/minute * 60 minutes/hour * 24 hours/day * 365.242199 days/year
* This means that the time difference is 1 year or more
*/
return get_time_ago_string($time_stamp, 60 * 60 * 24 * 365.242199, 'year');
}
elseif ($time_difference >= 60 * 60 * 24 * 30.4368499)
{
/*
* 60 seconds/minute * 60 minutes/hour * 24 hours/day * 30.4368499 days/month
* This means that the time difference is 1 month or more
*/
return get_time_ago_string($time_stamp, 60 * 60 * 24 * 30.4368499, 'month');
}
elseif ($time_difference >= 60 * 60 * 24 * 7)
{
/*
* 60 seconds/minute * 60 minutes/hour * 24 hours/day * 7 days/week
* This means that the time difference is 1 week or more
*/
return get_time_ago_string($time_stamp, 60 * 60 * 24 * 7, 'week');
}
elseif ($time_difference >= 60 * 60 * 24)
{
/*
* 60 seconds/minute * 60 minutes/hour * 24 hours/day
* This means that the time difference is 1 day or more
*/
return get_time_ago_string($time_stamp, 60 * 60 * 24, 'day');
}
elseif ($time_difference >= 60 * 60)
{
/*
* 60 seconds/minute * 60 minutes/hour
* This means that the time difference is 1 hour or more
*/
return get_time_ago_string($time_stamp, 60 * 60, 'hour');
}
else
{
/*
* 60 seconds/minute
* This means that the time difference is a matter of minutes
*/
return get_time_ago_string($time_stamp, 60, 'minute');
}
}
function get_time_ago_string($time_stamp, $divisor, $time_unit)
{
$time_difference = strtotime("now") - $time_stamp;
$time_units = floor($time_difference / $divisor);
settype($time_units, 'string');
if ($time_units === '0')
{
return 'less than 1 ' . $time_unit . ' ago';
}
elseif ($time_units === '1')
{
return '1 ' . $time_unit . ' ago';
}
else
{
/*
* More than "1" $time_unit. This is the "plural" message.
*/
// TODO: This pluralizes the time unit, which is done by adding "s" at the end; this will not work for i18n!
return $time_units . ' ' . $time_unit . 's ago';
}
}
-
it gives this string `-170 minutes ago` why minus sign and why it dont return 2 hour ago – vikas devde Apr 28 '13 at 20:01
-
what code did u write? this example works fine `echo get_time_ago(strtotime('-2 hours'));` – Aman May 02 '13 at 05:35
-
I have a DATETIME field in mysql, which i am retrieving and passing to this function as is..get_time_ago(strtotime(datetime value from db))...you know date format of mysql 'Y-m-d H:i:s' .....I tried both with and without strtotime...but didnt worked...pls help – vikas devde May 02 '13 at 16:20
-
Now I converted my datetime value to timestamp and passed to the function, its started working fine, I uploaded a new post, which should show date as 'less than 1 minute ago' but it showing '-210 minutes ago' – vikas devde May 02 '13 at 18:50
-
1Make sure your PHP server and your database are both set up with the same time zone. – nullability Oct 31 '13 at 17:59
I think I have a function which should do what you want:
function time2string($timeline) {
$periods = array('day' => 86400, 'hour' => 3600, 'minute' => 60, 'second' => 1);
foreach($periods AS $name => $seconds){
$num = floor($timeline / $seconds);
$timeline -= ($num * $seconds);
$ret .= $num.' '.$name.(($num > 1) ? 's' : '').' ';
}
return trim($ret);
}
Simply apply it to the difference between time()
and strtotime('2010-04-28 17:25:43')
as so:
print time2string(time()-strtotime('2010-04-28 17:25:43')).' ago';

- 1,901
- 3
- 25
- 39
-
1that would return a string like "3 hours 5 minutes 6 seconds ago", right? – arnorhs May 26 '10 at 19:41
-
Yep, it would do. Possibly misread the OP's question a bit, in which case it'd need some tweaking for their use. – JoeR May 26 '10 at 19:43
-
This worked for me perfectly, and much better than selected answer IMO. Thanks. +1 – Norman Bird Oct 25 '20 at 16:04
-
@JoeR Thanks a lot! Such tasks are not often encountered but when they do it is difficult to find a solution quickly. But you shared it with us, thanks! – Victor Sokoliuk Feb 23 '21 at 15:40
To improve upon @arnorhs answer I've added in the ability to have a more precise result so if you wanted years, months, days & hours for instance since the user joined.
I've added a new parameter to allow you to specify the number of points of precision you wish to have returned.
function get_friendly_time_ago($distant_timestamp, $max_units = 3) {
$i = 0;
$time = time() - $distant_timestamp; // to get the time since that moment
$tokens = [
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
];
$responses = [];
while ($i < $max_units && $time > 0) {
foreach ($tokens as $unit => $text) {
if ($time < $unit) {
continue;
}
$i++;
$numberOfUnits = floor($time / $unit);
$responses[] = $numberOfUnits . ' ' . $text . (($numberOfUnits > 1) ? 's' : '');
$time -= ($unit * $numberOfUnits);
break;
}
}
if (!empty($responses)) {
return implode(', ', $responses) . ' ago';
}
return 'Just now';
}
If you use the php Datetime class you could use:
function time_ago(Datetime $date) {
$time_ago = '';
$diff = $date->diff(new Datetime('now'));
if (($t = $diff->format("%m")) > 0)
$time_ago = $t . ' months';
else if (($t = $diff->format("%d")) > 0)
$time_ago = $t . ' days';
else if (($t = $diff->format("%H")) > 0)
$time_ago = $t . ' hours';
else
$time_ago = 'minutes';
return $time_ago . ' ago (' . $date->format('M j, Y') . ')';
}

- 1,054
- 1
- 9
- 23
-
If you change the else-if to simple if statements you could probably output months days hours seconds ago (but I felt it was too clutering). – Tessmore Apr 23 '13 at 18:12
Be warned, the majority of the mathematically calculated examples have a hard limit of 2038-01-18
dates and will not work with fictional dates.
As there was a lack of DateTime
and DateInterval
based examples, I wanted to provide a multi-purpose function that satisfies the OP's need and others wanting compound elapsed periods, such as 1 month 2 days ago
. Along with a bunch of other use cases, such as a limit to display the date instead of the elapsed time, or to filter out portions of the elapsed time result.
Additionally the majority of the examples assume elapsed is from the current time, where the below function allows for it to be overridden with the desired end date.
/**
* multi-purpose function to calculate the time elapsed between $start and optional $end
* @param string|null $start the date string to start calculation
* @param string|null $end the date string to end calculation
* @param string $suffix the suffix string to include in the calculated string
* @param string $format the format of the resulting date if limit is reached or no periods were found
* @param string $separator the separator between periods to use when filter is not true
* @param null|string $limit date string to stop calculations on and display the date if reached - ex: 1 month
* @param bool|array $filter false to display all periods, true to display first period matching the minimum, or array of periods to display ['year', 'month']
* @param int $minimum the minimum value needed to include a period
* @return string
*/
function elapsedTimeString($start, $end = null, $limit = null, $filter = true, $suffix = 'ago', $format = 'Y-m-d', $separator = ' ', $minimum = 1)
{
$dates = (object) array(
'start' => new DateTime($start ? : 'now'),
'end' => new DateTime($end ? : 'now'),
'intervals' => array('y' => 'year', 'm' => 'month', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second'),
'periods' => array()
);
$elapsed = (object) array(
'interval' => $dates->start->diff($dates->end),
'unknown' => 'unknown'
);
if ($elapsed->interval->invert === 1) {
return trim('0 seconds ' . $suffix);
}
if (false === empty($limit)) {
$dates->limit = new DateTime($limit);
if (date_create()->add($elapsed->interval) > $dates->limit) {
return $dates->start->format($format) ? : $elapsed->unknown;
}
}
if (true === is_array($filter)) {
$dates->intervals = array_intersect($dates->intervals, $filter);
$filter = false;
}
foreach ($dates->intervals as $period => $name) {
$value = $elapsed->interval->$period;
if ($value >= $minimum) {
$dates->periods[] = vsprintf('%1$s %2$s%3$s', array($value, $name, ($value !== 1 ? 's' : '')));
if (true === $filter) {
break;
}
}
}
if (false === empty($dates->periods)) {
return trim(vsprintf('%1$s %2$s', array(implode($separator, $dates->periods), $suffix)));
}
return $dates->start->format($format) ? : $elapsed->unknown;
}
One thing to note - the retrieved intervals for the supplied filter values do not carry over to the next period. The filter merely displays the resulting value of the supplied periods and does not recalculate the periods to display only the desired filter total.
Usage
For the OP's need of displaying the highest period (as of 2015-02-24).
echo elapsedTimeString('2010-04-26');
/** 4 years ago */
To display compound periods and supply a custom end date (note the lack of time supplied and fictional dates).
echo elapsedTimeString('1920-01-01', '2500-02-24', null, false);
/** 580 years 1 month 23 days ago */
To display the result of filtered periods (ordering of array doesn't matter).
echo elapsedTimeString('2010-05-26', '2012-02-24', null, ['month', 'year']);
/** 1 year 8 months ago */
To display the start date in the supplied format (default Y-m-d) if the limit is reached.
echo elapsedTimeString('2010-05-26', '2012-02-24', '1 year');
/** 2010-05-26 */
There are bunch of other use cases. It can also easily be adapted to accept unix timestamps and/or DateInterval objects for the start, end, or limit arguments.

- 17,883
- 4
- 67
- 69
I liked Mithun's code, but I tweaked it a bit to make it give more reasonable answers.
function getTimeSince($eventTime)
{
$totaldelay = time() - strtotime($eventTime);
if($totaldelay <= 0)
{
return '';
}
else
{
$first = '';
$marker = 0;
if($years=floor($totaldelay/31536000))
{
$totaldelay = $totaldelay % 31536000;
$plural = '';
if ($years > 1) $plural='s';
$interval = $years." year".$plural;
$timesince = $timesince.$first.$interval;
if ($marker) return $timesince;
$marker = 1;
$first = ", ";
}
if($months=floor($totaldelay/2628000))
{
$totaldelay = $totaldelay % 2628000;
$plural = '';
if ($months > 1) $plural='s';
$interval = $months." month".$plural;
$timesince = $timesince.$first.$interval;
if ($marker) return $timesince;
$marker = 1;
$first = ", ";
}
if($days=floor($totaldelay/86400))
{
$totaldelay = $totaldelay % 86400;
$plural = '';
if ($days > 1) $plural='s';
$interval = $days." day".$plural;
$timesince = $timesince.$first.$interval;
if ($marker) return $timesince;
$marker = 1;
$first = ", ";
}
if ($marker) return $timesince;
if($hours=floor($totaldelay/3600))
{
$totaldelay = $totaldelay % 3600;
$plural = '';
if ($hours > 1) $plural='s';
$interval = $hours." hour".$plural;
$timesince = $timesince.$first.$interval;
if ($marker) return $timesince;
$marker = 1;
$first = ", ";
}
if($minutes=floor($totaldelay/60))
{
$totaldelay = $totaldelay % 60;
$plural = '';
if ($minutes > 1) $plural='s';
$interval = $minutes." minute".$plural;
$timesince = $timesince.$first.$interval;
if ($marker) return $timesince;
$first = ", ";
}
if($seconds=floor($totaldelay/1))
{
$totaldelay = $totaldelay % 1;
$plural = '';
if ($seconds > 1) $plural='s';
$interval = $seconds." second".$plural;
$timesince = $timesince.$first.$interval;
}
return $timesince;
}
}

- 39
- 1
- 6
One option that'll work with any version of PHP is to do what's already been suggested, which is something like this:
$eventTime = '2010-04-28 17:25:43';
$age = time() - strtotime($eventTime);
That will give you the age in seconds. From there, you can display it however you wish.
One problem with this approach, however, is that it won't take into account time shifts causes by DST. If that's not a concern, then go for it. Otherwise, you'll probably want to use the diff() method in the DateTime class. Unfortunately, this is only an option if you're on at least PHP 5.3.

- 2,348
- 1
- 21
- 26
-
Didn't know DateTime::diff. It rocks ! On the PHP manual there is a retro portage of date_diff => http://us2.php.net/manual/en/datetime.diff.php#97810 – mexique1 May 26 '10 at 19:21
Use This one and you can get the
$previousDate = '2013-7-26 17:01:10';
$startdate = new DateTime($previousDate);
$endDate = new DateTime('now');
$interval = $endDate->diff($startdate);
echo$interval->format('%y years, %m months, %d days');
Refer this http://ca2.php.net/manual/en/dateinterval.format.php

- 349
- 1
- 3
- 13
Convert [saved_date] to timestamp. Get current timestamp.
current timestamp - [saved_date] timestamp.
Then you can format it with date();
You can normally convert most date formats to timestamps with the strtotime() function.

- 5,551
- 8
- 37
- 49
-
Unfortunately date() does not allow to calculate the number of days /minutes... between two dates... – mexique1 May 26 '10 at 19:22
Try one of these repos:
https://github.com/salavert/time-ago-in-words
https://github.com/jimmiw/php-time-ago
I just started using the latter, does the trick, but no stackoverflow-style fallback on exact date when the date in question is too far away, nor is there support for future dates - and the API is a little funky, but at least it works seemingly flawlessly and is maintained...

- 17,799
- 12
- 70
- 83
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; } ?>

- 5
- 2
You can get a function for this directly form WordPress core files take a look here
http://core.trac.wordpress.org/browser/tags/3.6/wp-includes/formatting.php#L2121
function human_time_diff( $from, $to = '' ) {
if ( empty( $to ) )
$to = time();
$diff = (int) abs( $to - $from );
if ( $diff < HOUR_IN_SECONDS ) {
$mins = round( $diff / MINUTE_IN_SECONDS );
if ( $mins <= 1 )
$mins = 1;
/* translators: min=minute */
$since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
} elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
$hours = round( $diff / HOUR_IN_SECONDS );
if ( $hours <= 1 )
$hours = 1;
$since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
} elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
$days = round( $diff / DAY_IN_SECONDS );
if ( $days <= 1 )
$days = 1;
$since = sprintf( _n( '%s day', '%s days', $days ), $days );
} elseif ( $diff < 30 * DAY_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
$weeks = round( $diff / WEEK_IN_SECONDS );
if ( $weeks <= 1 )
$weeks = 1;
$since = sprintf( _n( '%s week', '%s weeks', $weeks ), $weeks );
} elseif ( $diff < YEAR_IN_SECONDS && $diff >= 30 * DAY_IN_SECONDS ) {
$months = round( $diff / ( 30 * DAY_IN_SECONDS ) );
if ( $months <= 1 )
$months = 1;
$since = sprintf( _n( '%s month', '%s months', $months ), $months );
} elseif ( $diff >= YEAR_IN_SECONDS ) {
$years = round( $diff / YEAR_IN_SECONDS );
if ( $years <= 1 )
$years = 1;
$since = sprintf( _n( '%s year', '%s years', $years ), $years );
}
return $since;
}

- 18,333
- 14
- 83
- 102
-
take it from trunk ;) [https://core.trac.wordpress.org/browser/trunk/src/wp-includes/formatting.php](https://core.trac.wordpress.org/browser/trunk/src/wp-includes/formatting.php) – Nov 15 '14 at 07:49
Improvisation to the function "humanTiming" by arnorhs. It would calculate a "fully stretched" translation of time string to human readable text version. For example to say it like "1 week 2 days 1 hour 28 minutes 14 seconds"
function humantime ($oldtime, $newtime = null, $returnarray = false) {
if(!$newtime) $newtime = time();
$time = $newtime - $oldtime; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
$htarray = array();
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
$htarray[$text] = $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
$time = $time - ( $unit * $numberOfUnits );
}
if($returnarray) return $htarray;
return implode(' ', $htarray);
}

- 1,184
- 2
- 13
- 27
To find out time elapsed i usually use time()
instead of date()
and formatted time stamps.
Then get the difference between the latter value and the earlier value and format accordingly. time()
is differently not a replacement for date()
but it totally helps when calculating elapsed time.
example:
The value of time()
looks something like this 1274467343
increments every second. So you could have $erlierTime
with value 1274467343
and $latterTime
with value 1274467500
, then just do $latterTime - $erlierTime
to get time elapsed in seconds.

- 18,300
- 28
- 78
- 125
Wrote my own
function getElapsedTime($eventTime)
{
$totaldelay = time() - strtotime($eventTime);
if($totaldelay <= 0)
{
return '';
}
else
{
if($days=floor($totaldelay/86400))
{
$totaldelay = $totaldelay % 86400;
return $days.' days ago.';
}
if($hours=floor($totaldelay/3600))
{
$totaldelay = $totaldelay % 3600;
return $hours.' hours ago.';
}
if($minutes=floor($totaldelay/60))
{
$totaldelay = $totaldelay % 60;
return $minutes.' minutes ago.';
}
if($seconds=floor($totaldelay/1))
{
$totaldelay = $totaldelay % 1;
return $seconds.' seconds ago.';
}
}
}

- 49,883
- 70
- 181
- 236
-
8At least my code does not ave any for loops. And there is no act of disrespect as I have followed the way directed by the persons who answered here. – Mithun Sreedharan Jun 03 '10 at 11:30
Had to do this recently - hope this helps someone. It doesn't cater for every possibility, but met my needs for a project.
https://github.com/duncanheron/twitter_date_format
https://github.com/duncanheron/twitter_date_format/blob/master/twitter_date_format.php

- 61
- 9