Hard worker
Hard worker

Reputation: 4066

How to work out time elapsed from this datetime stamp and return it in either minutes, hours or days as appropriate

I'm dealing with an api which is returning the date time stamp exactly as follows:

Mon, 14 May 2012 14:14:11 +0000

I would like process this so php works out how many minutes ago this was if the number of minutes is less than 60 else how many hours ago it was if the number of hours is less than 24 else the number of days.

Dates will never be older than a few weeks.

Thanks.

Upvotes: 0

Views: 101

Answers (1)

Cfreak
Cfreak

Reputation: 19319

You want to use the DateTime class. It can parse that date.

$now = new DateTime('now');
$dt = new DateTime('Mon, 14 May 2012 14:14:11 +0000');
$interval = $now->diff($dt);

$minutes = $interval->format('%i');

Note that 'now' will be in your current time zone so you might want to pass the DateTimeZone parameters as well. More info is here: http://php.net/DateTime

The class should already be built into your PHP. You won't need to include it.

Upvotes: 1

Related Questions