Blockchain Blog

Using the Blockchain API to determine the time left until the next bitcoin block halving

![Countdown clock image via Tony Webster/Wikimedia ](
Countdown clock image via Tony Webster/Wikimedia
Earlier this year, we did a [featured spotlight]( on a bitcoin developer Kyle Honeycutt who does tutorials focused on helping explain how to build exciting new tools leveraging the Blockchain suite of services, including our [API](

Kyle has impressed us again, by coming up with a novel way to calculate the amount of time left until the bitcoin block reward halving happens in real-time. For those unfamiliar with the bitcoin halving, here is a quick explanation pulled from our 10 commonly used bitcoin terms article.

Bitcoins have a finite supply, which makes them scarce. The total amount that will ever be issued is 21 million. The number of bitcoins generated per block is decreased 50% every four years. This is called “halving.” The final halving will take place in the year 2140.

If you’d like a deeper understanding of the bitcoin halving, you can read *Block Reward Halving: A Guide*written by Vitalik Buterin, who is the founder of Ethereum. There is also another more recent article which talks about the different aspects of the bitcoin halving which is a good read.

Getting into the code

Kyle, who enjoys teaching people how to setup tools and services from top-to-bottom, walked users through a tutorial using the Blockchain Stats page which parses the data into JSON format for developers who want to programmatically pull the data. We are re-sharing the tutorial that Kyle published, since it’s a practical learning tool for developers. This walkthrough will give you a step-by-step on how to build out a real-time bitcoin halving display that you can put on your website, or maybe projected in your office.

Step 1: Goto so you can execute PHP code without having a server or downloading anything. Unlike HTML, with PHP you can’t just run it on your computer.

Step 2: Click on the “Code Space” tab on the top left to get to the editor portion.

Step 3: The block rewards half after every 210,000 blocks. So in order to determine the time we’ll first need to know which block we are on currently. We can get this with’s API. The useful stats API request will provide us with all the information we need.

Step 4:  In the Code Space section at PHPfiddle, type the following between the php tags

$url = "";

Step 5: The $ defines a variable, and we set it equal to Blockchain’s stats URL.

Step 6: This URL is in JSON format, we are going to tell PHP to get the information in JSON format. Add the following below your first line.

$json = json_decode(file_get_contents($url), true);

Step 7: Now we need to tell PHP which information from the JSON output that we want. JSON puts all the results into arrays, and we can tell PHP which data we want using straight brackets. The current block height is stored under “n_blocks_total”.

$currentBlock = $json['n_blocks_total'];

Step 8: You can test this to make sure it works by echoing it out. Let’s combine it all and test if it provides the current block height.

$url = ""; $json = json_decode(file_get_contents($url), true); $currentBlock = $json['n_blocks_total']; echo $currentBlock;

Step 9: Now that we have the current block height we can start the calculations. The next block halving will happen at block height 420,000. So we can figure out how many blocks are left until the halving by subtracting.

$blocksLeft = 420000 - $currentBlock;

Step 10: Another useful figure with Blockchain’s stats request is “minutes_between_blocks”. This tells us how long it takes to solve a block, on average. A few more simple calculations can tell us how many more minutes, hours, days etc, until the block halving.

$minutesBetween = $json['minutes_between_blocks']; $minutesLeft = $blocksLeft * $minutesBetween; $hoursLeft = $minutesLeft / 60; $daysLeft = $hoursLeft / 24;

Step 11: And that’s it! From here you should be able to see how to create your own calculations. Put it all together like this:

$url = ""; $json = json_decode(file_get_contents($url), true); $currentBlock = $json['n_blocks_total']; $blocksLeft = 420000 - $currentBlock; $minutesBetween = $json['minutes_between_blocks']; $minutesLeft = $blocksLeft * $minutesBetween; $hoursLeft = $minutesLeft / 60; $daysLeft = $hoursLeft / 24; echo "Blocks Remaining Until Halving: ".number_format($blocksLeft); echo "
Approx Minutes Until Halving: ".number_format($minutesLeft); echo "
Approx Hours Until Halving: ".number_format($hoursLeft); echo "
Approx Days Until Halving: ".number_format($daysLeft);

You can view the final code output on PHPfiddle, just click Run to run the code and show the results. Do you have a code tutorial that uses the Blockchain API that you want to share with us? Let us know in the comments below!