Skip to content

Format date and time

You can format date and time by using the following services:

  • @ezplatform.user.settings.short_datetime_format.formatter
  • @ezplatform.user.settings.short_datet_format.formatter
  • @ezplatform.user.settings.short_time_format.formatter
  • @ezplatform.user.settings.full_datetime_format.formatter
  • @ezplatform.user.settings.full_date_format.formatter
  • @ezplatform.user.settings.full_time_format.formatter

To use them, create an src\Service\MyService.php file containing:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

namespace App\Service;

use EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface;

class MyService
{
    /** @var \EzSystems\EzPlatformUser\UserSetting\DateTimeFormat\FormatterInterface */
    private $shortDateTimeFormatter;

    public function __construct(FormatterInterface $shortDateTimeFormatter)
    {
        // your code

        $this->shortDateTimeFormatter = $shortDateTimeFormatter;

        // your code
    }

    public function foo()
    {
        // your code

        $now = new \DateTimeImmutable();
        $date = $this->shortDateTimeFormatter->format($now);
        $utc = $this->shortDateTimeFormatter->format($now, 'UTC');

        // your code
    }
}

Then, add the following to config/services.yaml:

1
2
3
4
services:    
    App\Service\MyService:
        arguments:
            $shortDateTimeFormatter: '@ezplatform.user.settings.short_datetime_format.formatter'