> For the complete documentation index, see [llms.txt](https://docs.sonsofphp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sonsofphp.com/components/clock.md).

# Clock

The Clock Component is a wrapper around PHP's native DateTime objects and functions. Using the Clock Component helps you test and keep everything a standard timezone

### Installation

```shell
composer require sonsofphp/clock
```

### Usage

#### SystemClock

The `SystemClock` is generally used in production. If you need check timestamps or manage dates and times, this is the Clock you want to use. Use Dependency Injection to make it easy to test with.

```php
<?php

use SonsOfPHP\Component\Clock\SystemClock;

$clock = new SystemClock();

$now = $clock->now(); // Returns a DateTimeImmutable object
```

#### FixedClock

The `FixedClock` is used for testing. Just set the time and inject into your class.

```php
<?php
use SonsOfPHP\Component\Clock\FixedClock;

$clock = new FixedClock();
$firstNow = $clock->now();
sleep(10);
$secondNow = $clock->now();

var_dump($firstNow === $secondNow); // true

// You can update the internal time at any time.
$clock->tick(); // Updates the internal clock to the curent time
$thridNow = $clock->now();

var_dump($firstNow === $secondNow); // still true
var_dump($firstNow === $thirdNow); // false
var_dump($thirdNow === $secondNow); // false

// You can also set the internal clock to whatever time you need it to be
$clock->tickTo('2022-04-20 04:20:00'); // format is "Y-m-d H:i:s"
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.sonsofphp.com/components/clock.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
