<p>I've written this simple PHP code with an explanation for each comma</p>

<p><strong>PHP code</strong></p>

<pre><code>&lt;?php

$lastBlockEnd = $this-&gt;MyRPC-&gt;BitcoinCoreCommand('getblockcount' , []);
// https://bitcoin.org/en/developer-reference#getblockcount

for ($i = 0; $i &lt;= $lastBlockEnd; $i++){
    $blockHash = $this-&gt;MyRPC-&gt;BitcoinCoreCommand('getblockhash' , [$i]);
    //https://bitcoin.org/en/developer-reference#getblockhash
    $blockData = $this-&gt;MyRPC-&gt;BitcoinCoreCommand('getdata' , [$blockHash]);
    //https://bitcoin.org/en/developer-reference#getblock
    $block[$i]['time'] = $blockData['time'];
    $block[$i]['hash'] = $blockHash;
}

print_r($block);
</code></pre>

<p><strong>Code Explanation</strong></p>

<ul>
<li>We are getting total available blocks number, say its 65443</li>
<li>we will start looping from block number 0 to 65443</li>
</ul>

<blockquote>
  <p>Note: You should send "batch request" instead of calling each block separately 
  - we are getting block hash, then we're calling <code>getdata</code> and passing that block hash.</p>
</blockquote>

<p>Now we've got the following response:</p>

<pre><code>{
    "hash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
    "confirmations": 447014,
    "strippedsize": 215,
    "size": 215,
    "weight": 860,
    "height": 1,
    "version": 1,
    "versionHex": "00000001",
    "merkleroot": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
    "tx": [
        "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
    ],
    "time": 1231469665,
    "mediantime": 1231469665,
    "nonce": 2573394689,
    "bits": "1d00ffff",
    "difficulty": 1,
    "chainwork": "0000000000000000000000000000000000000000000000000000000200020002",
    "previousblockhash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
    "nextblockhash": "000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd"
}
</code></pre>

<p>Finally, We are storing <code>time</code> and block hash in an array and printing it.</p>

