What are the best practices for structuring PHP code to manage the countdown timer functionality effectively?

When managing a countdown timer in PHP, it is best to encapsulate the timer logic within a class to keep the code organized and maintainable. This class should have methods to start, pause, resume, and reset the timer, as well as calculate the remaining time. Additionally, using PHP's built-in DateTime class can simplify date and time calculations.

class CountdownTimer {
    private $startTime;
    private $duration;

    public function __construct($duration) {
        $this->duration = $duration;
    }

    public function start() {
        $this->startTime = time();
    }

    public function pause() {
        $this->duration -= (time() - $this->startTime);
    }

    public function resume() {
        $this->startTime = time();
    }

    public function reset() {
        $this->startTime = null;
        $this->duration = 0;
    }

    public function getRemainingTime() {
        if ($this->startTime) {
            $elapsedTime = time() - $this->startTime;
            return max(0, $this->duration - $elapsedTime);
        }
        return $this->duration;
    }
}

// Example usage
$timer = new CountdownTimer(60); // 60 seconds
$timer->start();
echo $timer->getRemainingTime(); // Output remaining time in seconds