PHP code example of comsolit / clock-bundle

1. Go to this page and download the library: Download comsolit/clock-bundle 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/ */

    

comsolit / clock-bundle example snippets


$clock->hasElapsed($blockingPeriod, $lastFailedLogin->getDateTime())

((int)$lastFailedLogin->getDateTime()->format('U')) + $blockingPeriod >= $clock->getTimestamp()

$clock->isExpired($tokenExpirationTime)

$fileName = $clock->getFileName() . '_datadump';
// e.g. $fileName === '2015-03-25_23-12-59_datadump'


class Clock
{
    // SNIPP some stuff

    /**
     * Time representation usable as (part of) a file name, e.g.: 2014-05-23_13-45-23
     * @return string
     */
    public function getFileName()
    {
        return $this->now->format('Y-m-d_H-i-s');
    }

    /**
     * @return \DateTime
     */
    public function getDateTime()
    {
        return $this->now;
    }

    /**
     * @return int
     */
    public function getSeconds()
    {
        return (int)$this->now->format('U');
    }

    /**
     * @return int
     */
    public function getSecondsSince(\DateTimeInterface $past)
    {
        return $this->getSeconds() - (int)$past->format('U');
    }

    /**
     * @return int
     */
    public function getSecondsUntil(\DateTimeInterface $future)
    {
        return - $this->getSecondsSince($future);
    }

    /**
     * @return bool
     * @param int $seconds
     */
    public function hasElapsed($seconds, \DateTimeInterface $since)
    {
        return $this->getSecondsSince($since) > $seconds;
    }

    /**
     * @return bool
     */
    public function isExpired(\DateTimeInterface $expiryDate)
    {
        return (int)$this->now->format('U') > (int)$expiryDate->format('U');
    }

    /**
     * Create new DateTime object with the timezone of this clock
     *
     * @param String $time
     */
    public function createDateTime($time)
    {
        return (new \DateTime($time, $this->now->getTimezone()))->setTimezone($this->now->getTimezone());
    }

    /**
     * Returns an instance of DateTimeImmutable for the given implementation of DateTimeInterface.
     *
     * This method should rather exist in the DateTimeImmutable class but Derick Rethans doesn't
     * think so.
     *
     * @param \DateTimeInterface $datetime
     * @return \DateTimeImmutable
     */
    public static function createDateTimeImmutable(\DateTimeInterface $datetime)
    {
        if ($datetime instanceof \DateTimeImmutable)
        {
            return $datetime;
        }

        return \DateTimeImmutable::createFromMutable($datetime);
    }
}