When you create the code and store it in the DB, also create a timestamp:
Code:
insert into whatever_table
(user_id, code, created_at)
values( ?, ?, NOW() );
Then when you want to check for a valid code:
Code:
select * from whatever_table
where user_id = ?
and code = ?
and created_at > ( NOW() - interval 1 hour );