PHP code example of cakephp / chronos

1. Go to this page and download the library: Download cakephp/chronos library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cakephp / chronos example snippets



Cake\Chronos\Chronos;

printf("Now: %s", Chronos::now());

// This will lose modifications
$date = new Chronos('2015-10-21 16:29:00');
$date->modify('+2 hours');

// This will keep modifications
$date = new Chronos('2015-10-21 16:29:00');
$date = $date->modify('+2 hours');

use Cake\Chronos\ChronosDate;

$today = new ChronosDate();
echo $today;
// Outputs '2015-10-21'

echo $today->modify('+3 hours');
// Outputs '2015-10-21'

use Cake\Chronos\ChronosTime;

$time = new ChronosTime('14:30:00');
echo $time->format('g:i A'); // 2:30 PM

// Create from components
$time = ChronosTime::create(14, 30, 0);

// Arithmetic
$later = $time->addHours(2)->addMinutes(15);

use Cake\Chronos\Chronos;

// Freeze time for predictable tests
Chronos::setTestNow('2024-01-15 10:00:00');

$now = Chronos::now(); // Always 2024-01-15 10:00:00

// Reset to real time
Chronos::setTestNow(null);

use Cake\Chronos\ClockFactory;

$clock = new ClockFactory('UTC');
$now = $clock->now(); // Returns Chronos instance

// In your service
class OrderService
{
    public function __construct(private ClockInterface $clock) {}

    public function createOrder(): Order
    {
        return new Order(createdAt: $this->clock->now());
    }
}