PHP code example of cleatsquad / php-state-decay

1. Go to this page and download the library: Download cleatsquad/php-state-decay 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/ */

    

cleatsquad / php-state-decay example snippets


use CleatSquad\StateDecay\DecayConfig;
use CleatSquad\StateDecay\HalfLifeDecay;

$decay = new HalfLifeDecay(new DecayConfig(halfLifeDays: 30));

$decay->applyToValue(100.0, 30 * 86400); // 50.0
$decay->applyToValue(100.0, 60 * 86400); // 25.0

$counters = ['successes' => 40, 'failures' => 10];

$decay->applyToCounters($counters, 30 * 86400);
// ['successes' => 20, 'failures' => 5]

$decay->factor(0);             // 1.0
$decay->factor(30 * 86400);    // 0.5
$decay->factor(-3600);         // 1.0 — clock skew never resurrects state

DecayConfig::fromSeconds(1800);  // 30 minutes
DecayConfig::fromDays(0.25);     // 6 hours
DecayConfig::never();            // decay disabled; factor() stays 1.0