MediaWiki  1.23.2
MWTimestamp Class Reference

Library for creating and parsing MW-style timestamps. More...

Public Member Functions

 __construct ( $timestamp=false)
 Make a new timestamp and set it to the specified time, or the current time if unspecified. More...
 
 __toString ()
 
 diff (MWTimestamp $relativeTo)
 Calculate the difference between two MWTimestamp objects. More...
 
 format ( $format)
 Format the timestamp in a given format. More...
 
 getHumanTimestamp (MWTimestamp $relativeTo=null, User $user=null, Language $lang=null)
 Get the timestamp in a human-friendly relative format, e.g., "3 days ago". More...
 
 getRelativeTimestamp (MWTimestamp $relativeTo=null, User $user=null, Language $lang=null, array $chosenIntervals=array())
 Generate a purely relative timestamp, i.e., represent the time elapsed between the given base timestamp and this object. More...
 
 getTimestamp ( $style=TS_UNIX)
 Get the timestamp represented by this object in a certain form. More...
 
 getTimezone ()
 Get the timezone of this timestamp. More...
 
 offsetForUser (User $user)
 Adjust the timestamp depending on the given user's preferences. More...
 
 setTimestamp ( $ts=false)
 Set the timestamp to the specified time, or the current time if unspecified. More...
 
 setTimezone ( $timezone)
 Set the timezone of this timestamp to the specified timezone. More...
 

Static Public Member Functions

static getInstance ( $ts=false)
 Get a timestamp instance in GMT. More...
 
static getLocalInstance ( $ts=false)
 Get a timestamp instance in the server local timezone ($wgLocaltimezone) More...
 

Public Attributes

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

Static Private Attributes

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

Detailed Description

Library for creating and parsing MW-style timestamps.

Based on the JS library that does the same thing.

Since
1.20

Definition at line 31 of file MWTimestamp.php.

Constructor & Destructor Documentation

◆ __construct()

MWTimestamp::__construct (   $timestamp = false)

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

Since
1.20
Parameters
bool | string$timestampTimestamp to set, or false for current time

Definition at line 60 of file MWTimestamp.php.

References setTimestamp().

Member Function Documentation

◆ __toString()

MWTimestamp::__toString ( )
Since
1.20
Returns
string

Definition at line 315 of file MWTimestamp.php.

References getTimestamp().

◆ diff()

MWTimestamp::diff ( MWTimestamp  $relativeTo)

Calculate the difference between two MWTimestamp objects.

Since
1.22
Parameters
MWTimestamp$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 326 of file MWTimestamp.php.

Referenced by Language\getHumanTimestamp(), and getRelativeTimestamp().

◆ format()

MWTimestamp::format (   $format)

Format the timestamp in a given format.

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

Definition at line 362 of file MWTimestamp.php.

Referenced by Language\getHumanTimestamp().

◆ getHumanTimestamp()

MWTimestamp::getHumanTimestamp ( MWTimestamp  $relativeTo = null,
User  $user = null,
Language  $lang = null 
)

Get the timestamp in a human-friendly relative format, e.g., "3 days ago".

Determine the difference between the timestamp and the current time, and generate a readable timestamp by returning "<N> <units> ago", where the largest possible unit is used.

