A timestamp is a numerical value that represents a specific moment in time. It’s often used to track when a certain event occurred or when a piece of data was created or modified.
The most common way to represent a timestamp is by using the number of seconds that have elapsed since a specific moment in time, in Solidity and most other languages this is January 1st, 1970, at midnight (UTC), also known as the Unix epoch.
To obtain a timestamp in Solidity we can use a global variable that returns the number of seconds that have elapsed since epoch time.
uint secondsPassed = block.timestamp;
The block.timestamp
property in Solidity returns the elapsed time in seconds between the Unix epoch and the moment the block was validated. Note that this value is somewhat manipulatable, to an extent, because the validator node decides the exact moment when the block was added, which determines the value of the block.timestamp value.
Solidity also accepts some time units such as seconds, minutes, hours, days, and weeks, which are converted to their respective value in seconds.
uint oneYearInSeconds = 365 days;