DateHelper
in package
Helper class for working with dates.
Table of Contents
Constants
- DAY = 86400
- Seconds in day
- HOUR = 3600
- Seconds in hour
- MINUTE = 60
- Seconds in minute
- MONTH = 2629744
- Seconds in month
- WEEK = 604800
- Seconds in week
- YEAR = 31556926
- Seconds in year
- DATEFORMAT = 'Y-m-d 00:00:00'
- NBSP = ' '
Methods
- Age() : string
- Calculates the age based on the provided timestamp.
- AgeYears() : string
- Calculates the age in years based on the provided timestamp or date string.
- Create() : int
- Creates a date object based on the provided year, month, and day.
- DaysInMonth() : int
- Calculates the number of days in the month for the given DateTime object.
- Diff() : object
- Calculates the difference between two timestamps.
- DiffFullTokens() : string
- Calculates the difference between two timestamps in terms of full tokens (years, months, and days).
- FromDDMMYYYY() : string
- Converts a date string in the format "DD.MM.YYYY" to a correctly formatted string.
- FromJSDate() : DateTimeField
- Converts a JavaScript-style date string to a `DateTimeField` object.
- LastDayOfMonth() : bool|int
- Calculates the last day of the month for the given date.
- RFC() : string
- Generates an RFC 2822 formatted date string based on the provided timestamp.
- TimeToString() : string|null
- Converts a numeric timestamp to a human-readable time string formated as HH:MM:SS
- ToDbString() : string
- Converts a timestamp or date string to a database-friendly formatted string.
- ToHumanDate() : string
- Converts a timestamp to a human-readable date string.
- ToQuarter() : string
- Converts a timestamp or date string to a human-readable quarter representation.
- ToUnixTime() : int|null
- Converts a date string to a Unix timestamp.
Constants
DAY
Seconds in day
public
mixed
DAY
= 86400
HOUR
Seconds in hour
public
mixed
HOUR
= 3600
MINUTE
Seconds in minute
public
mixed
MINUTE
= 60
MONTH
Seconds in month
public
mixed
MONTH
= 2629744
WEEK
Seconds in week
public
mixed
WEEK
= 604800
YEAR
Seconds in year
public
mixed
YEAR
= 31556926
DATEFORMAT
private
mixed
DATEFORMAT
= 'Y-m-d 00:00:00'
NBSP
private
mixed
NBSP
= ' '
Methods
Age()
Calculates the age based on the provided timestamp.
public
static Age(int $time) : string
Parameters
- $time : int
-
The timestamp (as an integer) representing the date.
Tags
Return values
string —The age in years as a string (e.g., "30 years old").
AgeYears()
Calculates the age in years based on the provided timestamp or date string.
public
static AgeYears(int|string $time) : string
Parameters
- $time : int|string
-
The timestamp (as an integer) or date string.
Tags
Return values
string —The age in years as a string (e.g., "30 years old").
Create()
Creates a date object based on the provided year, month, and day.
public
static Create(int $year, int $month, int $day) : int
Parameters
- $year : int
-
The year (e.g., 2024).
- $month : int
-
The month (1 to 12).
- $day : int
-
The day of the month (1 to 31).
Tags
Return values
intDaysInMonth()
Calculates the number of days in the month for the given DateTime object.
public
static DaysInMonth(DateTime $dt) : int
Parameters
- $dt : DateTime
-
The DateTime object representing the desired month.
Tags
Return values
int —The number of days in the month (28 to 31).
Diff()
Calculates the difference between two timestamps.
public
static Diff(int $time1, int $time2) : object
Parameters
- $time1 : int
-
The first timestamp.
- $time2 : int
-
The second timestamp.
Tags
Return values
object —An object representing the time difference (e.g., days, hours, minutes).
DiffFullTokens()
Calculates the difference between two timestamps in terms of full tokens (years, months, and days).
public
static DiffFullTokens(int $time1, int $time2[, string $splitter = ' ' ][, array<string|int, mixed> $tokens = [['год', 'года', 'лет'], ['месяц', 'месяца', 'месяцев'], ['день', 'дня', 'дней']] ]) : string
Parameters
- $time1 : int
-
The first timestamp.
- $time2 : int
-
The second timestamp.
- $splitter : string = ' '
-
The delimiter to use between tokens (default is a space).
- $tokens : array<string|int, mixed> = [['год', 'года', 'лет'], ['месяц', 'месяца', 'месяцев'], ['день', 'дня', 'дней']]
-
An array of token names for years, months, and days (e.g., [['год', 'года', 'лет'], ...]).
Tags
Return values
string —The formatted difference string (e.g., "2 года 3 месяца 15 дней").
FromDDMMYYYY()
Converts a date string in the format "DD.MM.YYYY" to a correctly formatted string.
public
static FromDDMMYYYY(string $dateString[, string $delimiter = '.' ][, string $format = 'Y-m-d H:i:s' ]) : string
Parameters
- $dateString : string
-
The date string in "DD.MM.YYYY" format.
- $delimiter : string = '.'
-
The delimiter used in the input date string (default is '.').
- $format : string = 'Y-m-d H:i:s'
-
The desired output format (default is 'Y-m-d H:i:s').
Tags
Return values
string —The formatted date string.
FromJSDate()
Converts a JavaScript-style date string to a `DateTimeField` object.
public
static FromJSDate(string $date) : DateTimeField
Parameters
- $date : string
-
The JavaScript-style date string (e.g., "2024-03-15T12:30:45").
Return values
DateTimeField —A DateTimeField
object representing the parsed date and time.
LastDayOfMonth()
Calculates the last day of the month for the given date.
public
static LastDayOfMonth([int|null $date = null ]) : bool|int
Parameters
- $date : int|null = null
-
The date (as an integer timestamp) or null for the current date.
Tags
Return values
bool|int —The last day of the month (as an integer day of the month), or false if invalid input.
RFC()
Generates an RFC 2822 formatted date string based on the provided timestamp.
public
static RFC([float|null $time = null ]) : string
Parameters
- $time : float|null = null
-
The timestamp (as a float) or null for the current time.
Tags
Return values
string —The RFC 2822 formatted date string.
TimeToString()
Converts a numeric timestamp to a human-readable time string formated as HH:MM:SS
public
static TimeToString(int $number) : string|null
Parameters
- $number : int
-
The timestamp (as an integer).
Tags
Return values
string|null —The formatted time string (e.g., "12:30:10"), or null if invalid input.
ToDbString()
Converts a timestamp or date string to a database-friendly formatted string.
public
static ToDbString([float|string|null $time = null ][, string|null $format = 'Y-m-d H:i:s' ]) : string
Parameters
- $time : float|string|null = null
-
The timestamp (as a float), date string, or null for the current time.
- $format : string|null = 'Y-m-d H:i:s'
-
The desired format (default is 'Y-m-d H:i:s').
Tags
Return values
string —The formatted date string suitable for database storage.
ToHumanDate()
Converts a timestamp to a human-readable date string.
public
static ToHumanDate([float|null $time = null ][, bool $showTime = false ]) : string
Parameters
- $time : float|null = null
-
The timestamp (as a float) or null for the current time.
- $showTime : bool = false
-
Whether to include the time portion in the output (default is false).
Tags
Return values
string —The human-readable date string.
ToQuarter()
Converts a timestamp or date string to a human-readable quarter representation.
public
static ToQuarter([float|string|null $time = null ][, string $quarterName = 'квартал' ][, bool $numberOnly = false ]) : string
Parameters
- $time : float|string|null = null
-
The timestamp (as a float), date string, or null for the current time.
- $quarterName : string = 'квартал'
-
The name to use for the quarter (e.g., 'quarter' or 'Q').
- $numberOnly : bool = false
-
Whether to include only the quarter number (default is false).
Tags
Return values
string —The human-readable quarter representation (e.g., 'Q1 2024' or '1st quarter 2024').
ToUnixTime()
Converts a date string to a Unix timestamp.
public
static ToUnixTime(string $datestring) : int|null
Parameters
- $datestring : string
-
The date string to convert.
Tags
Return values
int|null —The Unix timestamp corresponding to the date string, or null if invalid input.