From 39a2564c945f728ac665381ef0447144e24b5d0e Mon Sep 17 00:00:00 2001 From: mpattman Date: Thu, 6 Nov 2014 12:46:49 +0000 Subject: [PATCH] Update functions.inc.php --- src/includes/functions.inc.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php index 9d267637..b810dd9e 100644 --- a/src/includes/functions.inc.php +++ b/src/includes/functions.inc.php @@ -236,6 +236,25 @@ function psm_log_uptime($server_id, $status, $latency) { ); } +/** + * Converts an interval into a string + * + * @param DateInterval $interval + * @return string + */ + function psm_format_interval(DateInterval $interval) { + $result = ""; + + if ($interval->y) { $result .= $interval->format("%y ") . ( ($interval->y == 1) ? psm_get_lang('system', 'year') : psm_get_lang('system', 'years') ) . " "; } + if ($interval->m) { $result .= $interval->format("%m ") . ( ($interval->m == 1) ? psm_get_lang('system', 'month') : psm_get_lang('system', 'months') ) . " "; } + if ($interval->d) { $result .= $interval->format("%d ") . ( ($interval->d == 1) ? psm_get_lang('system', 'day') : psm_get_lang('system', 'days') ) . " "; } + if ($interval->h) { $result .= $interval->format("%h ") . ( ($interval->h == 1) ? psm_get_lang('system', 'hour') : psm_get_lang('system', 'hours') ) . " "; } + if ($interval->i) { $result .= $interval->format("%i ") . ( ($interval->i == 1) ? psm_get_lang('system', 'minute') : psm_get_lang('system', 'minutes') ) . " "; } + if ($interval->s) { $result .= $interval->format("%s ") . ( ($interval->s == 1) ? psm_get_lang('system', 'second') : psm_get_lang('system', 'seconds') ) . " "; } + + return $result; +} + /** * Parses a string from the language file with the correct variables replaced in the message * @@ -254,6 +273,11 @@ function psm_parse_msg($status, $type, $vars) { } $vars['date'] = date('Y-m-d H:i:s'); + $online_date = new DateTime($vars['last_online']); + $offline_date = new DateTime($vars['last_offline']); + $difference = $online_date->diff($offline_date); + $vars['downtime'] = psm_format_interval($difference); + foreach($vars as $k => $v) { $message = str_replace('%' . strtoupper($k) . '%', $v, $message); }