0

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), 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?

  • Try to use 'bitcoin database viewer' 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. – D L Apr 18 '18 at 0:32
0

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.

  • Hello Adam, If I run your code in a PHP editor (e.g. jdoodle.com/php-online-editor) , it says there is an error. – Camilo Apr 17 '18 at 22:39
  • 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. – Adam Apr 18 '18 at 1:12
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)
  }
  • This code uses sites API? I think that number of requests is strongly limited for remote services – D L Apr 18 '18 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.