class DateTime

Supports ability to manipulate how time is displayed. Most functions are wrappers for those found in the Carbon class.

All date objects are created through DateFactory so timezone, locale, and Carbon::setTestNow() behave consistently.

Constants

FORMAT_12_HOUR

12 hour format.

FORMAT_24_HOUR

24 hour format.

FORMAT_HUMAN_READABLE

Human readable format.

FORMAT_DATE_ONLY

Date only.

FORMAT_TIME_ONLY_12H

Time only in 12 hour format

FORMAT_TIME_ONLY_24H

Time only in 24 hour format

FORMAT_FRIENDLY_DATE

Friendly date format.

FORMAT_DAY_DATE

Day date format.

FORMAT_ISO_8601

ISO 8601 format.

FORMAT_RFC_2822

RFC 2822 format.

FORMAT_SQL_DATETIME

SQL DateTime format.

Methods

static string
formatTime(string|DateTimeInterface $time, string $format = self::FORMAT_12_HOUR, string $locale = 'en', string $timezone = 'UTC')

Returns string that describes time. The results can be set using constants, locale, and timezone.

static string
now(string|null $timezone = null, string|null $format = null, string|null $locale = null)

Get the current time as a formatted string using Carbon.

static string
nowPlusSeconds(int $seconds)

Adds second passed as parameter to current time.

static string
timeAgo(string|DateTimeInterface $time, string $locale = 'en', string $timezone = 'UTC', bool $short = false)

Accepts UTC time in format Y-m-d H:i:s and returns a string describing how much time has elapsed.

static string
timeDifference(string|DateTimeInterface $startTime, string|DateTimeInterface $endTime, string $timezone = 'UTC')

Shows the difference between two times. An example is shown below: DateTime::timeDifference('2025-03-09 08:00:00', '2025-03-09 15:30:45'); Output: "7 hours before"

static string
timeStamps()

Generates a timestamp.

static string
today(string|null $timezone = null, string|null $format = null, string|null $locale = null)

Get the current day as a formatted string using Carbon.

static string
toISO8601(string|DateTimeInterface $time, string $timezone = 'UTC')

Converts to ISO 8601 format.

static string
present(CarbonInterface $date, string|null $format, string|null $locale)

Formats an instance that is already in the timezone the caller wants.

Details

at line 50
static string formatTime(string|DateTimeInterface $time, string $format = self::FORMAT_12_HOUR, string $locale = 'en', string $timezone = 'UTC')

Returns string that describes time. The results can be set using constants, locale, and timezone.

Parameters

string|DateTimeInterface $time

String in format Y-m-d H:i:s A using UTC, or a date object (its own timezone is used).

string $format

Set format with a default of FORMAT_12_HOUR.

string $locale

Set locale with 'en' as the default value.

string $timezone

Timezone a string $time is stored in, with 'UTC' as default value. Ignored for date objects.

Return Value

string

The formatted time in the application timezone.

at line 72
static string now(string|null $timezone = null, string|null $format = null, string|null $locale = null)

Get the current time as a formatted string using Carbon.

This function supports optional user-specific overrides for timezone, format, and locale. If not provided, it defaults to the application's configured environment values.

Parameters

string|null $timezone

The timezone to use (e.g., 'Europe/Berlin'). Falls back to TIME_ZONE if null.

string|null $format

The format string (e.g., 'H:i' or 'g:i A'). Falls back to DateTime::FORMAT_12_HOUR if null.

string|null $locale

The locale code (e.g., 'en', 'fr'). Falls back to LOCALE if null.

Return Value

string

The current time formatted based on the provided or default settings.

at line 82
static string nowPlusSeconds(int $seconds)

Adds second passed as parameter to current time.

Parameters

int $seconds

The number of seconds to add.

Return Value

string

The time plus seconds passed as parameter in UTC.

at line 105
static string timeAgo(string|DateTimeInterface $time, string $locale = 'en', string $timezone = 'UTC', bool $short = false)

Accepts UTC time in format Y-m-d H:i:s and returns a string describing how much time has elapsed.

This function supports a short form with the following example: DateTime::timeAgo($user->updated_at, 'en', 'UTC', true);

This will show something like 21m.

Parameters

string|DateTimeInterface $time

String in format Y-m-d H:i:s using UTC, or a date object (its own timezone is used).

string $locale

Set locale with 'en' as the default value.

string $timezone

Timezone a string $time is stored in, with 'UTC' as default value. Ignored for date objects.

bool $short

Set to true to show short form time.

Return Value

string

The time represented using language describing time since last change.

at line 123
static string timeDifference(string|DateTimeInterface $startTime, string|DateTimeInterface $endTime, string $timezone = 'UTC')

Shows the difference between two times. An example is shown below: DateTime::timeDifference('2025-03-09 08:00:00', '2025-03-09 15:30:45'); Output: "7 hours before"

Parameters

string|DateTimeInterface $startTime

String in format Y-m-d H:i:s using UTC, or a date object.

string|DateTimeInterface $endTime

String in format Y-m-d H:i:s using UTC, or a date object.

string $timezone

Timezone both times are stored in, with 'UTC' as default value.

Return Value

string

Show exact difference between two times.

at line 134
static string timeStamps()

Generates a timestamp.

Return Value

string

A timestamp in the format Y-m-d H:i:s UTC time.

at line 154
static string today(string|null $timezone = null, string|null $format = null, string|null $locale = null)

Get the current day as a formatted string using Carbon.

This function supports optional user-specific overrides for timezone, format, and locale. If not provided, it defaults to the application's configured environment values.

Parameters

string|null $timezone

The timezone to use (e.g., 'Europe/Berlin'). Falls back to TIME_ZONE if null.

string|null $format

The format string (e.g., 'H:i' or 'g:i A'). Falls back to DateTime::FORMAT_12_HOUR if null.

string|null $locale

The locale code (e.g., 'en', 'fr'). Falls back to LOCALE if null.

Return Value

string

The current day formatted based on the provided or default settings.

at line 167
static string toISO8601(string|DateTimeInterface $time, string $timezone = 'UTC')

Converts to ISO 8601 format.

Parameters

string|DateTimeInterface $time

String in format Y-m-d H:i:s using UTC, or a date object (its own timezone is used).

string $timezone

Timezone a string $time is stored in, with 'UTC' as default value. Ignored for date objects.

Return Value

string

The time in ISO 8601 format. Example output: 2025-03-09T15:30:45-05:00

at line 180
static protected string present(CarbonInterface $date, string|null $format, string|null $locale)

Formats an instance that is already in the timezone the caller wants.

Parameters

CarbonInterface $date
string|null $format

Falls back to FORMAT_12_HOUR.

string|null $locale

Falls back to LOCALE.

Return Value

string