class DateTime

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

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 $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
nowPlusSeconds(int $seconds)

Adds second passed as parameter to current time.

static string
timeAgo(string $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 $startTime, string $endTime, string $timezone = 'UTC')

Shows the difference between two times. An example is shown below: DateTimeHelper::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
toISO8601(string $time, string $timezone = 'UTC')

Converts to ISO 8610 format.

Details

at line 45
static string formatTime(string $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 $time

String in format Y-m-d H:i:s A using UTC.

string $format

Set format with a default of FORMAT_12_HOUR.

string $locale

Set locale with 'en' as the default value.

string $timezone

Override default timezone with 'UTC' as default value.

Return Value

string

The formatted time.

at line 58
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.

at line 79
static string timeAgo(string $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 $time

String in format Y-m-d H:i:s using UTC.

string $locale

Set locale with 'en' as the default value.

string $timezone

Override default timezone with 'UTC' as default value.

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 99
static string timeDifference(string $startTime, string $endTime, string $timezone = 'UTC')

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

Parameters

string $startTime

String in format Y-m-d H:i:s using UTC.

string $endTime

String in format Y-m-d H:i:s using UTC.

string $timezone

Override default timezone with 'UTC' as default value.

Return Value

string

Show exact difference between two times.

at line 110
static string timeStamps()

Generates a timestamp.

Return Value

string

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

at line 122
static string toISO8601(string $time, string $timezone = 'UTC')

Converts to ISO 8610 format.

Parameters

string $time

String in format Y-m-d H:i:s using UTC.

string $timezone

Override default timezone with 'UTC' as default value.

Return Value

string

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