tags:

views:

105

answers:

1

The event returns a standardized timestamp in the format of "Sat Oct 25 21:55:29 +0000 2008" How can I compare this with the current time in PHP so that it gives me the result in a form like "It has been 15 minutes!" or "It has been three days!" Also, how can I get the current time?

+6  A: 

First convert that string to a UNIX timestamp using strtotime():

$event_time = strtotime('Sat Oct 25 21:55:29 +0000 2008');

Then to get the current UNIX timestamp use time():

$current_time = time();

Then get the number of seconds between those timestamps:

$difference = $current_time - $event_time;

And finally use division to convert the number of seconds into number of days or hours or whatever:

$hours_difference = $difference / 60 / 60;
$days_difference = $difference / 60 / 60 / 24;

For more information on calculating relative time, see How do I calculate relative time?.

yjerem
beat me to it. (ps: gratz on 5000 rep)
nickf
I reached my limit unfortunately, but thanks :P
yjerem
If the input date format is fixed, it may be better to use strptime()
Tom Haigh