DateFormatter

February 2, 2026 ยท View on GitHub

The DateFormatter parses and reformats date and time strings using flexible input parsing and configurable output formats.

Usage

Basic Usage

use Respect\StringFormatter\DateFormatter;

$formatter = new DateFormatter();

echo $formatter->format('2024-01-15 10:30:45');
// Outputs: 2024-01-15 10:30:45

Custom Format

use Respect\StringFormatter\DateFormatter;

$formatter = new DateFormatter('m/d/Y');

echo $formatter->format('2024-01-15');
// Outputs: 01/15/2024

European Format

use Respect\StringFormatter\DateFormatter;

$formatter = new DateFormatter('d.m.Y');

echo $formatter->format('2024-01-15');
// Outputs: 15.01.2024

With Month Names

use Respect\StringFormatter\DateFormatter;

$formatter = new DateFormatter('l, F j, Y');

echo $formatter->format('2024-01-15');
// Outputs: Monday, January 15, 2024

ISO 8601 Format

use Respect\StringFormatter\DateFormatter;

$formatter = new DateFormatter('c');

echo $formatter->format('2024-01-15T10:30:45+00:00');
// Outputs: 2024-01-15T10:30:45+00:00

API

DateFormatter::__construct

  • __construct(string $format = 'Y-m-d H:i:s')

Creates a new formatter instance with the specified date format.

Parameters:

  • $format: PHP date format string (default: 'Y-m-d H:i:s')

format

  • format(string $input): string

Parses the input date string and formats it according to the formatter's configuration.

If the input cannot be parsed as a date, it is returned unchanged without modification.

Parameters:

  • $input: A date string in any format parseable by DateTime

Returns: The formatted date string if the input can be parsed; the input unchanged otherwise

Behavior

Strict Date Validation

The formatter uses a two-level validation approach:

  1. Exception handling: Catches DateMalformedStringException thrown by invalid format strings
  2. Error checking: Uses DateTime::getLastErrors() to detect parsing errors and warnings

This ensures even dates that appear parseable but have parsing issues are rejected.

Valid Input

The formatter uses PHP's DateTime constructor which supports various date formats including ISO 8601, MySQL format, relative formats (like "now", "tomorrow"), and other flexible formats. Valid input must parse without errors or warnings.

$formatter = new DateFormatter('Y-m-d');

// Valid date input
echo $formatter->format('2024-01-15');          // Outputs: 2024-01-15
echo $formatter->format('2024-01-15 10:30:45'); // Outputs: 2024-01-15
echo $formatter->format('January 15, 2024');    // Outputs: 2024-01-15

Invalid Input

When input cannot be parsed as a date or has parsing errors/warnings, the formatter returns it unchanged:

$formatter = new DateFormatter('Y-m-d');

// Invalid input is returned unchanged
echo $formatter->format('invalid date');       // Outputs: invalid date
echo $formatter->format('this-is-not-a-date'); // Outputs: this-is-not-a-date
echo $formatter->format('N/A');                // Outputs: N/A

Input Formats

The formatter uses PHP's DateTime constructor which supports various input formats:

Standard Date Formats

FormatExample
ISO 86012024-01-15T10:30:45
MySQL2024-01-15 10:30:45
US Format01/15/2024
European15.01.2024
Unix Timestamp@1705331445

Output Format Strings

Year

FormatDescriptionExample
Y4-digit year2024
y2-digit year24

Month

FormatDescriptionExample
m2-digit month01
nMonth without leading 01
FFull month nameJanuary
M3-letter monthJan

Day

FormatDescriptionExample
d2-digit day15
jDay without leading 015
D3-letter weekdayMon
lFull weekday nameMonday

Time

FormatDescriptionExample
H24-hour format (00-23)10
h12-hour format (01-12)10
iMinutes (00-59)30
sSeconds (00-59)45
AAM/PM uppercaseAM
aam/pm lowercaseam

Other

FormatDescriptionExample
cISO 86012024-01-15T10:30:45+00:00
rRFC 2822Mon, 15 Jan 2024 10:30:45 +0000
UUnix timestamp1705331445
zDay of year (0-365)014
WWeek number (ISO-8601)02

Examples

Common Formats

DescriptionFormatInputOutput
US Datem/d/Y2024-01-1501/15/2024
European Dated.m.Y2024-01-1515.01.2024
Time OnlyH:i:s2024-01-15 10:30:4510:30:45
Long Formatl, F j, Y2024-01-15Monday, January 15, 2024
Short FormatM d, Y2024-01-15Jan 15, 2024
ISO 8601c2024-01-15T10:30:452024-01-15T10:30:45+00:00
RFC 2822r2024-01-15 10:30:45Mon, 15 Jan 2024 10:30:45 +0000

Parsing Flexibility

The formatter intelligently parses various input formats:

$formatter = new DateFormatter('Y-m-d');

// All produce the same output: 2024-01-15
echo $formatter->format('2024-01-15');           // ISO format
echo $formatter->format('01/15/2024');           // US format
echo $formatter->format('15.01.2024');           // European format
echo $formatter->format('January 15, 2024');     // Long format

Relative Date Processing

$formatter = new DateFormatter('l, F j, Y');

echo $formatter->format('now');       // Today's date
echo $formatter->format('tomorrow');  // Tomorrow's date
echo $formatter->format('yesterday'); // Yesterday's date