PHP code example of scottlaurent / fsrs

1. Go to this page and download the library: Download scottlaurent/fsrs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

scottlaurent / fsrs example snippets




use Scottlaurent\FSRS\Manager;
use Scottlaurent\FSRS\Card;

// Create a scheduler with default settings
$fsrs = new Manager();

// Create a new card
$card = new Card(new DateTime('2024-01-01', new DateTimeZone('UTC')));

// Generate scheduling options for different ratings
$schedule = $fsrs->generateRepetitionSchedule($card);

// Choose a rating and get the updated card
// 1 = Again, 2 = Hard, 3 = Good, 4 = Easy
$updatedCard = $schedule[3]->card; // User rated "Good"

echo "Next review: " . $updatedCard->due->format('Y-m-d H:i:s');
echo "Interval: " . $updatedCard->scheduledDays . " days";

$fsrs = new Manager();
$card = new Card(new DateTime('now', new DateTimeZone('UTC')));

// First review
$schedule = $fsrs->generateRepetitionSchedule($card);
$card = $schedule[3]->card; // Good rating

// Subsequent reviews
$reviewDate = new DateTime('2024-01-05', new DateTimeZone('UTC'));
$schedule = $fsrs->generateRepetitionSchedule($card, $reviewDate);
$card = $schedule[2]->card; // Hard rating

// Check card retrievability
$retrievability = $fsrs->getCardRetrievability($card);
echo "Recall probability: " . ($retrievability * 100) . "%";

// Review card with timing data
$result = $fsrs->reviewCard($card, 3, new DateTime(), 2500);
$updatedCard = $result['card'];
$reviewLog = $result['log'];

// Serialize card data
$cardData = $card->toArray();
$cardJson = $card->toJson();

// Restore from serialized data
$restoredCard = Card::fromArray($cardData);
$cardFromJson = Card::fromJson($cardJson);

// Custom learning steps
$customFsrs = new Manager(
    learningSteps: [1, 5, 15, 30],
    relearningSteps: [5, 15],
    enableFuzzing: false
);

$fsrs = new Manager(
    defaultRequestRetention: 0.85,    // Target retention rate (85%)
    weights: [                        // Custom model weights
        0.4872, 1.4003, 3.7145, 13.8206, 5.1618, 1.2298, 0.8975, 0.031,
        1.6474, 0.1367, 1.0461, 2.1072, 0.0793, 0.3246, 1.587, 0.2272, 2.8755
    ],
    defaultMaximumInterval: 36500,    // Maximum interval in days (100 years)
    learningSteps: [1, 10],           // Learning phase intervals (minutes)
    relearningSteps: [10],            // Relearning phase intervals (minutes)
    enableFuzzing: true               // Add randomness to intervals
);

new Manager(
    float $defaultRequestRetention = 0.90,
    array $weights = [...],
    int $defaultMaximumInterval = 36500,
    array $learningSteps = [1, 10],
    array $relearningSteps = [10],
    bool $enableFuzzing = true
)

$retrievability = $fsrs->getCardRetrievability($card);
echo "Recall probability: " . ($retrievability * 100) . "%";

$result = $fsrs->reviewCard($card, 3, null, 2500);
$updatedCard = $result['card'];
$reviewLog = $result['log'];

// Export configuration
$config = $fsrs->toArray();

// Restore from configuration
$fsrs = Manager::fromArray($config);

new Card(
    ?DateTime $due = null,
    float $stability = 0,
    float $difficulty = 0,
    int $reps = 0,
    int $lapses = 0,
    int $state = 0,
    ?DateTime $lastReview = null,
    int $step = 0,
    ?string $cardId = null
)

// Export card data
$cardData = $card->toArray();
$jsonString = $card->toJson();

// Restore from data
$card = Card::fromArray($cardData);
$card = Card::fromJson($jsonString);

new ReviewLog(
    int $rating,
    int $scheduledDays,
    int $elapsedDays,
    DateTime $review,
    int $state,
    ?string $cardId = null,
    ?DateTime $reviewDateTime = null,
    ?int $reviewDurationMs = null
)

// Export review log
$logData = $reviewLog->toArray();
$jsonString = $reviewLog->toJson();

// Restore from data
$reviewLog = ReviewLog::fromArray($logData);
$reviewLog = ReviewLog::fromJson($jsonString);