DiscordPHP Documentation

Timestamp
in package
implements Stringable uses DynamicPropertyMutatorTrait

Timestamp is a helper class for formatting timestamps in Discord messages.

https://docs.discord.com/developers/reference#message-formatting

Tags
since
10.56.6
author

Valithor Obsidion valithor@discordphp.org

Table of Contents

Interfaces

Stringable

Constants

STYLE_LONG_DATE  : mixed = 'D'
Long Date, e.g. `April 20, 2021`.
STYLE_LONG_DATE_TIME  : mixed = 'F'
Long Date/Time, e.g. `Tuesday, April 20, 2021 16:20`.
STYLE_LONG_TIME  : mixed = 'T'
Long Time, e.g. `16:20:30`.
STYLE_RELATIVE_TIME  : mixed = 'R'
Relative Time, e.g. `4 years ago`.
STYLE_SHORT_DATE  : mixed = 'd'
Short Date, e.g. `20/04/2021`.
STYLE_SHORT_DATE_MEDIUM_TIME  : mixed = 'S'
Short Date, Medium Time, e.g. `20/04/2021, 16:20:30`.
STYLE_SHORT_DATE_SHORT_TIME  : mixed = 's'
Short Date, Short Time, e.g. `20/04/2021, 16:20`.
STYLE_SHORT_DATE_TIME  : mixed = 'f'
Short Date/Time, e.g. `20 April 2021 16:20`. Default.
STYLE_SHORT_TIME  : mixed = 't'
Short Time, e.g. `16:20`.
STYLES  : array<string|int, string> = [self::STYLE_SHORT_TIME, self::STYLE_LONG_TIME,...
All valid Discord timestamp styles.

Properties

$format  : string
The Discord timestamp style, one of the `Timestamp::*` constants. Defaults to `Timestamp::STYLE_SHORT_DATE_TIME`.
$timestamp  : int
Unix timestamp (in seconds) that will be displayed.

Methods

__construct()  : mixed
Creates a new Timestamp instance.
__get()  : mixed
Handles dynamic get calls onto the part.
__set()  : void
Handles dynamic set calls onto the part.
__toString()  : string
Converts the timestamp to Discord's `<t:UNIX_TIMESTAMP:FORMAT>` markdown.
new()  : self
Creates a new Timestamp instance.
checkForGetMutator()  : string|false
Checks if there is a get mutator present.
checkForSetMutator()  : string|false
Checks if there is a set mutator present.
getFormat()  : string
getProperty()  : mixed
Gets a property on the parent part.
getTimestamp()  : int
setFormat()  : void
Sets the `format` attribute, ensuring it is one of the valid Discord timestamp styles.
setProperty()  : void
Sets an property on the parent part.
setTimestamp()  : void
Normalizes and sets the `timestamp` attribute from a `DateTimeInterface` or Unix timestamp.
studly()  : string
Converts a string to studlyCase.

Constants

STYLE_LONG_DATE

Long Date, e.g. `April 20, 2021`.

public mixed STYLE_LONG_DATE = 'D'

STYLE_LONG_DATE_TIME

Long Date/Time, e.g. `Tuesday, April 20, 2021 16:20`.

public mixed STYLE_LONG_DATE_TIME = 'F'

STYLE_LONG_TIME

Long Time, e.g. `16:20:30`.

public mixed STYLE_LONG_TIME = 'T'

STYLE_RELATIVE_TIME

Relative Time, e.g. `4 years ago`.

public mixed STYLE_RELATIVE_TIME = 'R'

STYLE_SHORT_DATE

Short Date, e.g. `20/04/2021`.

public mixed STYLE_SHORT_DATE = 'd'

STYLE_SHORT_DATE_MEDIUM_TIME

Short Date, Medium Time, e.g. `20/04/2021, 16:20:30`.

public mixed STYLE_SHORT_DATE_MEDIUM_TIME = 'S'

STYLE_SHORT_DATE_SHORT_TIME

Short Date, Short Time, e.g. `20/04/2021, 16:20`.

public mixed STYLE_SHORT_DATE_SHORT_TIME = 's'

STYLE_SHORT_DATE_TIME

Short Date/Time, e.g. `20 April 2021 16:20`. Default.

public mixed STYLE_SHORT_DATE_TIME = 'f'

STYLE_SHORT_TIME

Short Time, e.g. `16:20`.

public mixed STYLE_SHORT_TIME = 't'

STYLES

All valid Discord timestamp styles.

public array<string|int, string> STYLES = [self::STYLE_SHORT_TIME, self::STYLE_LONG_TIME, self::STYLE_SHORT_DATE, self::STYLE_LONG_DATE, self::STYLE_SHORT_DATE_TIME, self::STYLE_LONG_DATE_TIME, self::STYLE_SHORT_DATE_SHORT_TIME, self::STYLE_SHORT_DATE_MEDIUM_TIME, self::STYLE_RELATIVE_TIME]

Properties

$format

The Discord timestamp style, one of the `Timestamp::*` constants. Defaults to `Timestamp::STYLE_SHORT_DATE_TIME`.

protected string $format

$timestamp

Unix timestamp (in seconds) that will be displayed.

protected int $timestamp

Methods

__construct()

Creates a new Timestamp instance.

public __construct(DateTimeInterface|int|string $timestamp[, string $format = self::STYLE_SHORT_DATE_TIME ]) : mixed
Parameters
$timestamp : DateTimeInterface|int|string

A DateTimeInterface (e.g. DateTime, Carbon) or a Unix timestamp in seconds.

$format : string = self::STYLE_SHORT_DATE_TIME

One of the Timestamp::* style constants.

__get()

Handles dynamic get calls onto the part.

public __get(string $key) : mixed
Parameters
$key : string

The attributes key.

Tags
throws
Exception
see
self::getAttribute()

This function forwards onto getAttribute.

Return values
mixed

The value of the attribute.

__set()

Handles dynamic set calls onto the part.

public __set(string $key, mixed $value) : void
Parameters
$key : string

The attributes key.

$value : mixed

The attributes value.

Tags
see
self::setAttribute()

This function forwards onto setAttribute.

new()

Creates a new Timestamp instance.

public static new(DateTimeInterface|int|string $timestamp[, string $format = self::STYLE_SHORT_DATE_TIME ]) : self
Parameters
$timestamp : DateTimeInterface|int|string

A DateTimeInterface (e.g. DateTime, Carbon) or a Unix timestamp in seconds.

$format : string = self::STYLE_SHORT_DATE_TIME

One of the Timestamp::* style constants.

Return values
self

checkForGetMutator()

Checks if there is a get mutator present.

protected checkForGetMutator(string $key) : string|false
Parameters
$key : string

The property name to check.

Return values
string|false

Either a string if it is a method or false.

checkForSetMutator()

Checks if there is a set mutator present.

protected checkForSetMutator(string $key) : string|false
Parameters
$key : string

The property name to check.

Return values
string|false

Either a string if it is a method or false.

getFormat()

protected getFormat() : string
Return values
string

One of the Timestamp::STYLE_* constants.

getProperty()

Gets a property on the parent part.

protected getProperty(string $key) : mixed
Parameters
$key : string

The name of the property.

Tags
throws
Exception
Return values
mixed

Either the property if it exists or void.

getTimestamp()

protected getTimestamp() : int
Return values
int

Unix timestamp (in seconds).

setFormat()

Sets the `format` attribute, ensuring it is one of the valid Discord timestamp styles.

protected setFormat(string $format) : void
Parameters
$format : string
Tags
throws
InvalidArgumentException

setProperty()

Sets an property on the parent part.

protected setProperty(string $key, mixed $value) : void
Parameters
$key : string

The name of the property.

$value : mixed

The value of the property.

setTimestamp()

Normalizes and sets the `timestamp` attribute from a `DateTimeInterface` or Unix timestamp.

protected setTimestamp(DateTimeInterface|int|string $timestamp) : void
Parameters
$timestamp : DateTimeInterface|int|string

studly()

Converts a string to studlyCase.

private static studly(string $string) : string

This is a port of updated Laravel's implementation, a non-regex with static cache. This method is private static as we may move it outside this class in future.

Parameters
$string : string

The string to convert.

Tags
since
10.0.0
Return values
string
On this page

Search results