1. Go to this page and download the library: Download recruiterphp/clock 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/ */
recruiterphp / clock example snippets
use Recruiter\Clock\ManualClock;
use Recruiter\Clock\SystemClock;
use Recruiter\DateTime\UTCDateTime;
// Production: Use system time
$clock = new SystemClock();
$now = $clock->now(); // Returns DateTimeImmutable
// Testing: Use fixed time (DateTime gets converted to DateTimeImmutable)
$clock = new ManualClock(new \DateTimeImmutable('2024-01-01 12:00:00'));
$fixedTime = $clock->now(); // Always returns 2024-01-01 12:00:00
// UTC DateTime with microsecond precision
$utcTime = UTCDateTime::now();
echo $utcTime->toIso8601WithMicroseconds(); // 2024-01-01T12:00:00.123456+0000
// MongoDB integration
$mongoClock = $clock->asMongoUTC();
$bsonDateTime = $mongoClock->now(); // Returns MongoDB\BSON\UTCDateTime
use Symfony\Component\Clock\ClockInterface;
interface Clock extends ClockInterface
{
// Methods from Symfony's ClockInterface:
// - now(): \DateTimeImmutable
// - sleep(float|int $seconds): void
// - withTimeZone(\DateTimeZone|string $timezone): static
// Additional methods for specialized clocks:
public function asUTC(): UTCClock;
public function asMongoUTC(): MongoUTCClock;
public function asMicrotime(): MicrotimeClock;
public function stopWatch(): StopWatch;
}
interface UTCClock
{
public function now(): UTCDateTime;
}
interface MongoUTCClock
{
public function now(): MongoDB\BSON\UTCDateTime;
}
$clock = new SystemClock();
$now = $clock->now(); // Returns current DateTimeImmutable
// With timezone
$nyClock = new SystemClock('America/New_York');
// You can pass either DateTime or DateTimeImmutable - both work
$clock = new ManualClock(new \DateTimeImmutable('2024-01-01'));
// or use DateTime (gets converted internally to DateTimeImmutable)
$clock = new ManualClock(new \DateTime('2024-01-01'));
// or use the factory method
$clock = ManualClock::fromIso8601('2024-01-01T12:00:00Z');
// Advance time
$clock->advance(3600); // Advance 1 hour (seconds)
$clock->advance(new \DateInterval('P1D')); // Advance 1 day
$clock = new ProgressiveClock(
new \DateTimeImmutable('2024-01-01'),
new \DateInterval('PT1H') // Advance 1 hour each call
);
$time1 = $clock->now(); // 2024-01-01 00:00:00
$time2 = $clock->now(); // 2024-01-01 01:00:00
$baseClock = new SystemClock();
$clock = new SettableClock($baseClock);
// Override with fixed time
$clock->nowIs(new \DateTimeImmutable('2024-01-01'));
$fixed = $clock->now(); // 2024-01-01
// Reset to base clock
$clock->reset();
$system = $clock->now(); // Current system time
$baseClock = new SystemClock();
$clock = new DelayedClock($baseClock, 3600); // 1 hour delay
$past = $clock->now(); // Returns time from 1 hour ago
use Recruiter\DateTime\UTCDateTimeRange;
$start = UTCDateTime::fromString('2024-01-01');
$end = UTCDateTime::fromString('2024-01-31');
$range = UTCDateTimeRange::fromTo($start, $end);
// Iterate by days
foreach ($range->dailyIterator() as $day) {
echo $day->toIso8601Day(); // 2024-01-01, 2024-01-02, etc.
}
// Iterate by hours
foreach ($range->hourlyIterator() as $hour) {
echo $hour->toIso8601(); // Every hour in the range
}
use Recruiter\Clock\Clock;
use Recruiter\Clock\ManualClock;
class OrderService
{
public function __construct(private Clock $clock) {}
public function createOrder(): Order
{
return new Order($this->clock->now());
}
}
// In tests
$testClock = new ManualClock(new \DateTimeImmutable('2024-01-01'));
$service = new OrderService($testClock);
$order = $service->createOrder();
// Order will always have 2024-01-01 timestamp
// Advance time to test time-based logic
$testClock->advance(3600); // 1 hour later
$laterOrder = $service->createOrder();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.