CMXX - Relative time on created and last login

This commit is contained in:
“VeLiTi”
2024-12-12 15:28:39 +01:00
parent 1b17a98e1f
commit 5e858fb785
13 changed files with 187 additions and 8 deletions

View File

@@ -2837,4 +2837,78 @@ function getLatestVersion($productcode,$token){
//DEFAULT OUTPUT
return $responses;
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Timestamp converter ++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
function getRelativeTime($timestamp) {
//GET TRANSLATION FILE
if(isset($_SESSION['country_code'])){
$api_file_language = dirname(__FILE__,2).'/settings/translations/translations_'.strtoupper($_SESSION['country_code']).'.php';
if (file_exists($api_file_language)){
include $api_file_language; //Include the code
}
else {
include dirname(__FILE__,2).'/settings/translations/translations_US.php';
}
}
else {
include dirname(__FILE__,2).'/settings/translations/translations_US.php';
}
// Ensure the timestamp is a valid integer
$timestamp = is_numeric($timestamp) ? $timestamp : strtotime($timestamp);
// Get current timestamp and calculate difference
$now = time();
$diff = $now - $timestamp;
// Define time periods
$minute = 60;
$hour = $minute * 60;
$day = $hour * 24;
$week = $day * 7;
$month = $day * 30;
$year = $day * 365;
// Handle future timestamps
if ($diff < 0) {
$diff = abs($diff);
$suffix = $time_from_now;
} else {
$suffix = $time_ago;
}
// Determine the appropriate time description
if ($diff < $minute) {
return $time_just_now;
} elseif ($diff < $hour) {
$minutes = floor($diff / $minute);
return $minutes.$time_minute . ($minutes != 1 ? $time_minutes : '') . " $suffix";
} elseif ($diff < $day) {
$hours = floor($diff / $hour);
return $hours.$time_hour . ($hours != 1 ? $time_hours : '') . " $suffix";
} elseif ($diff < $week) {
$days = floor($diff / $day);
// Special handling for today and yesterday
if ($days == 0) {
return $time_today;
} elseif ($days == 1) {
return $time_yesterday;
}
return $days.(($days != 1)?$time_days:$time_day) . " $suffix";
} elseif ($diff < $month) {
$weeks = floor($diff / $week);
return $weeks.(($weeks != 1)?$time_weeks:$time_week) . " $suffix";
} elseif ($diff < $year) {
$months = floor($diff / $month);
return $months.(($months != 1)?$time_months:$time_month) . " $suffix";
} else {
$years = floor($diff / $year);
return $years.(($years != 1)?$time_years:$time_year) . " $suffix";
}
}