1. Go to this page and download the library: Download vaibhavpandeyvpz/samay 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/ */
vaibhavpandeyvpz / samay example snippets
use Samay\SystemClock;
// Create a new clock instance
$clock = new SystemClock();
// Get the current time
$now = $clock->now();
echo $now->format('Y-m-d H:i:s'); // Output: 2024-01-15 10:30:45
use Samay\SystemClock;
$clock = new SystemClock();
$now = $clock->now();
// $now is a DateTimeImmutable instance
echo $now->format('Y-m-d H:i:s');
echo $now->getTimestamp();
use Samay\SystemClock;
use Psr\Clock\ClockInterface;
class OrderService
{
public function __construct(
private readonly ClockInterface $clock = new SystemClock()
) {
}
public function createOrder(array $items): Order
{
$order = new Order();
$order->setCreatedAt($this->clock->now());
$order->setItems($items);
return $order;
}
}
use Samay\FrozenClock;
use DateTimeImmutable;
// Freeze at a specific time
$frozenTime = new DateTimeImmutable('2024-01-15 10:30:45');
$clock = new FrozenClock($frozenTime);
$now = $clock->now();
echo $now->format('Y-m-d H:i:s'); // Always: 2024-01-15 10:30:45
// Even after waiting, it returns the same time
sleep(5);
$later = $clock->now();
echo $later->format('Y-m-d H:i:s'); // Still: 2024-01-15 10:30:45
use Samay\FrozenClock;
use DateTimeImmutable;
// Freeze at a specific time
$clock1 = new FrozenClock(new DateTimeImmutable('2024-01-15 10:30:45'));
// Freeze at current time (captures the moment of construction)
$clock2 = new FrozenClock();
// Freeze at a time with timezone
$clock3 = new FrozenClock(
new DateTimeImmutable('2024-01-15 10:30:45', new \DateTimeZone('UTC'))
);
use PHPUnit\Framework\TestCase;
use Samay\FrozenClock;
use Samay\SystemClock;
use DateTimeImmutable;
class OrderServiceTest extends TestCase
{
public function test_order_created_at_time(): void
{
// Freeze time for deterministic testing
$frozenTime = new DateTimeImmutable('2024-01-15 10:30:45');
$clock = new FrozenClock($frozenTime);
$service = new OrderService($clock);
$order = $service->createOrder(['item1', 'item2']);
// Assert exact time
$this->assertEquals($frozenTime, $order->getCreatedAt());
}
public function test_order_expires_after_30_days(): void
{
$createdAt = new DateTimeImmutable('2024-01-01 00:00:00');
$clock = new FrozenClock($createdAt);
$service = new OrderService($clock);
$order = $service->createOrder(['item1']);
// Fast-forward time
$expiryTime = $createdAt->modify('+30 days');
$expiredClock = new FrozenClock($expiryTime);
$this->assertTrue($order->isExpired($expiredClock->now()));
}
}
use Samay\LocalClock;
use DateTimeZone;
// Using timezone string
$clock = new LocalClock('America/New_York');
$now = $clock->now();
echo $now->getTimezone()->getName(); // "America/New_York"
echo $now->format('Y-m-d H:i:s'); // Current time in New York timezone
// Using DateTimeZone object
$timezone = new DateTimeZone('Europe/London');
$clock = new LocalClock($timezone);
$now = $clock->now();
echo $now->getTimezone()->getName(); // "Europe/London"
use Samay\LocalClock;
use Psr\Clock\ClockInterface;
class RegionalService
{
public function __construct(
private readonly ClockInterface $clock
) {
}
public function getLocalTime(): string
{
return $this->clock->now()->format('Y-m-d H:i:s T');
}
}
// For New York office
$nyService = new RegionalService(new LocalClock('America/New_York'));
// For Tokyo office
$tokyoService = new RegionalService(new LocalClock('Asia/Tokyo'));
use Samay\UtcClock;
$clock = new UtcClock();
$now = $clock->now();
echo $now->getTimezone()->getName(); // "UTC"
echo $now->format('Y-m-d H:i:s'); // Current time in UTC
use Samay\UtcClock;
use Psr\Clock\ClockInterface;
class ApiService
{
public function __construct(
private readonly ClockInterface $clock = new UtcClock()
) {
}
public function createTimestamp(): string
{
// Always use UTC for API timestamps
return $this->clock->now()->format('Y-m-d\TH:i:s\Z');
}
}
$service = new ApiService();
echo $service->createTimestamp(); // e.g., "2024-01-15T10:30:45Z"
use Psr\Clock\ClockInterface;
use Samay\SystemClock;
class EventLogger
{
public function __construct(
private readonly ClockInterface $clock = new SystemClock()
) {
}
public function log(string $message): void
{
$timestamp = $this->clock->now()->format('Y-m-d H:i:s');
echo "[$timestamp] $message\n";
}
}
// In production
$logger = new EventLogger(new SystemClock());
// In tests
$logger = new EventLogger(new FrozenClock(new DateTimeImmutable('2024-01-15 10:30:45')));
use Samay\SystemClock;
use Samay\LocalClock;
use Samay\UtcClock;
use DateTimeImmutable;
use DateTimeZone;
// SystemClock uses system default timezone
$systemClock = new SystemClock();
$now = $systemClock->now();
echo $now->getTimezone()->getName(); // e.g., "America/New_York" (system default)
// LocalClock uses specified timezone
$localClock = new LocalClock('Asia/Tokyo');
$tokyoTime = $localClock->now();
echo $tokyoTime->getTimezone()->getName(); // "Asia/Tokyo"
// UtcClock always uses UTC
$utcClock = new UtcClock();
$utcTime = $utcClock->now();
echo $utcTime->getTimezone()->getName(); // "UTC"
// FrozenClock preserves timezone from the frozen time
$frozenTime = new DateTimeImmutable('2024-01-15 10:30:45', new DateTimeZone('UTC'));
$frozenClock = new FrozenClock($frozenTime);
$frozen = $frozenClock->now();
echo $frozen->getTimezone()->getName(); // "UTC"
use Samay\SystemClock;
use Samay\LocalClock;
use Samay\UtcClock;
$systemClock = new SystemClock();
$now = $systemClock->now();
echo $now->format('Y-m-d H:i:s.u'); // e.g., "2024-01-15 10:30:45.123456"
$localClock = new LocalClock('UTC');
$utcNow = $localClock->now();
echo $utcNow->format('Y-m-d H:i:s.u'); // e.g., "2024-01-15 10:30:45.123456"
$utcClock = new UtcClock();
$utcTime = $utcClock->now();
echo $utcTime->format('Y-m-d H:i:s.u'); // e.g., "2024-01-15 10:30:45.123456"
namespace Psr\Clock;
interface ClockInterface
{
public function now(): \DateTimeImmutable;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.