Jenssegers\Date\Date::format PHP Method

format() public method

Returns date formatted according to given or predefined format.
public format ( string $format ) : string
$format string
return string
    public function format($format)
    {
        $replace = [];
        // Loop all format characters and check if we can translate them.
        for ($i = 0; $i < strlen($format); $i++) {
            $character = $format[$i];
            // Check if we can replace it with a translated version.
            if (in_array($character, ['D', 'l', 'F', 'M'])) {
                // Check escaped characters.
                if ($i > 0 and $format[$i - 1] == '\\') {
                    continue;
                }
                switch ($character) {
                    case 'D':
                        $key = parent::format('l');
                        break;
                    case 'M':
                        $key = parent::format('F');
                        break;
                    default:
                        $key = parent::format($character);
                }
                // The original result.
                $original = parent::format($character);
                // Translate.
                $lang = $this->getTranslator();
                // For declension support, we need to check if the month is lead by a numeric number.
                // If so, we will use the second translation choice if it is available.
                if (in_array($character, ['F', 'M'])) {
                    $choice = ($i - 2 >= 0 and in_array($format[$i - 2], ['d', 'j'])) ? 1 : 0;
                    $translated = $lang->transChoice(strtolower($key), $choice);
                } else {
                    $translated = $lang->trans(strtolower($key));
                }
                // Short notations.
                if (in_array($character, ['D', 'M'])) {
                    $translated = mb_substr($translated, 0, 3);
                }
                // Add to replace list.
                if ($translated and $original != $translated) {
                    $replace[$original] = $translated;
                }
            }
        }
        // Replace translations.
        if ($replace) {
            return str_replace(array_keys($replace), array_values($replace), parent::format($format));
        }
        return parent::format($format);
    }

Usage Example

Beispiel #1
1
 public function getUpdatedBudgetAttribute()
 {
     $date = new Date($this->updated_at);
     return $date->format('l j F Y');
 }
All Usage Examples Of Jenssegers\Date\Date::format