MediaWiki  REL1_21
Timestamp.php
Go to the documentation of this file.
00001 <?php
00031 class MWTimestamp {
00035         private static $formats = array(
00036                 TS_UNIX => 'U',
00037                 TS_MW => 'YmdHis',
00038                 TS_DB => 'Y-m-d H:i:s',
00039                 TS_ISO_8601 => 'Y-m-d\TH:i:s\Z',
00040                 TS_ISO_8601_BASIC => 'Ymd\THis\Z',
00041                 TS_EXIF => 'Y:m:d H:i:s', // This shouldn't ever be used, but is included for completeness
00042                 TS_RFC2822 => 'D, d M Y H:i:s',
00043                 TS_ORACLE => 'd-m-Y H:i:s.000000', // Was 'd-M-y h.i.s A' . ' +00:00' before r51500
00044                 TS_POSTGRES => 'Y-m-d H:i:s',
00045         );
00046 
00051         private static $units = array(
00052                 "milliseconds" => 1,
00053                 "seconds" => 1000, // 1000 milliseconds per second
00054                 "minutes" => 60, // 60 seconds per minute
00055                 "hours" => 60, // 60 minutes per hour
00056                 "days" => 24, // 24 hours per day
00057                 "months" => 30, // approximately 30 days per month
00058                 "years" => 12, // 12 months per year
00059         );
00060 
00067         private $timestamp;
00068 
00077         public function __construct( $timestamp = false ) {
00078                 $this->setTimestamp( $timestamp );
00079         }
00080 
00092         public function setTimestamp( $ts = false ) {
00093                 $da = array();
00094                 $strtime = '';
00095 
00096                 if ( !$ts || $ts === "\0\0\0\0\0\0\0\0\0\0\0\0\0\0" ) { // We want to catch 0, '', null... but not date strings starting with a letter.
00097                         $uts = time();
00098                         $strtime = "@$uts";
00099                 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
00100                         # TS_DB
00101                 } elseif ( preg_match( '/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
00102                         # TS_EXIF
00103                 } elseif ( preg_match( '/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D', $ts, $da ) ) {
00104                         # TS_MW
00105                 } elseif ( preg_match( '/^-?\d{1,13}$/D', $ts ) ) {
00106                         # TS_UNIX
00107                         $strtime = "@$ts"; // http://php.net/manual/en/datetime.formats.compound.php
00108                 } elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) {
00109                         # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6
00110                         $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
00111                                         str_replace( '+00:00', 'UTC', $ts ) );
00112                 } elseif ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
00113                         # TS_ISO_8601
00114                 } elseif ( preg_match( '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
00115                         #TS_ISO_8601_BASIC
00116                 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/', $ts, $da ) ) {
00117                         # TS_POSTGRES
00118                 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d* GMT$/', $ts, $da ) ) {
00119                         # TS_POSTGRES
00120                 } elseif ( preg_match( '/^[ \t\r\n]*([A-Z][a-z]{2},[ \t\r\n]*)?' . # Day of week
00121                                                                 '\d\d?[ \t\r\n]*[A-Z][a-z]{2}[ \t\r\n]*\d{2}(?:\d{2})?' .  # dd Mon yyyy
00122                                                                 '[ \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
00123                         # TS_RFC2822, accepting a trailing comment. See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html / r77171
00124                         # The regex is a superset of rfc2822 for readability
00125                         $strtime = strtok( $ts, ';' );
00126                 } 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 ) ) {
00127                         # TS_RFC850
00128                         $strtime = $ts;
00129                 } 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 ) ) {
00130                         # asctime
00131                         $strtime = $ts;
00132                 } else {
00133                         throw new TimestampException( __METHOD__ . " : Invalid timestamp - $ts" );
00134                 }
00135 
00136                 if( !$strtime ) {
00137                         $da = array_map( 'intval', $da );
00138                         $da[0] = "%04d-%02d-%02dT%02d:%02d:%02d.00+00:00";
00139                         $strtime = call_user_func_array( "sprintf", $da );
00140                 }
00141 
00142                 try {
00143                         $final = new DateTime( $strtime, new DateTimeZone( 'GMT' ) );
00144                 } catch(Exception $e) {
00145                         throw new TimestampException( __METHOD__ . ' Invalid timestamp format.' );
00146                 }
00147 
00148                 if( $final === false ) {
00149                         throw new TimestampException( __METHOD__ . ' Invalid timestamp format.' );
00150                 }
00151                 $this->timestamp = $final;
00152         }
00153 
00166         public function getTimestamp( $style = TS_UNIX ) {
00167                 if( !isset( self::$formats[$style] ) ) {
00168                         throw new TimestampException( __METHOD__ . ' : Illegal timestamp output type.' );
00169                 }
00170 
00171                 if( is_object( $this->timestamp  ) ) {
00172                         // DateTime object was used, call DateTime::format.
00173                         $output = $this->timestamp->format( self::$formats[$style] );
00174                 } elseif( TS_UNIX == $style ) {
00175                         // Unix timestamp was used and is wanted, just return it.
00176                         $output = $this->timestamp;
00177                 } else {
00178                         // Unix timestamp was used, use gmdate().
00179                         $output = gmdate( self::$formats[$style], $this->timestamp );
00180                 }
00181 
00182                 if ( ( $style == TS_RFC2822 ) || ( $style == TS_POSTGRES ) ) {
00183                         $output .= ' GMT';
00184                 }
00185 
00186                 return $output;
00187         }
00188 
00200         public function getHumanTimestamp() {
00201                 $then = $this->getTimestamp( TS_UNIX );
00202                 $now = time();
00203                 $timeago = ($now - $then) * 1000;
00204                 $message = false;
00205 
00206                 foreach( self::$units as $unit => $factor ) {
00207                         $next = $timeago / $factor;
00208                         if( $next < 1 ) {
00209                                 break;
00210                         } else {
00211                                 $timeago = $next;
00212                                 $message = array( $unit, floor( $timeago ) );
00213                         }
00214                 }
00215 
00216                 if( $message ) {
00217                         $initial = call_user_func_array( 'wfMessage', $message );
00218                         return wfMessage( 'ago', $initial->parse() );
00219                 } else {
00220                         return wfMessage( 'just-now' );
00221                 }
00222         }
00223 
00229         public function __toString() {
00230                 return $this->getTimestamp();
00231         }
00232 }
00233 
00237 class TimestampException extends MWException {}