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 开发者_JAVA百科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?
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.
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.).
精彩评论