drnessie
drnessie

Reputation: 897

PHP * hours ago

I have a time string which looks like this

2011-06-11T08:51:51Z

I'm not sure what this time format is, but I know it's not pretty to look at. Could someone help me convert it to "* hours ago" format

Upvotes: 2

Views: 3218

Answers (4)

Tgr
Tgr

Reputation: 28160

In PHP 5.3, you can use DateInterval:

$date = new DateTime('2011-06-11T08:51:51Z');
$now = new DateTime();
$diff = $now->diff($date);
echo $diff->format('h');

Upvotes: 4

konsolenfreddy
konsolenfreddy

Reputation: 9671

$tstamp = strtotime('2011-06-11T08:51:51Z'); 

will give you the timestamp, calculate the number of hours like that:

$hours = floor((time() - $stamp()) / 3600);

Upvotes: 3

Nobita
Nobita

Reputation: 23713

I would do this:

  1. Get the timestamp of that string: $timestamp = strtotime($time_to_convert);
  2. Get the actual timestamp: $actual_timestamp = time();
  3. Substract: $dif_timestampm = $actual_timestamp - $timestamp;
  4. Pass it to hours: $hours = $dif_timestampm / 3600;

Upvotes: 0

Chris Browne
Chris Browne

Reputation: 1612

First thing you need to do is convert it into a number (strtotime()), then compare it to the current time (time()) and finally divide the result by 3600 (the number of seconds in an hour).

Upvotes: 0

Related Questions