1

Basically, I have a time stamp that looks like 2010-12-15 16:14:06. If the time stamp is seconds old I want it transform it into "N Seconds Old." If the time stamp is minutes old, I want it to transform it into "N Minutes Old." etc. Is there any built in PHP functions that will do this?

2
  • 2
    Short answer: No. But you can create your own ;) Feb 2, 2011 at 20:14
  • 3
    Google PHP relative time and you will find many scripts for this.
    – alexn
    Feb 2, 2011 at 20:20

2 Answers 2

2

if you're running 5.3 you can use DateTime::diff to do it. It returns a DateInterval object which contains the info you'll want.

0

As mentioned by others, you need to have some "starting point" to measure against. There are a number of ways to turn a time stamp such as 2010-12-15 16:14:06 into a PHP timestamp (seconds since 1970-01-01 00:00:00). If that's your "starting point", simply subtract it from the current time or whatever to get the difference in seconds. If less than a certain amount (say, 60), give that as "N seconds ago". If greater, divide by 60 to get minutes ago, or 3600 to get hours ago, etc.).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.