---
title: "Find historical bitcoin block number with unix timestamp"
author: "sandwich"
npub: npub1uac67zc9er54ln0kl6e4qp2y6ta3enfcg7ywnayshvlw9r5w6ehsqq99rx
naddr: naddr1qvzqqqr4gupzpem34u9stj8ftlxldl4n2qz5f5hmrnxns3uga86fpwe7u28ga4n0qq5xv6twvskky6t5vdhkjm3dvfkx7cmt94h82mtzv4ez6amfw35z6arfd4jhxarpd4cqd0zdzg
published: 2024-09-05T22:12:02.000Z
updated: 2024-09-05T22:14:04.000Z
summary: "Script to find historical block number from timestamp."
---

I have searched the web for "Find bitcoin block number from historical date" maybe 100 times in my life. 

**Never again.**

You'll need a bitcoin node for this to work. The script is fast. 

# Install

## WoT Install
_one liner_
```
# curl
curl -o fjb.sh https://gist.githubusercontent.com/dskvr/18252c16cf85c06c1ee6cb5ae04a3197/raw/34bad6a35d98501978c8cd0c25b1628db1191cfe/fjb.sh && chmod +x fjb.sh

#wget
wget -O fjb.sh https://gist.githubusercontent.com/dskvr/18252c16cf85c06c1ee6cb5ae04a3197/raw/34bad6a35d98501978c8cd0c25b1628db1191cfe/fjb.sh && chmod +x fjb.sh
```

## Trust no one.
_create file_
```
vi fjb.sh
#or
nano fjb.sh
```

_review, copy and past into file_
```
TIMESTAMP=$1
LOWER=0
UPPER=$(bitcoin-cli getblockcount)

while (( LOWER <= UPPER )); do
    MID=$(( (LOWER + UPPER) / 2 ))
    BLOCKHASH=$(bitcoin-cli getblockhash $MID)
    BLOCKTIME=$(bitcoin-cli getblockheader $BLOCKHASH | jq .time)

    if (( BLOCKTIME < TIMESTAMP )); then
        LOWER=$(( MID + 1 ))
    elif (( BLOCKTIME > TIMESTAMP )); then
        UPPER=$(( MID - 1 ))
    else
        echo "$BLOCKTIME"
        exit 0
    fi
done

echo "$UPPER"
```

_give executable permissions_
```
chmod +x ./fjb.sh
```

# Usage
```
./fjb.sh <timestampSeconds> 
```
_It returns block number only_

# Example
```
$: ./fjb.sh 1668779310
763719
```
