DateTime
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
Returns string that describes time. The results can be set using constants, locale, and timezone.
Get the current time as a formatted string using Carbon.
Adds second passed as parameter to current time.
Accepts UTC time in format Y-m-d H:i:s and returns a string describing how much time has elapsed.
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"
Generates a timestamp.
Get the current day as a formatted string using Carbon.
Converts to ISO 8601 format.
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.
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.
at line 82
static string
nowPlusSeconds(int $seconds)
Adds second passed as parameter to current time.
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.
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"
at line 134
static string
timeStamps()
Generates a timestamp.
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.
at line 167
static string
toISO8601(string|DateTimeInterface $time, string $timezone = 'UTC')
Converts to ISO 8601 format.
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.