<?php
/**
* SeekQuarry/Yioop --
* Open Source Pure PHP Search Engine, Crawler, and Indexer
*
* Copyright (C) 2009 - 2026 Chris Pollett chris@pollett.org
*
* LICENSE:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* END LICENSE
*
* @author Chris Pollett chris@pollett.org
* @license https://www.gnu.org/licenses/ GPL3
* @link https://www.seekquarry.com/
* @copyright 2009 - 2026
* @filesource
*/
namespace seekquarry\yioop\views\helpers;
use seekquarry\yioop\configs as C;
/**
* This helper class turns a moment in time into words describing how long
* ago it was, such as five minutes ago or two years ago, for display near
* things like commits and issues.
*
* @author Chris Pollett
*/
class TimeformatterHelper extends Helper
{
/**
* Describes in words how long ago a moment was, choosing the largest
* unit that fits, from years down to minutes, and saying moments ago for
* anything under a minute. A moment at or before the start of time gives
* back the empty string.
*
* @param int $timestamp the moment to describe, as a Unix timestamp
* @return string the phrase for how long ago, or the empty string
*/
public function timeAgo($timestamp)
{
if ($timestamp <= 0) {
return "";
}
$elapsed = time() - $timestamp;
if ($elapsed < 0) {
$elapsed = 0;
}
if ($elapsed >= C\ONE_YEAR) {
$count = (int)($elapsed / C\ONE_YEAR);
if ($count === 1) {
return tl('time_formatter_helper_year', $count);
}
return tl('time_formatter_helper_years', $count);
}
if ($elapsed >= C\ONE_MONTH) {
$count = (int)($elapsed / C\ONE_MONTH);
if ($count === 1) {
return tl('time_formatter_helper_month', $count);
}
return tl('time_formatter_helper_months', $count);
}
if ($elapsed >= C\ONE_WEEK) {
$count = (int)($elapsed / C\ONE_WEEK);
if ($count === 1) {
return tl('time_formatter_helper_week', $count);
}
return tl('time_formatter_helper_weeks', $count);
}
if ($elapsed >= C\ONE_DAY) {
$count = (int)($elapsed / C\ONE_DAY);
if ($count === 1) {
return tl('time_formatter_helper_day', $count);
}
return tl('time_formatter_helper_days', $count);
}
if ($elapsed >= C\ONE_HOUR) {
$count = (int)($elapsed / C\ONE_HOUR);
if ($count === 1) {
return tl('time_formatter_helper_hour', $count);
}
return tl('time_formatter_helper_hours', $count);
}
if ($elapsed >= C\ONE_MINUTE) {
$count = (int)($elapsed / C\ONE_MINUTE);
if ($count === 1) {
return tl('time_formatter_helper_minute', $count);
}
return tl('time_formatter_helper_minutes', $count);
}
return tl('time_formatter_helper_moments');
}
}