Documentation

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

MINUTE

Seconds in minute

public mixed MINUTE = 60

MONTH

Seconds in month

public mixed MONTH = 2629744

YEAR

Seconds in year

public mixed YEAR = 31556926

DATEFORMAT

private mixed DATEFORMAT = 'Y-m-d 00:00:00'

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
example
DateHelper::Age(1704067200) returns 2 месяца назад
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
example
DateHelper::AgeYears(1704067200) returns 0
DateHelper::AgeYears(1602062200) returns 3
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
example
DateHelper::Create(2024,1,1) returns 1704067200
Return values
int

DaysInMonth()

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
example
DateHelper::DaysInMonth(new DateTime('now')) today returns 31
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
example
DateHelper::Diff(1602062200, 1704067200) returns (object)["years" => 3,"months" => 2,"days" => 25]
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
example
DateHelper::DiffFullTokens(1602062200, 1704067200) returns "3 года 2 месяца25 дней"
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
example
DateHelper::FromDDMMYYYY('01.01.2024') returns 2024-01-01
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
example
DateHelper::LastDayOfMonth(1704067200) returns 31
DateHelper::LastDayOfMonth() returns last day of current month, if january = 31, february = 28 or 29
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
example
DateHelper::RFC() today returns Fri, 15 Mar 2024 03:57:55 +0000
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
example
DateHelper::TimeToString(1602062200) returns 57:16:40
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
example
DateHelper::ToDbString(1704067200) returns 2024-01-01 00:00:00
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
example
DateHelper::ToHumanDate() returns today 15 марта 2024
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
example
DateHelper::ToQuarter() returns today 1 квартал 2024
DateHelper::ToQuarter(null, '', true) returns today 1
DateHelper::ToQuarter('2024-08-01') returns today 3 квартал 2024
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
example
DateHelper::ToUnixTime('2024-01-01') returns 1704067200
Return values
int|null

The Unix timestamp corresponding to the date string, or null if invalid input.


        
On this page

Search results