1. Go to this page and download the library: Download barretstorck/tempus-machina 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/ */
barretstorck / tempus-machina example snippets
$clock = new \BarretStorck\TempusMachina\SystemClock();
$now = $clock->now(); // Returns a DateTimeImmutable object for the system's current time.
$timestamp = $now->getTimestamp(); // Returns the system's current Unix timestamp.
// Simulate February 29th 2024.
$clock = new \BarretStorck\TempusMachina\FrozenClock('February 29th 2024');
$now1 = $clock->now();
sleep(10); // Wait 10 seconds in real time.
$now2 = $clock->now(); // $now2 is still equal to $now1.
// Simulate February 29th 2124.
$clock->set(4864860000);
// Simulate March 9th 2025 at 23:59:59 just before daylight savings time begins
$clock = new \BarretStorck\TempusMachina\OffsetClock('2025-03-25T23:59:59+00:00');
echo $clock->now()->format(\DateTimeInterface::RFC3339); // Will echo "2025-03-25T23:59:59+00:00"
sleep(10); // Wait 10 seconds to allow for real time to pass.
echo $clock->now()->format(\DateTimeInterface::RFC3339); // Will echo "2025-03-26T01:09:00+00:00"
use Psr\Clock\ClockInterface;
use BarretStorck\TempusMachina\{UsesClockInterface, UsesClockTrait};
class MyObject implements UsesClockInterface
{
use UsesClockTrait;
public function __construct(null|ClockInterface $clock = null)
{
// If $clock is null, then the real time SystemClock will be available
// by default for any future calls of `getClock()`.
$this->setClock($clock);
}
public function doSomething()
{
// No longer hard coding `time()` or `new DateTimeImmutable('now')` calls.
//$now = time();
//$now = new DateTimeImmutable('now');
// The clock is now available and can be mocked in testing by providing
// a FrozenClock or OffsetClock to the MyObject constructor or it's
// `setClock()` function.
$now = $this->getClock()->now();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.