Refer below link for code:
https://djtechblog.com/php/make-otp-e...
Making otp expire after certain minutes is very important from security of point of view. In order to generate random otp we we use php rand() function. Though, this function generates random number but it is not secured. The otp can be manipulated or guessed by the craker given a considearble amount of time. So, we need to set a time limit on the otp generated and beyond this time the otp will expire and user need to regenerate the otp. So, a secured web application in php needs otp automatically expires after ceratin minutes so that user regenarte otp. This is very important in making security web application