X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FTimestamp.php;h=9952001e7328a4fd852eb465a4bb6aad96be600f;hb=fa84729e54d2256c5b352e096f8a23c049a307d5;hp=5296122ef116e3e88d28041483b4e1257f354975;hpb=fd0ff7a312126ca68e2ce4a11418d7854395f46a;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/Timestamp.php b/includes/Timestamp.php index 5296122ef1..9952001e73 100644 --- a/includes/Timestamp.php +++ b/includes/Timestamp.php @@ -270,6 +270,44 @@ class MWTimestamp { return $interval; } + /** + * Generate a purely relative timestamp, i.e., represent the time elapsed between + * the given base timestamp and this object. + * + * @param MWTimestamp $relativeTo Relative base timestamp (defaults to now) + * @param User $user Use to use offset for + * @param Language $lang Language to use + * @param array $chosenIntervals Intervals to use to represent it + * @return string Relative timestamp + */ + public function getRelativeTimestamp( + MWTimestamp $relativeTo = null, + User $user = null, + Language $lang = null, + array $chosenIntervals = array() + ) { + if ( $relativeTo === null ) { + $relativeTo = new self; + } + if ( $user === null ) { + $user = RequestContext::getMain()->getUser(); + } + if ( $lang === null ) { + $lang = RequestContext::getMain()->getLanguage(); + } + + $ts = ''; + $diff = $this->diff( $relativeTo ); + if ( wfRunHooks( 'GetRelativeTimestamp', array( &$ts, &$diff, $this, $relativeTo, $user, $lang ) ) ) { + $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s ); + $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) ) + ->inLanguage( $lang ) + ->text(); + } + + return $ts; + } + /** * @since 1.20 *