The timestamps on the blocks are written by the miner that built the block.
Computers suck at time, so the time of one machine may be minutes off another, also miners could be intentionally lying in the hopes of breaking or swaying something in the Bitcoin protocol.
Fortunately, this is all accounted for in the code. Timestamps are allowed a threshold of error when an active node sees it, so that reduces massive lies, and there is also a threshold, although more forgiving, applied when nodes are validating historical blocks during bootstrapping.
Also the difficulty adjustment algo in Bitcoin relies on the timestamps, but it relies on median averages instead of mean averages because that is less susceptible to biases.
Bitcoin has us covered.