Since
1.20
1.22 Uses Language::getHumanTimestamp to produce the timestamp
Parameters
MWTimestamp | null$relativeToThe base timestamp to compare to (defaults to now)
User | null$userUser the timestamp is being generated for (or null to use main context's user)
Language | null$langLanguage to use to make the human timestamp (or null to use main context's language)
Returns
string Formatted timestamp

Definition at line 178 of file MWTimestamp.php.

References $user, array(), RequestContext\getMain(), offsetForUser(), and wfRunHooks().

◆ getInstance()

static MWTimestamp::getInstance (   $ts = false)
static

Get a timestamp instance in GMT.

Since
1.22
Parameters
bool | string$tsTimestamp to set, or false for current time
Returns
MWTimestamp the instance

Definition at line 387 of file MWTimestamp.php.

Referenced by Xml\dateMenu(), ReverseChronologicalPager\getDateCond(), and FileRepo\storeTemp().

◆ getLocalInstance()

static MWTimestamp::getLocalInstance (   $ts = false)
static

Get a timestamp instance in the server local timezone ($wgLocaltimezone)

Since
1.22
Parameters
bool | string$tsTimestamp to set, or false for current time
Returns
MWTimestamp the local instance

Definition at line 373 of file MWTimestamp.php.

References $timestamp, and global.

Referenced by DatabaseOracle\doQuery(), SpecialVersion\getCopyrightAndAuthorList(), Preferences\getTimezoneOptions(), HistoryAction\onView(), CoreParserFunctions\revisionday(), CoreParserFunctions\revisionday2(), CoreParserFunctions\revisionmonth(), CoreParserFunctions\revisionmonth1(), CoreParserFunctions\revisiontimestamp(), CoreParserFunctions\revisionyear(), and UserMailer\send().

◆ getRelativeTimestamp()

MWTimestamp::getRelativeTimestamp ( MWTimestamp  $relativeTo = null,
User  $user = null,
Language  $lang = null,
array  $chosenIntervals = array() 
)

Generate a purely relative timestamp, i.e., represent the time elapsed between the given base timestamp and this object.

Parameters
MWTimestamp$relativeToRelative base timestamp (defaults to now)
User$userUse to use offset for
Language$langLanguage to use
array$chosenIntervalsIntervals to use to represent it
Returns
string Relative timestamp

Definition at line 282 of file MWTimestamp.php.

References $user, array(), diff(), RequestContext\getMain(), wfMessage(), and wfRunHooks().

◆ getTimestamp()

MWTimestamp::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.

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

Definition at line 149 of file MWTimestamp.php.

References $output, TS_POSTGRES, and TS_RFC2822.

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

◆ getTimezone()

MWTimestamp::getTimezone ( )

Get the timezone of this timestamp.

Since
1.22
Returns
DateTimeZone The timezone

Definition at line 351 of file MWTimestamp.php.

◆ offsetForUser()

MWTimestamp::offsetForUser ( User  $user)

Adjust the timestamp depending on the given user's preferences.

Since
1.22
Parameters
User$userUser to take preferences from
[out]MWTimestamp$tsTimestamp to adjust
Returns
DateInterval Offset that was applied to the timestamp

Definition at line 214 of file MWTimestamp.php.

References $e, $user, and global.

Referenced by getHumanTimestamp().

◆ setTimestamp()

MWTimestamp::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.

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

Definition at line 75 of file MWTimestamp.php.

References $e, array(), and of.

Referenced by __construct().

◆ setTimezone()

MWTimestamp::setTimezone (   $timezone)

Set the timezone of this timestamp to the specified timezone.

Since
1.22
Parameters
String$timezoneTimezone to set
Exceptions
TimestampException

Definition at line 337 of file MWTimestamp.php.

References $e.

Member Data Documentation

◆ $formats

MWTimestamp::$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',
)

Standard gmdate() formats for the different timestamp types.

Definition at line 35 of file MWTimestamp.php.

◆ $timestamp

DateTime MWTimestamp::$timestamp

The actual timestamp being wrapped (DateTime object).

Definition at line 50 of file MWTimestamp.php.

Referenced by getLocalInstance().


The documentation for this class was generated from the following file:
TS_EXIF
const TS_EXIF
An Exif timestamp (YYYY:MM:DD HH:MM:SS)
Definition: GlobalFunctions.php:2457
TS_DB
const TS_DB
MySQL DATETIME (YYYY-MM-DD HH:MM:SS)
Definition: GlobalFunctions.php:2436
TS_ORACLE
const TS_ORACLE
Oracle format time.
Definition: GlobalFunctions.php:2462
TS_ISO_8601
const TS_ISO_8601
ISO 8601 format with no timezone: 1986-02-09T20:00:00Z.
Definition: GlobalFunctions.php:2448
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2431
TS_ISO_8601_BASIC
const TS_ISO_8601_BASIC
ISO 8601 basic format with no timezone: 19860209T200000Z.
Definition: GlobalFunctions.php:2472
TS_UNIX
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: GlobalFunctions.php:2426
TS_POSTGRES
const TS_POSTGRES
Postgres format time.
Definition: GlobalFunctions.php:2467
TS_RFC2822
const TS_RFC2822
RFC 2822 format, for E-mail and HTTP headers.
Definition: GlobalFunctions.php:2441