How can one implement a password expiration feature in PHP?

To implement a password expiration feature in PHP, you can store a timestamp of when the password was last updated in the user's database record. Then, whenever a user logs in, you can check if the password has expired based on a predefined expiration period. If the password has expired, prompt the user to update their password.

// Check if the password has expired
function isPasswordExpired($lastUpdated, $expirationPeriod) {
    $currentTime = time();
    $expirationTime = strtotime($lastUpdated) + $expirationPeriod;
    
    if ($currentTime > $expirationTime) {
        return true;
    } else {
        return false;
    }
}

// Usage
$lastUpdated = "2022-01-01 12:00:00";
$expirationPeriod = 60 * 60 * 24 * 30; // 30 days
if (isPasswordExpired($lastUpdated, $expirationPeriod)) {
    echo "Your password has expired. Please update it.";
}