Function: DynamicBase::timeSince
avatar
Señor FAQ

¡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.


Function: 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.

Syntax


DynamicBase::timeSince($timestamp, string $dateFormat, string $timeFormat, string $lang, bool $useTimeAgo = false): string
    

Parameters

  • $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.

Returns

(string) A formatted date/time string or "time ago" string depending on the parameters.

Example Usage


<?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"
?>
    

Best Practices

  • Use this function to display user-friendly timestamps for activities like comments, posts, or logs.
  • Ensure the $timestamp is correctly formatted or converted to avoid parsing issues.
  • Pass the appropriate language strings in $lang for consistent localization.

Common Issues

  • Invalid Timestamps: Ensure the $timestamp is a valid date or Unix timestamp. Invalid timestamps may result in PHP warnings or unexpected behavior.
  • Improper Language Strings: Verify that the $lang parameter includes the necessary time unit translations and plurals.
Was this article helpful?
0 out of 0 found this helpful