0

I am working on a project to determinate the age of a url/post listed in a feedburner feed. I can parse, and get the pubDate value in a format like this: Sat, 13 Jul 2013 10:00:15 +0000

I would like to create a PHP script that subtract the original datetime from the current one. I already created strtotime($item->pubDate), but I am confused by the time zones. The script must work with every feed (maybe in different time zones), and the script must be work properly from every timezone.

So how can I determine the age of the post based on these specifications?

Patartics Milán
  • 4,858
  • 4
  • 26
  • 32

1 Answers1

0

You can use the DateTime class for this, specifically the static method `DateTime::createFromFormat()', something like this:-

$dateStr = 'Sat, 13 Jul 2013 10:00:15 +0000';
$dateTime = \DateTime::createFromFormat('D, d M Y H:i:s O', $dateStr);
$dateTime->setTimezone(new \DateTimeZone('UTC'));
var_dump($dateTime);
$diff = $dateTime->diff(new \DateTime('now', new \DateTimeZone('UTC')));
var_dump($diff);

Output:-

object(DateTime)[1]
  public 'date' => string '2013-07-13 10:00:15' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

object(DateInterval)[3]
  public 'y' => int 0
  public 'm' => int 0
  public 'd' => int 0
  public 'h' => int 22
  public 'i' => int 47
  public 's' => int 48
  public 'invert' => int 0
  public 'days' => int 0

As you can see each dateTime object has had its timezone set to UTC. $diff is an instance of DateInterval which represents the difference between 'now' and the date you received.

You can output the difference between the dates in any format you wish using 'DateInterval::format()'

vascowhite
  • 18,120
  • 9
  • 61
  • 77