This question already has an answer here:
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
This question already has an answer here:
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
I liked Mithun's code, but I tweaked it a bit to make it give more reasonable answers.
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
andDateInterval
based examples, I wanted to provide a multi-purpose function that satisfies the OP's need and others wanting compound elapsed periods, such as1 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.
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).
To display compound periods and supply a custom end date (note the lack of time supplied and fictional dates).
To display the result of filtered periods (ordering of array doesn't matter).
To display the start date in the supplied format (default Y-m-d) if the limit is reached.
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.
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...
Wrote my own
To find out time elapsed i usually use
time()
instead ofdate()
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 fordate()
but it totally helps when calculating elapsed time.example:
The value of
time()
looks something like this1274467343
increments every second. So you could have$erlierTime
with value1274467343
and$latterTime
with value1274467500
, then just do$latterTime - $erlierTime
to get time elapsed in seconds.