Sons of PHP
Source Code
  • 🏠Home
  • Contributor Covenant Code of Conduct
  • Getting Help
  • Reporting Issues
  • Security Policy
  • 🪈Bard
    • Overview
    • Commands
  • Symfony Bundles
    • Feature Toggle
  • Contracts
    • Contracts Overview
    • Common
    • Cookie
    • CQRS
    • Filesystem
    • Mailer
    • Pager
    • Registry
    • State Machine
  • 📦Components
    • Assert
    • Cache
      • Adapters
      • Marshallers
    • Clock
    • Container
    • Cookie
    • CQRS
    • Event Dispatcher
    • Event Sourcing
      • Aggregates
        • Aggregate Repository
      • Event Messages
        • Using the Serializable Event Message
        • Message Enrichers
        • Message Serializers
        • Message Repository
        • Message Upcasters
    • Feature Toggle
    • Filesystem
      • Adapters
    • Http Factory
    • Http Handler
    • Http Message
    • JSON
    • Link
    • Logger
      • Handlers
      • Enrichers
      • Filters
      • Formatters
    • Mailer
      • Transports
    • Money
      • Currency Providers
      • Operators
      • Queries
    • Pager
      • Adapters
    • Registry
    • State Machine
    • Version
  • 💁Contributing
    • Contributing Overview
    • Contributing Code
    • Discussions
    • Documentation
Powered by GitBook
On this page
  • Installation
  • Usage

Was this helpful?

Edit on GitHub
  1. Components

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

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

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
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"
PreviousMarshallersNextContainer

Last updated 8 months ago

Was this helpful?

📦