web analytics

logo Meta Data Science

By Massoud Seifi, Ph.D. Data Scientist

How to Estimate the Facebook Account Creation Date

Facebook Graph API and FQL don’t provide you with a simple way of getting the creation date of a Facebook account. But if you have a valid Facebook Access Token with ‘read_stream’ permission, it is possible to estimate the Facebook account creation date by finding the creation date of the oldest user post. According to the Facebook documentation,

each query of the stream table is limited to the previous 30 days or 50 posts, whichever is greater, however you can use time-specific fields such as created_time along with FQL operators (such as < or >) to retrieve a much greater range of posts.

Also you must have ‘read_stream’ permission:

Querying without the ‘read_stream’ permission will return only the public view of the data (i.e. data that can be see when the user is logged out).

Here is some code to do that:

Estimate the Facebook account age (AccountAge.php) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
 * Estimating the Facebook account age by finding the creation date of the oldest post.
 * A valid Facebook Access Token with read_stream permission is required.
 *
 * @author Massoud Seifi, Ph.D. @ MetaDataScience.com
 */

class AccountAge
{

	public $baseUrl;

	function __construct()
	{
		$this->baseUrl = 'https://graph.facebook.com/';
	}

	/**
	 * Run a Facebook FQL query
	 * @param string $fql Facebook query 
	 * @param string $access_token Facebook Access Token
	 * @return array Return Facebook query result
	 */
	public function doFQLRequest($fql, $access_token)
	{
		$url = $this->baseUrl	. 'fql?q=' . urlencode($fql)
			. '&access_token=' . $access_token;
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

		$decodedResult = json_decode(curl_exec($ch), true);
		curl_close($ch);

		$result = array();
		if(isset($decodedResult['data']))
			$result = $decodedResult['data'];
		else
			throw new Exception("Facebook FQL Error. Please check if the access token is valid.\n");

		return $result;
	}

	/**
	 * Estimate the account age by finding the creation date of the oldest post
	 * @param string $access_token Facebook Access Token
	 * @return integer Return the Facebook account age in seconds
	 */
	public function getAccountAge($access_token)
	{
		$date = new \DateTime('now');
		$timestamp = $date->getTimestamp();
		echo "# Finding the oldest post may take several minutes to complete.\n";
		echo "# Please wait ";
		while (true){ // Loop until finding the oldest post
			echo ".";
			$fql = "SELECT created_time FROM stream WHERE source_id = me()"
				. " AND created_time < " . $timestamp
				. " ORDER BY created_time ASC LIMIT 5000";
			$result = $this->doFQLRequest($fql, $access_token);
			if (!isset($result[0]['created_time']))
				break;
			$timestamp = $result[0]['created_time'];
		}
		echo "\n";
		$age = $date->getTimestamp() - $timestamp;
		return $age;
	}


	/**
	 * Display the account age in a human readable format
	 * @param int $age Account age in seconds
	 */
	public function printAccountAge($age)
	{
		$years = floor($age / (365*24*60*60));
		$months = floor(($age - $years * 365*24*60*60) / (30*24*60*60));
		$days = floor(($age - $years * 365*24*60*60 - $months * 30*24*60*60) / (24*60*60));
		echo "\nAccount age: $years years, $months months, $days days\n";	
	}

}

$p = new AccountAge();
// You need an Access Token with a read_stream permission
$access_token = 'AAACEdEose0cBAOY7bB3A9m7s3U6hbuJvfECxuZBFRN6YjqPC2eZB5x8WrnK51Gl3WsdwYovmxdPZCKFyJKB5TuFhpxsDJpAZCe9y6eutyQZDZD';
$age = $p->getAccountAge($access_token);
$p->printAccountAge($age);

Comments