PHP code example of gsokol / date-time-provider-bundle

1. Go to this page and download the library: Download gsokol/date-time-provider-bundle 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/ */

    

gsokol / date-time-provider-bundle example snippets


// app/AppKernel.php

// ...
class AppKernel Extends Kernel
{
    // ...
    public function registerBundles()
    {
        $bundles = [
            // ...
            new GSokol\DateTimeProviderBundle\DateTimePorviderBundle(),
            // ...
        ];
        // ...

doSomeStaff();
$timeOfRequestProcessionStart = $container->get('date-time-provider.greedy')->getRequestTime();

someLognOperation();
$currentTime = $container->get('date-time-provider.lazy')->getRequestTime();

$tomorrow = $container->get('date-time-provider.lazy')->getNewRequestTime()
    ->add(new \DateInterval('P1d'));


class NextHour
{
    public function get()
    {
        return (new \DateTime())
            ->add(new \DateInterval('PT1h'));
    }
}

// class

use GSokol\DateTimeProvider\DateTimeProviderInterface;

class NextHour
{
    /**
     * @var DateTimeProviderInterface
     */
    private $dtProvider;

    public function __construct(DateTimeProviderInterface $dtProvider)
    {
        $this->dtProvider = $dtProvider;
    }

    public function get()
    {
        return $this->dtProvider->getCurrentTime()
            ->add(new \DateInterval('PT1h'));
    }
}

// test

use GSokol\DateTimeProvider\DateTimeProviderInterface;
use PHPUnit\Framework\TestCase;

class NextHourTest extends TestCase
{
    public function testGet()
    {
        $dtStub = (new \DateTime())->setTimestamp(0);
        $dtExp = (new \DateTime())->setTimestamp(3600);
        $dtProvider = $this->getMockBuilder(DateTimeProviderInterface)
            ->disableOriginalConstructor()
            ->setMethods(['getCurrentTime'])
            ->getMock();
        $dtProvider->expects($this->once())
            ->method('getCurrentTime')
            ->will($this->returnValue($dtStub));

        $target = new NextHour($dtProvider);

        $this->assertEquals($dtExp, $target->get());
    }
}