4 * Library for creating and parsing MW-style timestamps. Based on the JS
5 * library that does the same thing.
7 * @author Tyler Romeo, 2012
12 * Standard gmdate() formats for the different timestamp types.
14 private static $formats = array(
17 TS_DB
=> 'Y-m-d H:i:s',
18 TS_ISO_8601
=> 'Y-m-d\TH:i:s\Z',
19 TS_ISO_8601_BASIC
=> 'Ymd\THis\Z',
20 TS_EXIF
=> 'Y:m:d H:i:s', // This shouldn't ever be used, but is included for completeness
21 TS_RFC2822
=> 'D, d M Y H:i:s',
22 TS_ORACLE
=> 'd-m-Y H:i:s.000000', // Was 'd-M-y h.i.s A' . ' +00:00' before r51500
23 TS_POSTGRES
=> 'Y-m-d H:i:s',
24 TS_DB2
=> 'Y-m-d H:i:s',
28 * Different units for human readable timestamps.
29 * @see MWTimestamp::getHumanTimestamp
31 private static $units = array(
33 "seconds" => 1000, // 1000 milliseconds per second
34 "minutes" => 60, // 60 seconds per minute
35 "hours" => 60, // 60 minutes per hour
36 "days" => 24 // 24 hours per day
40 * The actual timestamp being wrapped. Either a DateTime
41 * object or a string with a Unix timestamp depending on
47 * Make a new timestamp and set it to the specified time,
48 * or the current time if unspecified.
50 * @param $timestamp Timestamp to set, or false for current time
52 public function __construct( $timestamp = false ) {
53 $this->setTimestamp( $timestamp );
57 * Set the timestamp to the specified time, or the current time if unspecified.
59 * Parse the given timestamp into either a DateTime object or a Unix teimstamp,
62 * @param $ts Timestamp to store, or false for now
64 public function setTimestamp( $ts = false ) {
69 if ( !$ts ) { // We want to catch 0, '', null... but not date strings starting with a letter.
72 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
74 } elseif ( preg_match( '/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
76 } elseif ( preg_match( '/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D', $ts, $da ) ) {
78 } elseif ( preg_match( '/^-?\d{1,13}$/D', $ts ) ) {
81 $strtime = "@$ts"; // http://php.net/manual/en/datetime.formats.compound.php
82 } elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) {
83 # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6
84 $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
85 str_replace( '+00:00', 'UTC', $ts ) );
86 } elseif ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
88 } elseif ( preg_match( '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
90 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/', $ts, $da ) ) {
92 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d* GMT$/', $ts, $da ) ) {
94 } elseif (preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.\d\d\d$/', $ts, $da ) ) {
96 } elseif ( preg_match( '/^[ \t\r\n]*([A-Z][a-z]{2},[ \t\r\n]*)?' . # Day of week
97 '\d\d?[ \t\r\n]*[A-Z][a-z]{2}[ \t\r\n]*\d{2}(?:\d{2})?' . # dd Mon yyyy
98 '[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d/S', $ts ) ) { # hh:mm:ss
99 # TS_RFC2822, accepting a trailing comment. See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html / r77171
100 # The regex is a superset of rfc2822 for readability
101 $strtime = strtok( $ts, ';' );
102 } elseif ( preg_match( '/^[A-Z][a-z]{5,8}, \d\d-[A-Z][a-z]{2}-\d{2} \d\d:\d\d:\d\d/', $ts ) ) {
105 } elseif ( preg_match( '/^[A-Z][a-z]{2} [A-Z][a-z]{2} +\d{1,2} \d\d:\d\d:\d\d \d{4}/', $ts ) ) {
109 throw new TimestampException( __METHOD__
. " : Invalid timestamp - $ts" );
113 $da = array_map( 'intval', $da );
114 $da[0] = "%04d-%02d-%02dT%02d:%02d:%02d.00+00:00";
115 $strtime = call_user_func_array( "sprintf", $da );
118 if( function_exists( "date_create" ) ) {
120 $final = new DateTime( $strtime, new DateTimeZone( 'GMT' ) );
121 } catch(Exception
$e) {
122 throw new TimestampException( __METHOD__
. 'Invalid timestamp format.' );
125 $final = strtotime( $strtime );
128 if( $final === false ) {
129 throw new TimestampException( __METHOD__
. 'Invalid timestamp format.' );
131 $this->timestamp
= $final;
135 * Get the timestamp represented by this object in a certain form.
137 * Convert the internal timestamp to the specified format and then
140 * @param $style Output format for timestamp
141 * @return string The formatted timestamp
143 public function getTimestamp( $style = TS_UNIX
) {
144 if( !isset( self
::$formats[$style] ) ) {
145 throw new TimestampException( __METHOD__
. ' : Illegal timestamp output type.' );
148 if( is_object( $this->timestamp
) ) {
149 // DateTime object was used, call DateTime::format.
150 $output = $this->timestamp
->format( self
::$formats[$style] );
151 } elseif( TS_UNIX
== $style ) {
152 // Unix timestamp was used and is wanted, just return it.
153 $output = $this->timestamp
;
155 // Unix timestamp was used, use gmdate().
156 $output = gmdate( self
::$formats[$style], $this->timestamp
);
159 if ( ( $style == TS_RFC2822
) ||
( $style == TS_POSTGRES
) ) {
167 * Get the timestamp in a human-friendly relative format, e.g., "3 days ago".
169 * Determine the difference between the timestamp and the current time, and
170 * generate a readable timestamp by returning "<N> <units> ago", where the
171 * largest possible unit is used.
173 * @return string Formatted timestamp
175 public function getHumanTimestamp() {
176 $then = $this->getTimestamp( TS_UNIX
);
178 $timeago = ($now - $then) * 1000;
181 foreach( self
::$units as $unit => $factor ) {
182 $next = $timeago / $factor;
187 $message = array( $unit, floor( $timeago ) );
192 $initial = call_user_func_array( 'wfMessage', $message );
193 return wfMessage( 'ago', $initial );
195 return wfMessage( 'just-now' );
199 public function __toString() {
200 return $this->getTimestamp();
204 class TimestampException
extends MWException
{}