CC BY-SA 3.0
2018-04-17T22:39:03.860Z
Hello Adam, If I run your code in a PHP editor (e.g. https://www.jdoodle.com/php-online-editor) , it says there is an error.
Camilo
CC BY-SA 3.0
2018-04-18T01:12:00.680Z
if you copy and paste it, it shouldn't work. You have to connect to your Bitcoin-core server and setup RPC connection. the code is just a sample of required commands and how you can implement it.
Tailer
CC BY-SA 3.0
2018-04-17T21:41:42.997Z
2018-04-17T21:41:42.997Z
Tailer
<p>After some searching, I found a very nice code in R, that queries Bitcoin data from several API.The code works copying and pasting, pretty much, although it is slow. 
Credit to that person, but decided to post it here as I can see people using this for analytical purposes.</p>

<p><a href="https://github.com/organofcorti/bitcoin-blockchain-data" rel="nofollow noreferrer">https://github.com/organofcorti/bitcoin-blockchain-data</a></p>

<p>So, for use in R, here I extracted the code that queries data from blocktrail</p>

<pre><code>library(RJSONIO)

  nullToNA &lt;- function(x) {
  x[sapply(x, is.null)] &lt;- NA
  return(x)
  } #ensure null data are kept


  MY_APIKEY &lt;- "6debaf0ebd4c9081795fe38716df550c46ab06fb"
        # API url
        block_url   &lt;- "https://api.blocktrail.com/v1/btc/block/"
        APIkey      &lt;- paste0("?api_key=", MY_APIKEY)

  Data=data.frame()
  for (x in 446097:502027) {      #blocks for 2017
  block_data_list_0 &lt;- nullToNA(fromJSON(paste0(block_url, x, APIkey)))
  datax=data.frame(t(unlist(block_data_list_0)) )
  Data=rbind(Data,datax)
  }
</code></pre>

CC BY-SA 3.0
2018-04-18T22:39:55.740Z
This code uses sites API? I think that number of requests is strongly limited for remote services
Denis Leonov
CC BY-SA 3.0
2018-04-18T18:44:14.290Z
2018-04-19T03:26:17.047Z
2018-04-19T03:26:17.047Z
Camilo
<p>You can use any language that is able to talk to a HTTP server, which your node's <a href="https://developer.bitcoin.org/reference/rpc/" rel="nofollow noreferrer">RPC API</a> essentially is. The requests &amp; responses are in <code>json</code> format.</p>
<p>For my own needs I have a little Bash script (you need have <code>curl</code> and <code>jq</code> command-line tools installed, I use that for querying the HTTP API and converting response from <code>json</code> to <code>csv</code>) that I run to extract data like what you need, here you go:</p>
<pre><code>#!/bin/bash
num=$1
res=''
while [ &quot;${res:0:1}&quot; != ',' ]
do
    hash=$(curl -s --user bitcoin:bitcoin --data-binary '{&quot;jsonrpc&quot;: &quot;1.0&quot;, &quot;id&quot;:&quot;curltest&quot;, &quot;method&quot;: &quot;getblockhash&quot;, &quot;params&quot;: ['$num'] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/ | jq -r '.result')
    res=$(curl -s --user bitcoin:bitcoin --data-binary '{&quot;jsonrpc&quot;: &quot;1.0&quot;, &quot;id&quot;:&quot;curltest&quot;, &quot;method&quot;: &quot;getblock&quot;, &quot;params&quot;: [&quot;'$hash'&quot;, 1] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/ | jq -r '[(.result | .height, .time, .mediantime)] | join(&quot;,&quot;)')
    [ &quot;${res:0:1}&quot; != ',' ] || break
    echo $res
    num=$((num+1))
done
</code></pre>
<p>The argument is the starting height, and you can pipe the output to a <code>.csv</code> file, then use whatever sofware you want to process the data.
Usage:</p>
<pre><code>./script.sh &lt;height&gt; &gt; output.csv
</code></pre>
<p>This is how I configured the node to serve RPC API requests only to requests coming from the same machine, my <a href="https://manpages.org/bitcoinconf/5" rel="nofollow noreferrer"><code>bitcoin.conf</code></a> has just this:</p>
<pre><code>[main]
rpcport=8332
rpcuser=bitcoin
rpcpassword=bitcoin
rpcallowip=127.0.0.1
server=1
</code></pre>

CC BY-SA 4.0
2023-11-23T07:16:57.963Z
2023-11-23T07:39:22.803Z
2023-11-23T07:39:22.803Z
bca-0353f40e
<p>Is it possible to obtain the date (time stamp) of each bitcoin block so far, using languages like PHP or R?. </p>

<p>I am developing an analysis of the time series of bitcoin use for a scientific project, and while many sources provide averages, few provide the raw data. </p>

<p>In I related post (<a href="https://bitcoin.stackexchange.com/questions/74035/raw-data-of-transactions-in-all-blocks">Raw data of transactions in all blocks</a>), a person (amaclin) kindly provided the number of transactions in all existing blocks, could it be possible to obtain the date?.</p>

<p>Is this something that can be done in R or PHP? </p>

CC BY-SA 3.0
2018-04-18T00:32:08.583Z
Try to use 'bitcoin database viewer' https://sourceforge.net/projects/bitcoin-database-viewer/ This console app can convert bitcoin database from raw format to human readable view without using of any sites or APIs. So when you parse dat files with that app, you will see all that you need.
Denis Leonov
<blockchain><raw-data><block>
Raw data of dates in all blocks using PHP or R
Camilo
# Raw data of dates in all blocks using PHP or R

(Asked by: username_0 on 2018-04-17)

Is it possible to obtain the date (time stamp) of each bitcoin block so far, using languages like PHP or R?.

I am developing an analysis of the time series of bitcoin use for a scientific project, and while many sources provide averages, few provide the raw data.

In I related post ([Raw data of transactions in all blocks](https://bitcoin.stackexchange.com/questions/74035/raw-data-of-transactions-in-all-blocks)), a person (amaclin) kindly provided the number of transactions in all existing blocks, could it be possible to obtain the date?.

Is this something that can be done in R or PHP?

**Tags:** blockchain, raw-data, block

> **Comments:**
> **username_1**: Try to use 'bitcoin database viewer' https://sourceforge.net/projects/bitcoin-database-viewer/ This console app can convert bitcoin database from raw format to human readable view without using of any sites or APIs. So when you parse dat files with that app, you will see all that you need.  

---

## 3 answers

### Answer by username_2 on 2018-04-17. Score: 1

I've written this simple PHP code with an explanation for each comma

**PHP code**

```
<?php

$lastBlockEnd = $this->MyRPC->BitcoinCoreCommand('getblockcount' , []);
// https://bitcoin.org/en/developer-reference#getblockcount

for ($i = 0; $i <= $lastBlockEnd; $i++){
    $blockHash = $this->MyRPC->BitcoinCoreCommand('getblockhash' , [$i]);
    //https://bitcoin.org/en/developer-reference#getblockhash
    $blockData = $this->MyRPC->BitcoinCoreCommand('getdata' , [$blockHash]);
    //https://bitcoin.org/en/developer-reference#getblock
    $block[$i]['time'] = $blockData['time'];
    $block[$i]['hash'] = $blockHash;
}

print_r($block);

```

**Code Explanation**

* We are getting total available blocks number, say its 65443
* we will start looping from block number 0 to 65443

> Note: You should send "batch request" instead of calling each block separately
> - we are getting block hash, then we're calling `getdata` and passing that block hash.

Now we've got the following response:

```
{
    "hash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
    "confirmations": 447014,
    "strippedsize": 215,
    "size": 215,
    "weight": 860,
    "height": 1,
    "version": 1,
    "versionHex": "00000001",
    "merkleroot": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
    "tx": [
        "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
    ],
    "time": 1231469665,
    "mediantime": 1231469665,
    "nonce": 2573394689,
    "bits": "1d00ffff",
    "difficulty": 1,
    "chainwork": "0000000000000000000000000000000000000000000000000000000200020002",
    "previousblockhash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
    "nextblockhash": "000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd"
}

```

Finally, We are storing `time` and block hash in an array and printing it.

> **Comments:**
> **username_0**: Hello Adam, If I run your code in a PHP editor (e.g. https://www.jdoodle.com/php-online-editor) , it says there is an error.  
> **username_2**: if you copy and paste it, it shouldn't work. You have to connect to your Bitcoin-core server and setup RPC connection. the code is just a sample of required commands and how you can implement it.  

---

### Answer by username_0 on 2018-04-18. Score: 0

After some searching, I found a very nice code in R, that queries Bitcoin data from several API.The code works copying and pasting, pretty much, although it is slow.
Credit to that person, but decided to post it here as I can see people using this for analytical purposes.

<https://github.com/organofcorti/bitcoin-blockchain-data>

So, for use in R, here I extracted the code that queries data from blocktrail

```
library(RJSONIO)

  nullToNA <- function(x) {
  x[sapply(x, is.null)] <- NA
  return(x)
  } #ensure null data are kept

  MY_APIKEY <- "6debaf0ebd4c9081795fe38716df550c46ab06fb"
        # API url
        block_url   <- "https://api.blocktrail.com/v1/btc/block/"
        APIkey      <- paste0("?api_key=", MY_APIKEY)

  Data=data.frame()
  for (x in 446097:502027) {      #blocks for 2017
  block_data_list_0 <- nullToNA(fromJSON(paste0(block_url, x, APIkey)))
  datax=data.frame(t(unlist(block_data_list_0)) )
  Data=rbind(Data,datax)
  }

```

> **Comments:**
> **username_1**: This code uses sites API? I think that number of requests is strongly limited for remote services  

---

### Answer by username_3 on 2023-11-23. Score: 0

You can use any language that is able to talk to a HTTP server, which your node's [RPC API](https://developer.bitcoin.org/reference/rpc/) essentially is. The requests & responses are in `json` format.

For my own needs I have a little Bash script (you need have `curl` and `jq` command-line tools installed, I use that for querying the HTTP API and converting response from `json` to `csv`) that I run to extract data like what you need, here you go:

```
#!/bin/bash
num=$1
res=''
while [ "${res:0:1}" != ',' ]
do
    hash=$(curl -s --user bitcoin:bitcoin --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getblockhash", "params": ['$num'] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/ | jq -r '.result')
    res=$(curl -s --user bitcoin:bitcoin --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getblock", "params": ["'$hash'", 1] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/ | jq -r '[(.result | .height, .time, .mediantime)] | join(",")')
    [ "${res:0:1}" != ',' ] || break
    echo $res
    num=$((num+1))
done

```

The argument is the starting height, and you can pipe the output to a `.csv` file, then use whatever sofware you want to process the data.
Usage:

```
./script.sh <height> > output.csv

```

This is how I configured the node to serve RPC API requests only to requests coming from the same machine, my [`bitcoin.conf`](https://manpages.org/bitcoinconf/5) has just this:

```
[main]
rpcport=8332
rpcuser=bitcoin
rpcpassword=bitcoin
rpcallowip=127.0.0.1
server=1

```


---

