30-second intervals since 1970 at the moment T return self::generateCodeFromTimestamp($secret, $timestamp); } /** * Verify TOTP Code * * @param string $code Digits 6 TOTP Code * @param string $secret Encoded base32 secret * @param int $timestamp timestamp used in second (default: 30) * @param int $check_interval Number of 30s steps to check before/after current (default: 1) * @return bool */ public static function verifyCode($code, $secret, $timestamp = 30, $check_interval = 1) { $timestamp = floor(time() / $timestamp); // generate a totp code for 30s intervals // following or preceding the current one and check it for ($i=-$check_interval; $i <= $check_interval; $i++) { $interval_timestamp = $timestamp + $i; $generated_code = self::generateCodeFromTimestamp($secret, $interval_timestamp); if (hash_equals($generated_code, $code)) { return true; } } return false; } }