¡Hola, amigos! I’m Señor FAQ, the mustached maestro of questions and answers! With my trusty glasses and a book of endless wisdom, I turn dudas into solutions. Soy el héroe de los curiosos and the champion of clarity.
DynamicBase::timeSince
The DynamicBase::timeSince
function calculates the time elapsed since a given timestamp and formats it either in "time ago" format or a standard date/time format.
DynamicBase::timeSince($timestamp, string $dateFormat, string $timeFormat, string $lang, bool $useTimeAgo = false): string
$timestamp
: (string|int) The input timestamp, either a MySQL timestamp or Unix timestamp.$dateFormat
: (string) The format for the date in case time ago
is not used.$timeFormat
: (string) The format for the time in case time ago
is not used.$lang
: (string) Language strings for time units, provided as a comma-separated string (e.g., "days,hours,minutes,seconds,day,hour,minute,second,ago").$useTimeAgo
: (bool) Optional. Determines whether to use "time ago" format. Default is false
.(string) A formatted date/time string or "time ago" string depending on the parameters.
<?php
// Example with "time ago" enabled
echo DynamicBase::timeSince(time(), 'Y-m-d', 'H:i:s', 'days,hours,minutes,seconds,day,hour,minute,second,ago', true);
// Outputs: "1 hour ago"
// Example with standard date format
echo DynamicBase::timeSince('2025-01-01 12:00:00', 'Y-m-d', 'H:i:s', 'days,hours,minutes,seconds,day,hour,minute,second,ago', false);
// Outputs: "2025-01-01 12:00:00"
// Example with site vars
echo DynamicBase::timeSince(time(), $jkv["date_format"], $jkv["time_format"], $time_ago, $jkv["time_ago_show"]);
// Outputs: "1 hour ago"
?>
$timestamp
is correctly formatted or converted to avoid parsing issues.$lang
for consistent localization.$timestamp
is a valid date or Unix timestamp. Invalid timestamps may result in PHP warnings or unexpected behavior.$lang
parameter includes the necessary time unit translations and plurals.