dt.addAdds two dates specified as strings or as numbers of seconds.
d1 + abs(secondsOf(d1) - secondsOf(d2)), where abs is the absolute value of a number (always a positive value) and secondsOf is the number of seconds that have elapsed since January 1, 1970 (dates before this point are represented as negative numbers).Signature: dt.add(d1 int|string, d2 int|string) string
Arguments:
d1 is a string representing a date in one of the valid formats, or an integer corresponding to the number of seconds.d2 – similar to the first argument.Return value: Returns the new date and time as a string.
Example of use:
$d = dt.add(“2022-01-01 12:30:00”, 59) // $d contains the string “2022-01-01 12:30:59”
$d = dt.add(3600, “2022-01-01 12:30:00”) // $d contains the string “2022-01-01 13:30:00”
$d = dt.add(“2022-01-02 02:00:00”, “2022-01-01 01:00:00”) // $d contains the string “2022-01-03 03:00:00”
dt.subCalculates the difference between two dates specified as a string or number of seconds.
Signature: dt.sub(d1 int|string, d2 int|string) int|string
Arguments:
d1 – a string representing a date in one of the valid formats, or an integer corresponding to the number of seconds.d2 – similar to the first argument.Return value: Returns a new date and time as a string or the number of seconds between the dates.
Example of use:
$d = dt.sub(100, 50) // $d contains 50
$d = dt.sub(“2022-01-01 12:30:00”, 3600) // $d contains the string “2022-01-01 11:30:00”
$d = dt.sub(3600, “2022-01-01 12:30:00”) // This call is invalid and will cause the program to stop
$d = dt.sub(“2022-01-01 01:00:00”, “2022-01-01 00:00:00”) // $d contains 3600
dt.formatFormats the date according to the specified format.
Signature: dt.format(dt string|int|dt.Time, format string) -> string
Arguments:
dt — date as a string, integer (seconds since January 1, 1970), or dt.Time object.format — string defining the date and time output format.Return value: String with the date formatted according to the specified template.
Example of use:
$dt = dt.format(‘2022-12-20 08:34:05.123’, ‘y.MM.dd h-mm-ss.SSSSSS’) // “22.12.20 8-34-05.123000”
$dt = dt.format(‘16:30:47’, ‘\Hour\s \an\d \minute\s: hh/mm A’) // “Hours and minutes: 04/30 PM”
Valid formatting parameters:
Symbol in format string |
Description | Example |
|---|---|---|
| Year | ||
y |
Full numeric year (at least 4 digits) | 1999, 2012 |
yy |
Last two digits of the year, padded with zeros | 99, 05 |
| Month | ||
M |
Month number without leading zero | 1–12 |
MM |
Month number with leading zero | 01–12 |
| Day | ||
d |
Day of the month without leading zero | 1–31 |
dd |
Day of the month with leading zero | 01–31 |
| Hour | ||
h |
Hour in 12-hour format without leading zero | 1–12 |
hh |
Hour in 12-hour format with leading zero | 01–12 |
H |
Hour in 24-hour format without leading zero | 0–23 |
HH |
Hour in 24-hour format with leading zero | 00–23 |
a |
am or pm in lowercase | am, pm |
A |
AM or PM in uppercase | AM, PM |
| Minutes | ||
m |
Minutes without leading zero | 0–59 |
mm |
Minutes with leading zero | 00–59 |
| Seconds | ||
s |
Seconds without leading zero | 0–59 |
ss |
Seconds with leading zero | 00–59 |
| Fractional seconds | ||
S, SS, SSS, ..., SSSSSSSSS |
Fractional second truncated to the number of digits | 1, 12, 123, 123456 |
| Time zone | ||
Z |
Time zone name | UTC, PDT, BST |
z |
Time zone offset in hours | +02, -01 |
zz |
Offset without colon between hours and minutes | +0200, -0100 |
ZZ |
Offset with colon between hours and minutes | +02:00, -01:00 |
zzz |
Offset without colon between hours, minutes, and seconds | +020000, -010000 |
ZZZ |
Offset with colon between hours, minutes, and seconds | +02:00:00, -01:00:00 |
dt.yearReturns the numeric value of the year from the date passed.
Signature: dt.year(dt int|string) int
Arguments: dt — date specified as a string or number of seconds.
Return value: An integer representing the year, consisting of at least four digits. If the dt argument specifies only the time (without the date), the function considers the date to be January 1, 1970. In this case, the result will be 1970.
Example of use:
$dt = dt.year(‘2022-12-20 08:34:05’) // $dt will contain the number 2022
$dt = dt.year(‘08:34:05’) // $dt will contain the number 1970
dt.monthReturns the ordinal number of the month from the passed date.
Signature: dt.month(dt int|string) int
Arguments: dt — the date specified by a string or a number of seconds.
Return value: An integer from 1 to 12 representing the month in the date dt. If the argument dt specifies only the time (without the date), the function considers the date to be January 1, 1970. In this case, the result will be 1.
Example of use:
$dt = dt.month(‘2022-12-20 08:34:05’) // $dt will contain the number 12
$dt = dt.month(‘08:34:05’) // $dt will contain the number 1
dt.dayReturns the day of the month from the date you give it.
Signature: dt.day(dt int|string) int
Arguments: dt — the date, given as a string or a number of seconds.
Return value: An integer from 1 to 31 representing the day of the month in the date dt. If only the time is passed in dt, the default date is used — January 1, 1970. In this case, the result will be 1.
Example of use:
$dt = dt.day(‘2022-12-20 08:34:05’) // $dt will contain the number 20
$dt = dt.day(‘08:34:05’) // $dt will contain the number 1
dt.hourReturns the hour from the passed date in 24-hour format.
Signature: dt.hour(dt int|string) int
Arguments: dt — the date specified as a string or a number of seconds.
Return value: An integer from 0 to 23 representing the hour in the date dt. If dt specifies only a date without a time, 0 is returned.
Example of use:
// Let's say it's now 2023-01-01
$dt = dt.hour(‘2022-12-20 08:34:05’) // $dt will contain the number 8
$dt = dt.hour(‘2022-12-20’) // $dt will contain the number 0
dt.minuteReturns the minutes from the passed date.
Signature: dt.minute(dt int|string) int
Arguments: dt — date specified as a string or number of seconds.
Return value: An integer from 0 to 59 representing the minutes in the date dt. If dt specifies only a date without a time, 0 is returned.
Example of use:
// Let's assume that today is 2023-01-01.
$dt = dt.minute(‘2022-12-20 08:34:05’) // $dt will contain the number 34.
$dt = dt.minute(‘2022-12-20’) // $dt will contain the number 0
dt.secondReturns the seconds from the passed date.
Signature: dt.second(dt int|string) int
Arguments: dt — date specified as a string or number of seconds.
Return value: An integer from 0 to 59 representing the seconds in the date dt. If dt specifies only the date without the time, 0 is returned.
Example of use:
// Let's assume that today is 2023-01-01.
$dt = dt.second(‘2022-12-20 08:34:05’) // $dt will contain the number 5.
$dt = dt.second(‘2022-12-20’) // $dt will contain the number 0.
dt.weekdayReturns the ordinal number of the day of the week from the passed date.
Signature: dt.weekday(dt int|string) int
Arguments: dt — date specified as a string or number of seconds.
Return value: An integer from 0 to 6, where 0 is Monday and 6 is Sunday. If only the time is specified in dt, the default date is used — January 1, 1970. In this case, the result will be 3 (Thursday).
Example of use:
$dt = dt.weekday(‘2022-12-20 08:34:05’) // $dt will contain the number 1
$dt = dt.weekday(‘08:34:05’) // $dt will contain the number 3
date.nearFutureReturns the nearest date in the future relative to the current date, corresponding to the specified day of the month.
Signature: date.nearFuture(day int) string
Arguments: day — a number indicating the day of the month for which the nearest future date is to be found.
Return value: A string representing the nearest date in the future with the specified day of the month.
Example of use:
// Let's say it is now 2022-12-20
$d = date.nearFuture(25) // $d contains 2022-12-25
$d = date.nearFuture(10) // $d contains 2023-01-10
date.nearPastReturns the nearest date in the past relative to the current date, corresponding to the specified day of the month.
Signature: date.nearPast(day int) string
Arguments: day — a number indicating the day of the month for which the nearest past date is to be found.
Return value: A string representing the nearest date in the past with the specified day of the month.
Example of use:
// For example, now is 2022-12-20
$d = date.nearPast(25) // $d contains 2022-11-25
$d = date.nearPast(10) // $d contains 2023-12-10
date.futureReturns the date in the next month corresponding to the specified day.
Signature: date.future(day int) string
Arguments: day — a number indicating the day of the month for which you want to get the date in the next month.
Return value: A string representing the date in the future with the specified day of the month.
Example of use:
// Let's say it's now 2022-12-20
$d = date.future(25) // $d contains 2023-01-25
$d = date.future(10) // $d contains 2023-01-10
date.pastReturns the date in the previous month corresponding to the specified day.
Signature: date.past(day int) string
Arguments: day — a number indicating the day of the month for which you want to get the date in the previous month.
Return value: A string representing the date in the past with the specified day of the month.
// Let's assume that today is 2022-12-20.
$d = date.past(25) // $d contains 2022-11-25.
$d = date.past(10) // $d contains 2022-11-10.
time.nearFutureReturns the nearest time in the future relative to the current time, corresponding to the specified number of minutes.
Signature: time.nearFuture(minute int) string
Arguments: minute — the number of minutes to which the nearest future time should be found.
Return value: A string representing the nearest time in the future with the specified number of minutes.
Example of use:
// Let's say it's now 23:30:00
$t = time.nearFuture(45) // $t contains 23:45:00
$t = time.nearFuture(15) // $t contains 00:15:00
time.nearPastReturns the nearest time in the past relative to the present, corresponding to the specified number of minutes.
Signature: time.nearPast(minute int) string
Arguments: minute — the number of minutes to find the closest past time.
Return value: A string representing the closest time in the past with the specified number of minutes.
Example of use:
// Let's say it's now 23:30:00
$t = time.nearPast(45) // $t contains 22:45:00
$t = time.nearPast(15) // $t contains 23:15:00
time.futureReturns the time in the next hour corresponding to the specified number of minutes.
Signature: time.future(minute int) string
Arguments: minute — the number of minutes to which the time in the next hour should be obtained.
Return value: A string representing the time in the future with the specified number of minutes.
Example of use:
// Let's say it's now 23:30:00
$t = time.future(45) // $t contains 00:45:00
$t = time.future(15) // $t contains 00:15:00
time.pastReturns the time in the past hour corresponding to the specified number of minutes.
Signature: time.past(minute int) string
Arguments: minute — the number of minutes to which you want to get the time in the previous hour.
Return value: A string representing the time in the past with the specified number of minutes.
Example of use:
// Let's say it's now 23:30:00
$t = time.past(45) // $t contains 22:45:00
$t = time.past(15) // $t contains 22:15:00