https://github.com/ghostwriter/clock
Provides an immutable Clock implementation for PHP
https://github.com/ghostwriter/clock
clock ghostwriter
Last synced: 18 days ago
JSON representation
Provides an immutable Clock implementation for PHP
- Host: GitHub
- URL: https://github.com/ghostwriter/clock
- Owner: ghostwriter
- License: other
- Created: 2023-09-28T18:17:14.000Z (almost 2 years ago)
- Default Branch: 3.0.x
- Last Pushed: 2025-06-16T16:50:18.000Z (19 days ago)
- Last Synced: 2025-06-16T17:51:58.168Z (19 days ago)
- Topics: clock, ghostwriter
- Language: PHP
- Homepage: https://github.com/ghostwriter/clock
- Size: 440 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
# Clock
[](https://github.com/sponsors/ghostwriter)
[](https://github.com/ghostwriter/clock/actions/workflows/automation.yml)
[](https://www.php.net/supported-versions)
[](https://packagist.org/packages/ghostwriter/clock)Provides an immutable Clock implementation for PHP
## Installation
You can install the package via composer:
``` bash
composer require ghostwriter/clock
```### Star ⭐️ this repo if you find it useful
You can also star (🌟) this repo to find it easier later.
### Usage
``` php
now(); // DateTimeImmutable
$systemClock->now()->getTimezone()->getName(); // America/Los_Angelesdate_default_timezone_set('America/New_York');
$systemClock = SystemClock::new();
$systemClock->now(); // DateTimeImmutable
$systemClock->now()->getTimezone()->getName(); // America/New_York$localizedClock = LocalizedClock::new();
$localizedClock->now(); // DateTimeImmutable
$localizedClock->now()->getTimezone()->getName(); // UTC$localizedClock = LocalizedClock::new(new DateTimeZone('Africa/Addis_Ababa'));
$localizedClock->now(); // DateTimeImmutable
$localizedClock->now()->getTimezone()->getName(); // Africa/Addis_Ababa$frozenClock = FrozenClock::new(new DateTimeImmutable('now', new DateTimeZone('UTC')));
$frozenClock->now(); // DateTimeImmutable
$frozenClock->now()->getTimezone()->getName(); // UTC
```### API
``` php
interface ClockInterface
{
public function freeze(): FrozenClockInterface;public function now(): DateTimeImmutable;
}interface FrozenClockInterface extends ClockInterface
{
public static function new(DateTimeImmutable $dateTimeImmutable): self;
}interface LocalizedClockInterface extends ClockInterface
{
public static function new(DateTimeZone $dateTimeZone): self;
}interface SystemClockInterface extends ClockInterface
{
public static function new(): self;
}
```### Changelog
Please see [CHANGELOG.md](./CHANGELOG.md) for more information what has changed recently.
### Security
If you discover any security related issues, please email `[email protected]` or create a [Security Advisory](https://github.com/ghostwriter/clock/security/advisories/new) instead of using the issue tracker.
## License
The BSD-4-Clause. Please see [License File](./LICENSE) for more information.