Weave 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

lcobucci/clock is a lightweight clock abstraction for PHP that decouples code from DateTimeImmutable. Use SystemClock for real time or FrozenClock for tests, making time-dependent logic easy to control and verify.

View on GitHub
Deep Wiki
Context7

Yet another clock abstraction

Frequently asked questions about Clock
How do I integrate lcobucci/clock into a Laravel application for production use?
Bind the Clock interface in your `AppServiceProvider` using Laravel’s service container. For example, register `SystemClock` with a specific timezone like UTC or your app’s timezone from config. This ensures consistent time handling across your application while keeping it decoupled from `DateTimeImmutable`.
Can I use FrozenClock to mock time in Laravel unit tests without affecting other tests?
Yes, replace the Clock binding in your test environment with `FrozenClock` using a fixed `DateTimeImmutable` instance. Laravel’s service container allows you to override bindings per test, ensuring isolated and reproducible time-based logic without side effects.
Will lcobucci/clock work with Laravel’s built-in Carbon library?
While the package works with `DateTimeImmutable`, you’ll need to create a bridge adapter to convert between Carbon and `DateTimeImmutable` if your codebase relies heavily on Carbon. This ensures compatibility while leveraging the Clock abstraction for testability.
What Laravel versions and PHP versions does lcobucci/clock support?
The package requires PHP 8.4+ (as of v3.6.0). For Laravel, ensure your project uses Laravel 10+ (which supports PHP 8.2+). If you’re on an older Laravel version, check compatibility or upgrade PHP to use the latest features of `lcobucci/clock`.
How can I replace static DateTime::createFromFormat() calls in legacy Laravel code with Clock?
Use search-replace scripts to identify static `DateTime` calls and refactor them to use the Clock interface. For complex logic, create decorator classes or wrapper methods that inject the Clock dependency. Laravel’s autowiring simplifies dependency injection for new code.
Is there a performance overhead when using Clock instead of direct DateTimeImmutable calls?
The overhead is minimal since `SystemClock` is immutable and thread-safe. Benchmark your specific use case, but the benefits of testability and decoupling typically outweigh the negligible performance cost, especially in non-critical paths.
How do I handle timezone differences between SystemClock and Laravel’s app.timezone config?
Explicitly configure `SystemClock` with your desired timezone (e.g., `SystemClock::fromUTC()` or `SystemClock::fromTimezone(new DateTimeZone(config('app.timezone')))`). Align this with your database, cache, and other services to avoid inconsistencies.
Can I use FrozenClock to test Laravel queues or delayed jobs without waiting?
Yes, bind `FrozenClock` in your test environment to simulate time progression. This allows you to test delayed jobs or time-sensitive queue logic instantly, making CI/CD pipelines faster and more reliable.
Are there alternatives to lcobucci/clock for time abstraction in Laravel?
Yes, alternatives include `symfony/clock` (PSR-20 compliant) or Laravel’s built-in `Carbon` with manual mocking. However, `lcobucci/clock` is lightweight, widely adopted, and integrates seamlessly with Laravel’s DI system, making it a robust choice for testability.
How can I enforce Clock dependency injection across a large Laravel codebase?
Use static analysis tools like PHPStan to detect and flag classes that don’t depend on the Clock interface. For legacy code, prioritize critical paths (e.g., payments, subscriptions) and use feature flags to gradually migrate other components.
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.
robertmarney/lara-hierarchial-collections
simonschaufi/rule-doc-generator
emissaoja/emissaoja
blackbricksoftware/gravityforms-civicrm-form-processor
al-rahabi/fancy-crud
jinas/dhivatar-component
mltstephane/laravel-quality
bernskiold/laravel-talentlms
bernskioldmedia/laravel-form-architect
youbar/easy-crud
c-point-labs/laravel-hostyourai
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo