Loading...
As we work with the etherum blockchain network.we want to support our contract differently that after some time or the after particular time when the event occur for that etherum mainly contain the block.timestamp program which is the alise of the now program mainly.In this smartbook i will try to explain how the time worked in the blockchain. So let's started.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract time{
uint256 createtime;
function currenttime() public {
createtime = block.timestamp;
}
function returntime() public view returns(uint256)
{
return createtime;
}
}
In the above code i will created one contract in which we created one integer which is called as a createtime.Then we created the function called as a currenttime. Then we store the current creation of the block time in the previously define integer called as a createtime.
After this we created the another function called as a returntime through which we return the current that we store in the integer called as a create time that's it.It is simple to know the current time of the block and then work on that time to specify or the create the changes in your code.
so above program show you how the actually save or work in the etherum network with the solidity with the help of the block.timestamp function.But what if you want to specify one timer which mainly show you that what is the deadline and how much time is remain to you during finish of your required time.
function timer(uint256 minute) public payable
{
deadline = block.timestamp + (minute * 60 seconds);
}
function remaintime() public view returns(uint256 remaintime)
{
remaintime = deadline-block.timestamp;
return remaintime;
}
In solidity it mainly support the integer value one that mean if you assign 5 minutes timer then it should be converted into the seconds first so for that in the above code we assign one integer called as a minute and through which we want input from the user about the timer is for how much minutes the timer is required.Then at last we create the final function through which you can see how much time is remain to complete the timer basucally and at the end it return the remaining time.
thank you!