Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Clock Laravel Package

lcobucci/clock

Small PHP clock abstraction to decouple your code from direct DateTimeImmutable instantiation. Depend on the Clock interface and use SystemClock for real time or FrozenClock for deterministic tests, with explicit timezone support.

View on GitHub
Deep Wiki
Context7

Yet another clock abstraction

Frequently asked questions about Clock
How do I replace Carbon::now() or now() helpers in Laravel with lcobucci/clock?
Register the clock in Laravel’s container as a singleton, then inject the `Clock` interface into your classes. Replace direct calls like `Carbon::now()` with `$this->clock->now()`. For example, bind `SystemClock::fromUTC()` in `AppServiceProvider` and use dependency injection in your services.
Does lcobucci/clock work with Laravel’s testing tools like Pest or PHPUnit?
Yes, `FrozenClock` lets you set a fixed time for tests, eliminating flaky time-dependent logic. Use it in test setups to mock time, and avoid Laravel’s `travel()` or Carbon’s time manipulation. Works seamlessly with Pest/PHPUnit for deterministic testing.
What Laravel versions and PHP versions does lcobucci/clock support?
The package requires PHP 8.4+ (as of v3.6.0). It’s framework-agnostic but fully compatible with Laravel 10+ and modern PHP. If your Laravel app uses PHP 8.1–8.3, pin to an older version (e.g., v3.5.0) or check for forks supporting older PHP.
Can I use lcobucci/clock for timezone-aware applications in Laravel?
Absolutely. `SystemClock` supports explicit timezone configuration (e.g., `SystemClock::fromTimezone(new DateTimeZone('Europe/London'))`). This ensures consistent timezone handling across multi-region Laravel deployments, reducing bugs from implicit timezone assumptions.
How does lcobucci/clock improve performance in high-frequency time calls?
While `SystemClock` creates a `DateTimeImmutable` on every `now()` call, the overhead is minimal for most use cases. For hot paths (e.g., logging middleware), cache the clock instance or use a cached `DateTimeImmutable` to avoid repeated instantiation.
What’s the migration path from Carbon/DateTime to lcobucci/clock in Laravel?
Start by registering the clock in Laravel’s container, then refactor business logic to inject the `Clock` interface. Replace direct `Carbon::now()` calls with `$this->clock->now()`, and use `FrozenClock` in tests. Tools like Rector can automate search-and-replace for legacy code.
Are there alternatives to lcobucci/clock for Laravel time abstraction?
Yes, alternatives include Symfony’s `ClockInterface` (PSR-20 compliant) or Carbon’s `CarbonImmutable`. However, `lcobucci/clock` is lightweight, focuses solely on time abstraction, and integrates cleanly with Laravel’s DI container. It’s ideal for apps prioritizing testability and PSR standards.
How do I handle dynamic timezone changes in Laravel with lcobucci/clock?
Since `SystemClock` is immutable, configure it at runtime using Laravel’s context (e.g., middleware or request-scoped bindings). For dynamic timezones, bind a `TimezoneAwareClock` wrapper or use Laravel’s `app()->setLocale()` to adjust the clock’s timezone contextually.
Does lcobucci/clock support Laravel’s queue/job scheduling?
Yes, inject the `Clock` interface into jobs or queue workers. Use `SystemClock` for real-time scheduling and `FrozenClock` in tests to simulate delayed jobs. This ensures consistent time handling across Laravel’s queue system without relying on `now()` or Carbon.
How do I test time-sensitive features like rate limiting or token expiration in Laravel?
Use `FrozenClock` to set a fixed time in tests, then verify logic against that timestamp. For example, set `FrozenClock::fromUTC('2023-01-01')` and assert that rate limits or token expirations behave deterministically. This replaces Laravel’s `travel()` for more maintainable tests.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai