Skip to content

[DependencyInjection] EnvVarProcessor - adding section about datetime prefix #12709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions configuration/env_var_processors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,57 @@ Symfony provides the following env var processors:
],
]);

``env(datetime:NOW)``
Instantiates a ``DateTime`` object with the given time string.

.. code-block:: bash

# .env
NOW="11-21-2018 08:00"

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:
# if NOW is not set, the DateTime will be using system time
App\Service\Clock:
$now: '%env(datetime:default::NOW)%'

.. code-block:: xml

<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<services>
<!-- if NOW is not set, the DateTime will be using system time -->
<service id="App\Service\Clock">
<argument key="$now">%env(datetime:default::NOW)%</argument>
</service>
</services>
</container>

.. code-block:: php

// config/services.php
use App\Service\Clock

// if NOW is not set, the DateTime will be using system time
$container->getDefinition(Clock::class)
->setArgument('$now', '%env(datetime:default::NOW)%');

.. note::

It is also possible to use ``DateTimeImmutable`` instead by
using ``datetime_immutable`` as prefix.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using ``datetime_immutable`` as prefix.
using ``datetime_immutable`` as prefix.
.. versionadded:: 5.1
The ``datetime`` and ``datetime_immutable`` processors were introduced in Symfony 5.1.


It is also possible to combine any number of processors:

.. code-block:: yaml
Expand Down