From 826e8fbcfb787563290d852622b5a73331653a1e Mon Sep 17 00:00:00 2001 From: Karsten Heiken Date: Sat, 10 Sep 2011 13:52:41 +0200 Subject: [PATCH 1/2] Improve "prettyTime()" --- application/helpers/MY_date_helper.php | 64 ++++++++++++++++++-------- application/models/server.php | 5 +- 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/application/helpers/MY_date_helper.php b/application/helpers/MY_date_helper.php index 5bb365e..69ca811 100644 --- a/application/helpers/MY_date_helper.php +++ b/application/helpers/MY_date_helper.php @@ -73,28 +73,54 @@ if ( ! function_exists('time_diff')) * @param boolean $includeseconds should seconds be appended to the string? * @return string */ -if ( ! function_exists('secondsToString')) +if ( ! function_exists('prettyTime')) { - function secondsToString($secs, $includeseconds = false) + function prettyTime($secs, $includeseconds = false) { - $days = intval($secs / 86400); - $hours = intval($secs / 3600 % 24); - $minutes = intval($secs / 60 % 60); - $seconds = intval($secs % 60); - if (($minutes + $hours + $days) < 1) - return (sprintf(_('%d seconds'), $seconds)); - else if (($minutes + $hours) < 1) - $string = sprintf(_('%d minutes'), $minutes); - else if ($days < 1) - $string = sprintf(_('%d hours, %d minutes'), $hours, $minutes); + if(!defined('SECOND')) define("SECOND", 1); + if(!defined('MINUTE')) define("MINUTE", 60 * SECOND); + if(!defined('HOUR')) define("HOUR", 60 * MINUTE); + if(!defined('DAY')) define("DAY", 24 * HOUR); + if(!defined('MONTH')) define("MONTH", 30 * DAY); + + if ($secs < 1 * MINUTE) + { + return sprintf(ngettext('one second ago', '%d seconds ago', $secs), $secs); + } + if ($secs < 2 * MINUTE) + { + return _('a minute ago'); + } + if ($secs < 45 * MINUTE) + { + return _('%d minutes ago', floor($secs / MINUTE)); + } + if ($secs < 90 * MINUTE) + { + return _('an hour ago'); + } + if ($secs < 24 * HOUR) + { + return _('%d hours ago', floor($secs / HOUR)); + } + if ($secs < 48 * HOUR) + { + return _('yesterday'); + } + if ($secs < 30 * DAY) + { + return sprintf(_('%d days ago'), floor($secs / DAY)); + } + if ($secs < 12 * MONTH) + { + $months = floor($secs / DAY / 30); + return sprintf(ngettext('one month ago', '%d months ago', $months), $months); + } else - $string = sprintf(_('%d days, %d hours, %d minutes'), $days, $hours, - $minutes); - - if ($includeseconds) - $string .= ' ' . sprintf(_('and %d seconds'), $seconds); - - return $string; + { + $years = floor($secs / DAY / 365); + return sprintf(ngettext('one year ago', '%d years ago', $years), $years); + } } } diff --git a/application/models/server.php b/application/models/server.php index e33ee7a..e221c4e 100644 --- a/application/models/server.php +++ b/application/models/server.php @@ -103,9 +103,8 @@ class Server extends CI_Model { public function getById($serverId) { $this->load->helper('date'); $server = $this->db->get_where('servers', array('id' => $serverId))->row(); - $server->uptimestring = secondsToString($server->uptime); - $server->lastheartbeat = sprintf(_('%s ago'), - secondsToString(time_diff($server->last_update, mysql_now()))); + $server->uptimestring = prettyTime($server->uptime); + $server->lastheartbeat = prettyTime(time_diff($server->last_update, mysql_now())); return $server; } } From 249e433bae90c978467ca64c60b31da55e8405a8 Mon Sep 17 00:00:00 2001 From: Karsten Heiken Date: Sat, 10 Sep 2011 19:19:12 +0200 Subject: [PATCH 2/2] Final implementation of prettyTime() --- application/helpers/MY_date_helper.php | 58 ++++++++++---------------- 1 file changed, 23 insertions(+), 35 deletions(-) diff --git a/application/helpers/MY_date_helper.php b/application/helpers/MY_date_helper.php index 69ca811..87b329d 100644 --- a/application/helpers/MY_date_helper.php +++ b/application/helpers/MY_date_helper.php @@ -77,50 +77,38 @@ if ( ! function_exists('prettyTime')) { function prettyTime($secs, $includeseconds = false) { + if(!defined('SECOND')) define("SECOND", 1); if(!defined('MINUTE')) define("MINUTE", 60 * SECOND); if(!defined('HOUR')) define("HOUR", 60 * MINUTE); if(!defined('DAY')) define("DAY", 24 * HOUR); if(!defined('MONTH')) define("MONTH", 30 * DAY); - - if ($secs < 1 * MINUTE) - { - return sprintf(ngettext('one second ago', '%d seconds ago', $secs), $secs); + + $days = intval($secs / 86400); + $hours = intval($secs / 3600 % 24); + $minutes = intval($secs / 60 % 60); + $seconds = intval($secs % 60); + + $d = sprintf(ngettext('%d day', '%d days', $days), $days); + $h = sprintf(ngettext('%d hour', '%d hours', $hours), $hours); + $m = sprintf(ngettext('%d minute', '%d minutes', $minutes), $minutes); + $s = sprintf(ngettext('%d second', '%d seconds', $seconds), $seconds); + + $output = ""; + if($days > 0) { + $output .= $d; } - if ($secs < 2 * MINUTE) - { - return _('a minute ago'); + if($hours > 0) { + $output .= !empty($output) ? ", ". $h : "". $h; } - if ($secs < 45 * MINUTE) - { - return _('%d minutes ago', floor($secs / MINUTE)); + if($minutes > 0) { + $output .= !empty($output) ? ", ". $m : "". $m; } - if ($secs < 90 * MINUTE) - { - return _('an hour ago'); - } - if ($secs < 24 * HOUR) - { - return _('%d hours ago', floor($secs / HOUR)); - } - if ($secs < 48 * HOUR) - { - return _('yesterday'); - } - if ($secs < 30 * DAY) - { - return sprintf(_('%d days ago'), floor($secs / DAY)); - } - if ($secs < 12 * MONTH) - { - $months = floor($secs / DAY / 30); - return sprintf(ngettext('one month ago', '%d months ago', $months), $months); - } - else - { - $years = floor($secs / DAY / 365); - return sprintf(ngettext('one year ago', '%d years ago', $years), $years); + if($includeseconds || empty($output)) { + $output .= !empty($output) ? ", ". $s : "". $s; } + + return $output; } }