Skip to content

DateFormatterInterface

Alexander Saal edited this page Aug 4, 2025 · 1 revision

DateFormatterInterface

Interface with Constants & Methods

interface DateFormatterInterface
{
    /**
     * Date format id for d.m.Y
     */
    public const FORMAT_WITH_POINT = 1;

    /**
     *  Date format id for d/m/Y
     */
    public const FORMAT_WITH_SLASHES = 2;

    /**
     * Date format id for m/d/Y
     */
    public const FORMAT_WITH_SLASHES_MONTH_FIRST = 3;

    /**
     * Date format id for Y-m-d
     */
    public const FORMAT_WITH_HYPHENS = 4;

    /**
     * Returns a formatted date according to a date $id (DateFormatterInterface::FORMAT_WITH_*). If an invalid ID
     * is specified, the method returns the date in the standard format.
     *
     * @param int $id ID of the date format (DateFormatterInterface:FORMAT_WITH_*)
     * @param string|int|bool $date Date string or date timestamp; if the value is false, the current timestamp is used
     * @param bool $fullDateTime Flag whether the time should also be returned
     * @param bool $isTimestamp Flag whether $date is a timestamp
     * @param string $timeZone TimeZone
     *
     * @return string formatted date
     */
    public function getFormattedDate(
        int             $id,
        string|int|bool $date = false,
        bool            $fullDateTime = false,
        bool            $isTimestamp = false,
        string          $timeZone = '',
    ): string;

    /**
     * Returns a date in the standard format YYYY-MM-DD.
     *
     * @param int $id ID of the date format (\JobRouter\Sdk\DateFormatterInterface::FORMAT_WITH_*)
     * @param string $date Date string
     * @param bool $fullDateTime Flag whether the time should also be returned
     * @param string $sourceTimeZone Source time zone
     * @param string $targetTimeZone Target time zone
     *
     * @return string Date in the standard format YYY-MM-DD
     *
     * @throws \JobRouterException
     */
    public function getUnformattedDate(
        int    $id,
        string $date,
        bool   $fullDateTime = false,
        string $sourceTimeZone = '',
        string $targetTimeZone = '',
    ): string;
}

Example

<?php

use JobRouter\Sdk\DateFormatterInterface;

return function (DateFormatterInterface $jobRouterDateFormatter): void {
    echo '<h1 style="color: #fc0">JobRouter SDK Date Formatter Interface example!</h1>';

    try {
        echo '<pre>Formatted Date (FORMAT_WITH_POINT): ' . print_r(
            $jobRouterDateFormatter->getFormattedDate(
                DateFormatterInterface::FORMAT_WITH_POINT,
                fullDateTime: true,
                isTimestamp: true,
            ),
            true,
        ) . '</pre>';

        echo '<pre>Formatted Date (FORMAT_WITH_SLASHES): ' . print_r(
            $jobRouterDateFormatter->getFormattedDate(
                DateFormatterInterface::FORMAT_WITH_SLASHES,
                fullDateTime: true,
                isTimestamp: true,
            ),
            true,
        ) . '</pre>';

        echo '<pre>Formatted Date (FORMAT_WITH_SLASHES_MONTH_FIRST): ' . print_r(
            $jobRouterDateFormatter->getFormattedDate(
                DateFormatterInterface::FORMAT_WITH_SLASHES_MONTH_FIRST,
                fullDateTime: true,
                isTimestamp: true,
            ),
            true,
        ) . '</pre>';
 

        echo '<pre>Formatted Date (FORMAT_WITH_HYPHENS): ' . print_r(
            $jobRouterDateFormatter->getFormattedDate(
                DateFormatterInterface::FORMAT_WITH_HYPHENS,
                fullDateTime: true,
                isTimestamp: true,
            ),
            true,
        ) . '</pre>';

        echo '<br />';

        echo '<pre>Unformatted Date (FORMAT_WITH_POINT): ' . print_r(
            $jobRouterDateFormatter->getUnformattedDate(
                DateFormatterInterface::FORMAT_WITH_POINT,
                $jobRouterDateFormatter->getFormattedDate(
                    DateFormatterInterface::FORMAT_WITH_POINT,
                    fullDateTime: true,
                    isTimestamp: true,
                ),
            ),
            true,
        ) . '</pre>';

    } catch (\RuntimeException $e) {
        echo '<h3 style="color: #f44;">Error!</h3>';
        echo '<p style="color: #f44;">Message: ' . $e->getMessage() . '</p>';
    }
};

Response

JobRouter SDK Date Formatter Interface example!

Formatted Date (FORMAT_WITH_POINT): 24.12.2024 18:00:00
Formatted Date (FORMAT_WITH_SLASHES): 24/12/2024 18:00:00
Formatted Date (FORMAT_WITH_SLASHES_MONTH_FIRST): 12/24/2024 18:00:00
Formatted Date (FORMAT_WITH_HYPHENS): 2024-12-24 18:00:00

Unformatted Date (FORMAT_WITH_POINT): 2024-11-24
Clone this wiki locally