1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-02-02 01:48:23 +01:00

Relative date helper, for 'today' and 'yesterday'

Summary:
Format a date as 'today', 'yesterday', or 'Mar 27 2012'.  Optionally,
the final example can be rendered 'on Mar 27 2012' for things like:

  $excuse =
    'I fell out of a window '.
    phabricator_on_rel_date($time, $me);

Test Plan: Tested in my sandbox!!!!

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, epriestley

Differential Revision: https://secure.phabricator.com/D2035
This commit is contained in:
Edward Speyer 2012-03-27 18:16:59 -07:00
parent 99704ed485
commit c15d8d4d23
2 changed files with 29 additions and 6 deletions

View file

@ -59,16 +59,12 @@ final class PhabricatorFeedBuilder {
require_celerity_resource('phabricator-feed-css');
$last_date = null;
$today = phabricator_date(time(), $user);
foreach ($stories as $story) {
$story->setHandles($handles);
$story->setObjects($objects);
$story->setFramed($this->framed);
$date = phabricator_date($story->getEpoch(), $user);
if ($date == $today) {
$date = 'Today';
}
$date = ucfirst(phabricator_relative_date($story->getEpoch(), $user));
if ($date !== $last_date) {
if ($last_date !== null) {

View file

@ -1,7 +1,7 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,6 +23,33 @@ function phabricator_date($epoch, $user) {
'M j Y');
}
function phabricator_on_relative_date($epoch, $user) {
return phabricator_relative_date($epoch, $user, true);
}
function phabricator_relative_date($epoch, $user, $on = false) {
static $today;
static $yesterday;
if (!$today || !$yesterday) {
$now = time();
$today = phabricator_date($now, $user);
$yesterday = phabricator_date($now - 86400, $user);
}
$date = phabricator_date($epoch, $user);
if ($date === $today) {
return 'today';
}
if ($date === $yesterday) {
return 'yesterday';
}
return (($on ? 'on ' : '').$date);
}
function phabricator_time($epoch, $user) {
return __phabricator_format_local_time(
$epoch,