
1) Goto PHPfiddle.org 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.
2) Click on the "Code Space" tab on the top left to get to the editor portion.
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 blockchain.info's API. The useful stats API request (https://blockchain.info/stats?format=json) will provide us with all the information we need.
4) In the Code Space section at PHPfiddle, type the following between the php tags <?php ?>
$url = "https://blockchain.info/stats?format=json";5) The $ defines a variable, and we set it equal to blockchain's stats URL.
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);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 brakets. ][ The current block height is stored under "n_blocks_total"
$currentBlock = $json['n_blocks_total'];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 = "https://blockchain.info/stats?format=json"; $json = json_decode(file_get_contents($url), true); $currentBlock = $json['n_blocks_total']; echo $currentBlock;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;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;11) And that's it! From here you should be able to see how to create your own calculations! Put it all together
$url = "https://blockchain.info/stats?format=json"; $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 "<br>Approx Minutes Until Halving: ".number_format($minutesLeft); echo "<br>Approx Hours Until Halving: ".number_format($hoursLeft); echo "<br>Approx Days Until Halving: ".number_format($daysLeft);http://phpfiddle.org/lite/code/exjg-sb08