MediaWiki REL1_28
ConvertibleTimestamp Class Reference

Library for creating, parsing, and converting timestamps. More...

Inheritance diagram for ConvertibleTimestamp:

Public Member Functions

 __construct ( $timestamp=false)
 Make a new timestamp and set it to the specified time, or the current time if unspecified.
 
 __toString ()
 
 diff (ConvertibleTimestamp $relativeTo)
 Calculate the difference between two ConvertibleTimestamp objects.
 
 format ( $format)
 Format the timestamp in a given format.
 
 getTimestamp ( $style=TS_UNIX)
 Get the timestamp represented by this object in a certain form.
 
 getTimezone ()
 Get the timezone of this timestamp.
 
 setTimestamp ( $ts=false)
 Set the timestamp to the specified time, or the current time if unspecified.
 
 setTimezone ( $timezone)
 Set the timezone of this timestamp to the specified timezone.
 

Static Public Member Functions

static convert ( $style=TS_UNIX, $ts)
 Convert a timestamp string to a given format.
 
static now ( $style=TS_MW)
 Get the current time in the given format.
 

Public Attributes

DateTime $timestamp
 The actual timestamp being wrapped (DateTime object).
 

Static Private Attributes

static $formats
 Standard gmdate() formats for the different timestamp types.
 

Detailed Description

Library for creating, parsing, and converting timestamps.

Based on the JS library that does the same thing.

Since
1.28

Definition at line 31 of file ConvertibleTimestamp.php.

Constructor & Destructor Documentation

◆ __construct()

ConvertibleTimestamp::__construct (   $timestamp = false)

Make a new timestamp and set it to the specified time, or the current time if unspecified.

Parameters
bool | string | int | float | DateTime$timestampTimestamp to set, or false for current time

Definition at line 59 of file ConvertibleTimestamp.php.

References $timestamp, and setTimestamp().

Member Function Documentation

◆ __toString()

ConvertibleTimestamp::__toString ( )
Returns
string

Definition at line 222 of file ConvertibleTimestamp.php.

References getTimestamp().

◆ convert()

static ConvertibleTimestamp::convert (   $style = TS_UNIX,
  $ts 
)
static

Convert a timestamp string to a given format.

Parameters
int$styleConstant Output format for timestamp
string$tsTimestamp
Returns
string|bool Formatted timestamp or false on failure

Definition at line 171 of file ConvertibleTimestamp.php.

References $e.

Referenced by Database\decodeExpiry(), FileBackendStore\setFileCache(), ConvertibleTimestampTest\testConvert(), ConvertibleTimestampTest\testConvertInvalid(), XMPValidate\validateDate(), and wfTimestamp().

◆ diff()

ConvertibleTimestamp::diff ( ConvertibleTimestamp  $relativeTo)

Calculate the difference between two ConvertibleTimestamp objects.

Parameters
ConvertibleTimestamp$relativeToBase time to calculate difference from
Returns
DateInterval|bool The DateInterval object representing the difference between the two dates or false on failure

Definition at line 233 of file ConvertibleTimestamp.php.

Referenced by Language\getHumanTimestampInternal().

◆ format()

ConvertibleTimestamp::format (   $format)

Format the timestamp in a given format.

Parameters
string$formatPattern to format in
Returns
string The formatted timestamp

Definition at line 266 of file ConvertibleTimestamp.php.

Referenced by Language\getHumanTimestampInternal(), and MWTimestamp\getTimezoneMessage().

◆ getTimestamp()

ConvertibleTimestamp::getTimestamp (   $style = TS_UNIX)

Get the timestamp represented by this object in a certain form.

Convert the internal timestamp to the specified format and then return it.

Parameters
int$styleConstant Output format for timestamp
Exceptions
TimestampException
Returns
string The formatted timestamp

Definition at line 200 of file ConvertibleTimestamp.php.

References $output, TS_MW, TS_POSTGRES, and TS_RFC2822.

Referenced by __toString(), and Language\getHumanTimestampInternal().

◆ getTimezone()

ConvertibleTimestamp::getTimezone ( )

Get the timezone of this timestamp.

Returns
DateTimeZone The timezone

Definition at line 256 of file ConvertibleTimestamp.php.

◆ now()

static ConvertibleTimestamp::now (   $style = TS_MW)
static

Get the current time in the given format.

Parameters
int$styleConstant Output format for timestamp
Returns
string

Definition at line 186 of file ConvertibleTimestamp.php.

Referenced by wfTimestampNow().

◆ setTimestamp()

ConvertibleTimestamp::setTimestamp (   $ts = false)

Set the timestamp to the specified time, or the current time if unspecified.

Parse the given timestamp into either a DateTime object or a Unix timestamp, and then store it.

Parameters
string | bool$tsTimestamp to store, or false for now
Exceptions
TimestampException

Definition at line 76 of file ConvertibleTimestamp.php.

References $e, and of.

Referenced by __construct().

◆ setTimezone()

ConvertibleTimestamp::setTimezone (   $timezone)

Set the timezone of this timestamp to the specified timezone.

Parameters
string$timezoneTimezone to set
Exceptions
TimestampException

Definition at line 243 of file ConvertibleTimestamp.php.

References $e.

Member Data Documentation

◆ $formats

ConvertibleTimestamp::$formats
staticprivate
Initial value:
= [
TS_UNIX => 'U',
TS_MW => 'YmdHis',
TS_DB => 'Y-m-d H:i:s',
TS_ISO_8601 => 'Y-m-d\TH:i:s\Z',
TS_ISO_8601_BASIC => 'Ymd\THis\Z',
TS_EXIF => 'Y:m:d H:i:s',
TS_RFC2822 => 'D, d M Y H:i:s',
TS_ORACLE => 'd-m-Y H:i:s.000000',
TS_POSTGRES => 'Y-m-d H:i:s',
]
const TS_ORACLE
Oracle format time.
Definition defines.php:42
const TS_POSTGRES
Postgres format time.
Definition defines.php:47
const TS_DB
MySQL DATETIME (YYYY-MM-DD HH:MM:SS)
Definition defines.php:16
const TS_ISO_8601
ISO 8601 format with no timezone: 1986-02-09T20:00:00Z.
Definition defines.php:28
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition defines.php:6
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition defines.php:11
const TS_RFC2822
RFC 2822 format, for E-mail and HTTP headers.
Definition defines.php:21
const TS_ISO_8601_BASIC
ISO 8601 basic format with no timezone: 19860209T200000Z.
Definition defines.php:52
const TS_EXIF
An Exif timestamp (YYYY:MM:DD HH:MM:SS)
Definition defines.php:37

Standard gmdate() formats for the different timestamp types.

Definition at line 35 of file ConvertibleTimestamp.php.

◆ $timestamp

DateTime ConvertibleTimestamp::$timestamp

The actual timestamp being wrapped (DateTime object).

Definition at line 51 of file ConvertibleTimestamp.php.

Referenced by __construct(), and MWTimestamp\getLocalInstance().


The documentation for this class was generated from the following file: