I'm trying the set up a user registration system that creates a token that expires after 48 hours (2 days). I attempted to program this but to no avail. When I test the code I get this error
A PHP Error was encountered
Severity: Error
Message: Class 'TimeDate' not found
Filename: models/User_model.php
Line Number: 58
Below is the code
public function isTokenValid($token)
{
$q = $this->db->get_where('tokens', array('token' => $token), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
$created = $row->created;
$createdTS = strtotime($created);
$expiredate = new TimeDate('Y-m-d');
//$todayTS = strtotime($today);
$expiredate->add(new DateInterval('P2D'));
if($createdTS != $expiredate){
return false;
}
$user_info = $this->getUserInfo($row->user_id);
return $user_info;
}else{
return false;
}
}
you could also use strtotime()
Assuming:
// the timestamp the token was created
$created = $row->created;
Since the token will expire after 48 hours, we need to add 48 hours to date variable $created
$expireDate = date("Y-m-d H:i:s", strtotime($created . " + 48 hours"));
If you only want the date part and not the time, you can omit the H:i:s
You can now compare the $expireDate
to today's timestamp.
$today = date("Y-m-d H:i:s");
if (strtotime($today) > strtotime($expire)) {
echo "expired";
} else {
echo "not yet";
}