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?
Asked
Active
Viewed 286 times
1 Answers
6
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?.

Community
- 1
- 1

Paige Ruten
- 172,675
- 36
- 177
- 197
-
beat me to it. (ps: gratz on 5000 rep) – nickf Nov 01 '08 at 06:44
-
If the input date format is fixed, it may be better to use strptime() – Tom Haigh Nov 01 '08 at 23:27