Date histogram aggregation returns a wrong number of buckets and wrong start date of each bucket

I did not check in details, but you should make sure that the dates you are sending to Elasticsearch
like 1636727017825 are UTC.
If you are not using a proper date format with the timezone, then Elasticsearch considers the date as UTC based.

If it's not exactly the problem here, could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

Please replace the ms since epoch dates by a readable date so we can better understand what is happening.

A full reproduction script is something anyone can copy and paste in Kibana dev console, click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.